Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/actionlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# GitHub-hosted labels newer than the pinned actionlint release.
self-hosted-runner:
labels: [ubuntu-26.04, ubuntu-26.04-arm]
54 changes: 46 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
matrix:
os: [ubuntu-22.04, ubuntu-24.04, ubuntu-24.04-arm, macos-latest, windows-latest]
swift: ["6.4.1", "6.3.3"]
# swift.org publishes Ubuntu 26.04 toolchains from 6.4 on.
include:
- { os: ubuntu-26.04, swift: "6.4.1" }
- { os: ubuntu-26.04-arm, swift: "6.4.1" }
steps:
- uses: actions/checkout@v7
- id: setup
Expand Down Expand Up @@ -66,6 +70,9 @@ jobs:
matrix:
os: [ubuntu-22.04, ubuntu-24.04, ubuntu-24.04-arm]
swift: ["6.4.1", "6.3.3"]
include:
- { os: ubuntu-26.04, swift: "6.4.1" }
- { os: ubuntu-26.04-arm, swift: "6.4.1" }
steps:
- uses: actions/checkout@v7
- id: setup
Expand All @@ -76,6 +83,17 @@ jobs:
env:
SOURCE: ${{ steps.setup.outputs.source }}
run: test "$SOURCE" = cache
- id: again
uses: ./
with:
swift-version: ${{ matrix.swift }}
- name: Two runs in one job resolve the same swift-path
env:
FIRST: ${{ steps.setup.outputs.swift-path }}
SECOND: ${{ steps.again.outputs.swift-path }}
run: |
echo "$FIRST / $SECOND"
test -n "$FIRST" && test "$FIRST" = "$SECOND"

unsupported-version:
runs-on: ubuntu-24.04
Expand Down Expand Up @@ -164,8 +182,10 @@ jobs:
export RUNNER_TEMP="${RUNNER_TEMP:-/tmp}" GITHUB_OUTPUT="$PWD/out.txt" GITHUB_PATH="$PWD/path.txt"
: > "$GITHUB_OUTPUT"; : > "$GITHUB_PATH"
bash plan-linux.sh
export SWIFT_URL SWIFT_FILE
export SWIFT_URL SWIFT_FILE INSTALL_DIR
SWIFT_URL="$(sed -n 's/^url=//p' out.txt)"; SWIFT_FILE="$(sed -n 's/^file=//p' out.txt)"
INSTALL_DIR="$(sed -n 's/^install-dir=//p' out.txt)"
echo "install dir: $INSTALL_DIR"
dl="$RUNNER_TEMP/dl-cache"

echo "== 1. populate: downloads and verifies"
Expand All @@ -176,19 +196,37 @@ jobs:
DOWNLOAD_DIR="$dl" bash install-linux.sh
tail -1 out.txt | grep -q '^source=cache$'
test "$(grep -c . path.txt)" -eq 2
test "$(sort -u path.txt | wc -l)" -eq 2 # a fresh directory per run
test "$(sort -u path.txt | wc -l)" -eq 1 # deterministic path keeps SwiftPM caches valid

echo "== 3. planted extracted tree and fake swift are never used (cache disabled)"
planted="$RUNNER_TOOL_CACHE/swift/planted"
mkdir -p "$planted/usr/bin"
printf '#!/bin/sh\ntouch "%s/planted-ran"\n' "$RUNNER_TEMP" > "$planted/usr/bin/swift"
chmod +x "$planted/usr/bin/swift"
echo "== 3. a planted tree at the deterministic path is wiped, never used (cache disabled)"
mkdir -p "$INSTALL_DIR/usr/bin" "$INSTALL_DIR/evil"
printf '#!/bin/sh\ntouch "%s/planted-ran"\n' "$RUNNER_TEMP" > "$INSTALL_DIR/usr/bin/swift"
chmod +x "$INSTALL_DIR/usr/bin/swift"
touch "$INSTALL_DIR/evil/marker"
: > path.txt
env -u DOWNLOAD_DIR bash install-linux.sh
tail -1 out.txt | grep -q '^source=download$'
if grep -q planted path.txt; then exit 1; fi
test ! -e "$INSTALL_DIR/evil/marker"
test ! -e "$RUNNER_TEMP/planted-ran"
"$INSTALL_DIR/usr/bin/swift" --version | grep -q "swift-$SWIFT_VERSION-RELEASE"
test ! -e "$RUNNER_TEMP/planted-ran"

echo "== 3b. unexpected install directories are refused"
for bad in "" / "$RUNNER_TEMP" "$RUNNER_TEMP/setup-swift/../x" "$RUNNER_TOOL_CACHE/swift-verified/x/y"; do
if INSTALL_DIR="$bad" DOWNLOAD_DIR="$dl" bash install-linux.sh; then echo "accepted '$bad'"; exit 1; fi
done
test -d "$RUNNER_TEMP"

echo "== 3c. a symlinked path component is refused and its target left untouched"
decoy="$(mktemp -d)"
touch "$decoy/marker"
rm -r -f -- "$RUNNER_TEMP/setup-swift/$SWIFT_VERSION"
ln -s "$decoy" "$RUNNER_TEMP/setup-swift/$SWIFT_VERSION"
if DOWNLOAD_DIR="$dl" bash install-linux.sh; then echo "installed through a symlink"; exit 1; fi
test -e "$decoy/marker"
test "$(find "$decoy" -mindepth 1 | wc -l)" -eq 1
rm -f -- "$RUNNER_TEMP/setup-swift/$SWIFT_VERSION"

echo "== 4. tampered archive fails closed (no silent re-download)"
cp -r "$dl" "$RUNNER_TEMP/dl-bad-archive"
printf 'x' >> "$RUNNER_TEMP/dl-bad-archive/$SWIFT_FILE"
Expand Down
81 changes: 81 additions & 0 deletions .github/workflows/watch-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Watch latest Linux

# Weekly: installs the newest swift.org release on every Ubuntu that swift.org publishes it for AND
# GitHub offers a hosted runner for, so new Ubuntu releases are exercised as soon as both exist
# (plan-linux.sh maps any Ubuntu version generically). Failures open or update a tracking issue.
on:
schedule:
- cron: "17 3 * * 1"
workflow_dispatch:

permissions:
contents: read

jobs:
discover:
runs-on: ubuntu-latest
outputs:
swift: ${{ steps.find.outputs.swift }}
runners: ${{ steps.find.outputs.runners }}
steps:
- id: find
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
releases="$(curl -sSfL --retry 3 https://www.swift.org/api/v1/install/releases.json)"
swift="$(jq -r '.[-1].name' <<< "$releases")"
[[ "$swift" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]] || { echo "::error::unexpected release name '$swift'"; exit 1; }
ubuntus="$(jq -r '.[-1].platforms[].name | capture("^Ubuntu (?<v>[0-9]{2}\\.[0-9]{2})$").v' <<< "$releases")"
labels="$(gh api repos/actions/runner-images/contents/README.md -H 'Accept: application/vnd.github.raw' \
| grep -oE "\`ubuntu-[0-9]{2}\\.[0-9]{2}(-arm)?\`" | tr -d '`' | sort -u)"
runners="$(for v in $ubuntus; do grep -E "^ubuntu-${v//./\\.}(-arm)?$" <<< "$labels" || true; done | jq -R . | jq -cs .)"
[ "$runners" != "[]" ] || { echo "::error::no Ubuntu runner matches swift.org $swift ($ubuntus)"; exit 1; }
echo "Swift $swift on $runners"
{ echo "swift=$swift"; echo "runners=$runners"; } >> "$GITHUB_OUTPUT"

install:
name: install (${{ matrix.os }}, ${{ needs.discover.outputs.swift }})
needs: discover
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ${{ fromJSON(needs.discover.outputs.runners) }}
steps:
- uses: actions/checkout@v7
- id: setup
uses: ./
with:
swift-version: ${{ needs.discover.outputs.swift }}
cache: "false"
- name: Compile and run hello world
env:
WANT: ${{ needs.discover.outputs.swift }}
VERSION: ${{ steps.setup.outputs.version }}
run: |
set -euo pipefail
test "$VERSION" = "$WANT"
mkdir hello && cd hello
swift package init --type executable --name hello
swift run

report:
needs: [discover, install]
if: failure() && github.event_name == 'schedule'
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
SWIFT: ${{ needs.discover.outputs.swift }}
RUNNERS: ${{ needs.discover.outputs.runners }}
run: |
set -euo pipefail
title="Latest Linux check failing"
body="Swift \`${SWIFT:-?}\` on \`${RUNNERS:-?}\` failed: $RUN_URL"
num="$(gh issue list --state open --search "\"$title\" in:title" --json number --jq '.[0].number // empty')"
if [ -n "$num" ]; then gh issue comment "$num" --body "$body"; else gh issue create --title "$title" --body "$body"; fi
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ see [How verification works](#how-verification-works)). It never runs a pre-exis
| `swift-version` | yes | | Exact toolchain version, e.g. `6.4.1` or `6.3.3`. Ranges and `latest` are not supported. |
| `swift-sdks` | no | | Cross-compilation Swift SDKs to install after the toolchain: `static-linux`, `wasm`, `android` (comma or space separated). |
| `use-preinstalled` | no | `false` | **Unverified opt-in.** If `swift` on `PATH` already reports exactly `swift-version`, run and use it instead of installing. By default no pre-existing binary is ever executed. |
| `cache` | no | `true` | Cache the downloaded Linux toolchain archive and its `.sig` with `actions/cache`. The archive is re-verified with GPG on every run and extracted into a fresh directory; `false` reuses nothing on disk. |
| `cache` | no | `true` | Cache the downloaded Linux toolchain archive and its `.sig` with `actions/cache`. The archive is re-verified with GPG on every run and extracted into a clean per-version directory; `false` reuses nothing on disk. |

## Outputs

Expand All @@ -40,24 +40,31 @@ emitted); that toolchain is **not verified** by this action.

| Platform | Artifact | Verification |
| --- | --- | --- |
| Ubuntu 20.04 / 22.04 / 24.04, x86_64 and aarch64 | `swift-<v>-RELEASE-ubuntuNN.NN[-aarch64].tar.gz` | GPG `.sig` against pinned swift.org keys |
| Ubuntu (any release swift.org publishes, e.g. 22.04 / 24.04 / 26.04), x86_64 and aarch64 | `swift-<v>-RELEASE-ubuntuNN.NN[-aarch64].tar.gz` | GPG `.sig` against pinned swift.org keys |
| macOS (any runner) | `swift-<v>-RELEASE-osx.pkg` | `pkgutil --check-signature`, notarization, pinned Developer ID team |
| Windows, x86_64 and arm64 | `swift-<v>-RELEASE-windows10[-arm64].exe` | Authenticode `Valid` and signer subject (Apple Inc.) |
| Swift SDKs (`static-linux`, `wasm`, `android`) on any host | `swift-<v>-RELEASE_<kind>.artifactbundle.tar.gz` | sha256 pinned in `sdk-checksums.txt`, plus GPG `.sig` when `gpg` exists |

Other Linux distributions and architectures fail with an explicit error. Not every version is
published for every platform (for example, newer releases have no Ubuntu 20.04 build); a missing
published for every platform (for example, newer releases have no Ubuntu 20.04 build and Ubuntu 26.04
starts with 6.4); a missing
download fails with the URL that was tried.

Linux: the tarball and `.sig` are kept in `$RUNNER_TOOL_CACHE/swift-archives/...` and cached (key: version,
platform, arch). Every run, including a cache hit, re-verifies them with GPG and extracts into a new private
directory under `$RUNNER_TEMP`, which is added to `PATH`; extracted trees are never cached or reused. A cache
entry that fails verification fails the job instead of being re-downloaded. Missing build prerequisites (`libcurl4-openssl-dev`,
platform, arch). Every run, including a cache hit, copies them to a private snapshot, re-verifies the snapshot with
GPG, and extracts that same snapshot into `$RUNNER_TEMP/setup-swift/<version>/<arch>-<platform>`, which is added to
`PATH`. The path is stable across runs so SwiftPM build caches (which record the compiler path) stay valid. Before
extraction the directory is validated (exactly that shape, no `..`, and no symlink in any component from
`RUNNER_TEMP` down, checked again just before use) and removed entirely, so nothing previously on disk is reused.
A cache entry that fails verification fails the job instead of being re-downloaded. Isolation model:
`RUNNER_TEMP` is private to one runner instance and emptied for every job on GitHub-hosted runners, so earlier or
concurrent jobs cannot plant or wipe the toolchain. Self-hosted runners must not share `RUNNER_TEMP` between
concurrently running jobs, and must run jobs of different trust levels on separate runner instances. Missing build prerequisites (`libcurl4-openssl-dev`,
`libxml2-dev`, the GCC dev packages, ...) are installed with `apt-get` only if absent.

macOS: the package is installed into the user domain (`~/Library/Developer/Toolchains`), falling back to
`sudo installer -target /`. `TOOLCHAINS` is exported with the toolchain's bundle identifier and its
`usr/bin` is prepended to `PATH`. macOS installs are not cached.
`usr/bin` is prepended to `PATH`. The install path is deterministic (like the Linux and Windows ones), so SwiftPM build caches stay valid. macOS installs are not cached.

Windows: the installer runs silently (`/install /quiet`) into `%LocalAppData%\Programs\Swift`; the toolchain and
runtime `bin` directories are added to `PATH` and `SDKROOT` is set. Visual Studio (C++ tools, Windows SDK),
Expand Down Expand Up @@ -139,6 +146,12 @@ compressed; use `curl --compressed`). The fingerprints were cross-checked agains
## Development

CI (`.github/workflows/ci.yml`) runs shellcheck, actionlint, installs 6.4.1 and 6.3.3 on
`ubuntu-22.04`, `ubuntu-24.04`, `ubuntu-24.04-arm`, `macos-latest` and `windows-latest`, checks the Linux
`ubuntu-22.04`, `ubuntu-24.04`, `ubuntu-24.04-arm`, `macos-latest` and `windows-latest` (plus 6.4.1 on
`ubuntu-26.04` and `ubuntu-26.04-arm`), checks the Linux
cache-hit path, and builds hello world with the static Linux, WebAssembly and Android SDKs. Not covered by CI:
Ubuntu 20.04 (no 6.x tarballs for recent versions), Windows arm64, running WebAssembly output.

`.github/workflows/watch-linux.yml` runs weekly: it installs the newest swift.org release on every Ubuntu
runner that both swift.org and GitHub-hosted runners offer, so a new Ubuntu release is tested as soon as it
appears (no code change is needed for a new Ubuntu version). A failing scheduled run opens or comments on a
"Latest Linux check failing" issue.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ runs:
env:
SWIFT_URL: ${{ steps.plan.outputs.url }}
SWIFT_FILE: ${{ steps.plan.outputs.file }}
INSTALL_DIR: ${{ steps.plan.outputs.install-dir }}
DOWNLOAD_DIR: ${{ inputs.cache == 'true' && steps.plan.outputs.download-dir || '' }}
run: bash "$GITHUB_ACTION_PATH/install-linux.sh"

Expand Down
59 changes: 52 additions & 7 deletions install-linux.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env bash
# Installs a swift.org Linux toolchain. The tarball and its .sig are verified with GPG on
# EVERY run (see verify-gpg.sh) and extracted into a fresh private directory, so nothing
# previously on disk is ever executed. DOWNLOAD_DIR may hold a cached copy of the archive
# EVERY run (see verify-gpg.sh) and extracted from a private snapshot (the very bytes that were verified) into INSTALL_DIR, a stable
# per-version path under RUNNER_TEMP (stable compiler paths keep SwiftPM build caches valid).
# INSTALL_DIR is removed completely first, so nothing previously on disk is ever executed. DOWNLOAD_DIR may hold a cached copy of the archive
# pair to skip the download; it is re-verified before use. Without DOWNLOAD_DIR
# (cache: false) nothing on disk is reused.
# Inputs (env): SWIFT_URL, SWIFT_FILE; optional DOWNLOAD_DIR, GITHUB_PATH, GITHUB_OUTPUT.
# Inputs (env): SWIFT_URL, SWIFT_FILE, INSTALL_DIR; optional DOWNLOAD_DIR, GITHUB_PATH, GITHUB_OUTPUT.
set -euo pipefail

url="${SWIFT_URL:?}"
file="${SWIFT_FILE:?}"
dest="${INSTALL_DIR:?}"
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=verify-gpg.sh
. "$here/verify-gpg.sh"
Expand All @@ -21,10 +23,14 @@ missing=()
# GCC development packages per swift.org's prerequisites for each Ubuntu release.
# shellcheck disable=SC1091
case "$(. /etc/os-release && echo "${VERSION_ID:-}")" in
26.04) gcc_ver=15 ;;
24.04) gcc_ver=13 ;;
22.04) gcc_ver=12 ;;
*) gcc_ver=10 ;;
20.04) gcc_ver=10 ;;
# Newer releases: the distribution's default GCC (what swift.org's Dockerfiles have used so far).
*) gcc_ver="$(apt-cache depends gcc 2> /dev/null | grep -m1 -oE 'gcc-[0-9]+' | cut -d- -f2 || true)" ;;
esac
[ -n "$gcc_ver" ] || { echo "::error::Cannot determine the GCC version for this Ubuntu release"; exit 1; }
for pkg in curl gnupg2 binutils git libcurl4-openssl-dev libedit2 libsqlite3-0 libxml2-dev \
libncurses-dev pkg-config tzdata uuid-dev zlib1g-dev "libgcc-${gcc_ver}-dev" "libstdc++-${gcc_ver}-dev"; do
dpkg -s "$pkg" > /dev/null 2>&1 || missing+=("$pkg")
Expand Down Expand Up @@ -63,11 +69,50 @@ else
mv "$archive_dir/$file.partial" "$archive_dir/$file"
fi

# Work on a private snapshot: the cache path can be rewritten by others between verification
# and extraction, so verify and extract the very same copy.
snap="$scratch/snapshot"
mkdir "$snap"
cp "$archive_dir/$file" "$snap/$file"
cp "$archive_dir/$file.sig" "$snap/$file.sig"

# Fail closed: a tampered cache entry is NOT replaced by a fresh download.
verify_gpg_signature "$archive_dir/$file" "$archive_dir/$file.sig" "$scratch"
verify_gpg_signature "$snap/$file" "$snap/$file.sig" "$scratch"

# The install directory must be exactly $RUNNER_TEMP/setup-swift/<version>/<platform>, with no
# symlink anywhere from RUNNER_TEMP down (checked again right before use).
case "${RUNNER_TEMP:-}" in
/*) ;;
*) echo "::error::RUNNER_TEMP must be set to an absolute path"; exit 1 ;;
esac
case "$dest" in
"$RUNNER_TEMP"/setup-swift/*/*) ;;
*) echo "::error::Refusing unexpected install directory '$dest'"; exit 1 ;;
esac
case "$dest" in
*..* | */./* | *//* | */) echo "::error::Refusing unexpected install directory '$dest'"; exit 1 ;;
esac

assert_no_symlinks() { # path: every component from RUNNER_TEMP down must not be a symlink
local cur="$RUNNER_TEMP" rest part
[ ! -L "$cur" ] || { echo "::error::$cur is a symlink"; return 1; }
rest="${1#"$RUNNER_TEMP"/}"
while [ -n "$rest" ]; do
part="${rest%%/*}"
cur="$cur/$part"
if [ -L "$cur" ]; then echo "::error::$cur is a symlink; refusing to install through it"; return 1; fi
case "$rest" in */*) rest="${rest#*/}" ;; *) rest="" ;; esac
done
}

dest="$(mktemp -d "${RUNNER_TEMP:-/tmp}/swift-toolchain.XXXXXX")"
tar -xzf "$archive_dir/$file" -C "$dest" --strip-components=1
(umask 077 && mkdir -p "$(dirname "$dest")")
assert_no_symlinks "$dest"
# Never reuse existing contents: wipe, then extract only the snapshot verified above.
rm -r -f -- "$dest"
mkdir "$dest"
assert_no_symlinks "$dest"
tar -xzf "$snap/$file" -C "$dest" --strip-components=1 --no-same-owner
rm -r -f -- "$snap"
[ -x "$dest/usr/bin/swift" ] || { echo "::error::$dest/usr/bin/swift is missing after extraction"; exit 1; }
if [ -n "${GITHUB_PATH:-}" ]; then echo "$dest/usr/bin" >> "$GITHUB_PATH"; fi
if [ -n "${GITHUB_OUTPUT:-}" ]; then echo "source=$source" >> "$GITHUB_OUTPUT"; fi
Expand Down
24 changes: 15 additions & 9 deletions plan-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ fi

# shellcheck disable=SC1091
. /etc/os-release
case "${ID:-}-${VERSION_ID:-}" in
ubuntu-20.04) dir_platform=ubuntu2004; file_platform=ubuntu20.04 ;;
ubuntu-24.04) dir_platform=ubuntu2404; file_platform=ubuntu24.04 ;;
ubuntu-22.04) dir_platform=ubuntu2204; file_platform=ubuntu22.04 ;;
*)
echo "::error::Unsupported Linux distribution '${ID:-?} ${VERSION_ID:-?}'; setup-swift supports Ubuntu 20.04, 22.04 and 24.04"
exit 1
;;
esac
# Any Ubuntu release maps to swift.org's naming (ubuntu2604 / ubuntu26.04), so new LTS releases work
# without a code change; a version swift.org does not publish fails at download time.
if [ "${ID:-}" = ubuntu ] && [[ "${VERSION_ID:-}" =~ ^[0-9]{2}\.[0-9]{2}$ ]]; then
dir_platform="ubuntu${VERSION_ID//./}"
file_platform="ubuntu${VERSION_ID}"
else
echo "::error::Unsupported Linux distribution '${ID:-?} ${VERSION_ID:-?}'; setup-swift supports Ubuntu releases published on swift.org"
exit 1
fi

case "$(uname -m)" in
x86_64) arch=x86_64; arch_suffix="" ;;
Expand All @@ -33,10 +33,16 @@ esac
base="https://download.swift.org/swift-${v}-release/${dir_platform}${arch_suffix}/swift-${v}-RELEASE"
file="swift-${v}-RELEASE-${file_platform}${arch_suffix}.tar.gz"
root="${RUNNER_TOOL_CACHE:-${RUNNER_TEMP:-/tmp}}"
# The toolchain lives under RUNNER_TEMP: private to this runner instance and emptied for every job.
case "${RUNNER_TEMP:-}" in
/*) ;;
*) echo "::error::RUNNER_TEMP must be set to an absolute path"; exit 1 ;;
esac

{
echo "url=${base}/${file}"
echo "file=${file}"
echo "install-dir=${RUNNER_TEMP}/setup-swift/${v}/${arch}-${dir_platform}"
echo "download-dir=${root}/swift-archives/${v}-${arch}-${dir_platform}"
echo "cache-key=swift-toolchain-${v}-${dir_platform}-${arch}-archive-v2"
} >> "${GITHUB_OUTPUT:?}"
Loading