Hello USD - Part 14: Now to get GitHub to run the tests
This article is part of a series.
- Is my code making the
.usda
files I think it should be? (XCTests) - [] Does my code compile on Linux?
- [] GitHub Action <== WE ARE HERE
- [] VM <== AND HERE
- [] Are my
.usda
files correct according tousdchecker
?
Most of the GitHub Action workflow files discussed can be found at: https://github.com/carlynorama/SketchPad/tree/main/.github/workflows
(July 2023, Swift 5.8, Ubuntu 22.04, MacOS’s 12 and 13)
For Linux
Since so much of the OpenUSD world seems to be focused on Linux work stations, I’d like SketchPad to run on them. While I do have a actual Linux machines and VM’s to test on at home, a GitHub action will let me know faster.
SketchPad doesn’t have a lot of bells or whistles so a fairly simple GitHub Action will work for it.
name: Swift, Linux Latest (swift-actions)
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
## NOTE: better practice to point to a hash.
- uses: swift-actions/setup-swift@v1
with:
swift-version: "5.8"
- uses: actions/checkout@v3
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
The above example uses a 3rd party plugin called Setup Swift by Swift Actions, but there are many in the GitHub action marketplace that allow for configuration as needed.
I wrote that action from a combo of a couple found in official GitHub documentaion, but I am explicitly NOT pointing at the hash or subversion. If the latest update breaks my build, I want to know.
I’d forgotten that last time I used the official swift docker container. That version also works.
name: Swift, Latest Ubuntu, Container
on:
push:
branches:
- '*'
pull_request:
branches:
- main
jobs:
build:
name: Build Linux
runs-on: ubuntu-latest
container:
image: swift:latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build
run: |
swift build -v
- name: Test
run: |
swift test -v
For MacOS
It’s actually HARDER to get Swift 5.8 running on a MacOS runner.
macos-latest
has two problems.
- Runs macOS 12 officially. Interestingly I saw it both pass and fail my new
if #available(macOS 13, *)
RegexBuilder. Build Details - Uses Swift Tools version 5.7
Redoing Regex
Just to not have to worry about it I swapped out the fancy RegexBuilder code with the older style.
func headerMatch(_ toTest:String) -> Bool {
let pattern = #"^#usda 1\.0\n\([\s\S]+\n\)$"#
var result = toTest.range(
of: pattern,
options: .regularExpression
)
return (result != nil)
}
Helpful Regex Links:
- https://paiv.github.io/swift-rxbgen/ -> RegexBuilder converter. Found it helpful to verify I was doing what I thought I was. It translated my new regex literal to the below.
let regex = Regex {
Anchor.startOfLine
"#usda 1.0"
Capture {
OneOrMore {
CharacterClass(.whitespace, .whitespace.inverted)
//I would choose could be CharacterClass.any, because .any matches newline, unlike `.`
}
}
"\n)"
Anchor.endOfLine //should be Anchor.endOfSubject?
}
- Shows older style of working with regex in Swift: https://www.advancedswift.com/regular-expressions/
- Never not useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet
- Using regex literals in Packages before using 5.9: https://stackoverflow.com/questions/75573646/does-the-swift-package-manager-support-regex-literals
- Using regex in scripts: https://stackoverflow.com/questions/75615132/can-regex-literals-be-used-in-a-swift-script
Working GitHub Action A
The following GitHub action runs consistently successfully for my code.
name: Swift, MacOS, Swift 5.8
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: macos-latest
steps:
##NOTE: better practice to point to hash.
- uses: swift-actions/setup-swift@v1
with:
swift-version: "5.8"
- uses: actions/checkout@v3
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
Working GitHub Action B
For the record, GitHub allows requests for specific version of macOS. MacOS 13 is currently in Beta. (macos-13
or macos-13-xl
). Runner Build details
name: Swift, MacOS, Force 13
on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]
jobs:
build:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
Unused Matrix Action
Helpful action that can check multiple OS’s and swift versions. Included for reference (updated from that GitHub link in the Linux section), but I personally prefer separate scripts so I can see at least SOME green check marks.
name: Swift Matrix, v2
on: [push]
jobs:
build:
name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
swift: ["5.8"]
runs-on: ${{ matrix.os }}
steps:
- uses: swift-actions/setup-swift@v1
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v3
- name: Build
run: swift build
- name: Run tests
run: swift test
Confirming on the VM
Opened up the VirtualBox instance set up for testing Tipsy-Robot. I installed Swift by hand on that VM. Notes in the link. Next one I might try this tool: https://github.com/stevapple/swiftbox
cd ~/$DESIRED_PATH
git clone https://github.com/carlynorama/SketchPad.git
cd SketchPad
swift run sketchpad multiball -s
All good!