Hello USD - Part 14: Now to get GitHub to run the tests

This article is part of a series.

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.

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)
    }
        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?
        }

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!

Screenshot of the Linux virtual machine with a VSCode window open to the SketchPad project with a freshly minted .usda file.

This article is part of a series.