swift version + github actions
Github Actions is great for automating coding tasks and the Ubuntu runners come with a ton of preinstalled tools. At the time of this writing the ubuntu-latest
runner is version 24.04.
While tons of preinstalled tools is super handy, what if the version of the tool is incompatible with the code you are working with? This is the scenario I ran in to when attempting to build a Swift project that depends on an outdated package which fails to build with Swift 6.
solution
While there are a couple of actions that seek to solve this problem, they don't strike me as having widespread use or being kept current, so I don't want to rely on those. That is just me though, maybe they are actually viable options.
Instead, I decided to use the official Swift toolchain manager Swiftly. This is a relatively new project, but I like that it is sponsored by the SSWG. It should be noted that it does not officially support Ubuntu 24.04 at the moment, but I'm sure that will change.
So, here's the documented install command:
curl -L https://swiftlang.github.io/swiftly/swiftly-install.sh | bash
Easy enough but after executing that, there is a series of confirmation questions, so this not good for a headless environment like Github Actions. The readme does not mention it but, this is a flag that can be passed to the script that will help, --disable-confirmation
.
But how to pass that flag on execution of the script? Well I didn't know so I asked AI. Here's how it is done:
curl -L https://swiftlang.github.io/swiftly/swiftly-install.sh | bash -s -- --disable-confirmation
And the helpful AI explanation:
Explanation:
• -s tells bash to read from standard input (the script from curl).
• -- separates bash options from script arguments.
• --disable-confirmation is passed as a flag to the script.
Fantastic! Now with that we can update the Github Action config file to get the desired version of Swift on the runner:
name: Swift
on:
push:
branches:
- main
jobs:
gh-pages:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Swift
run: |
curl -L https://swiftlang.github.io/swiftly/swiftly-install.sh | bash -s -- --disable-confirmation
swiftly install 5.10.1
swiftly use 5.10.1
- name: Build Swift
run: swift build
Boom, we're back in business.