Skip to content

Commit 8eeb89f

Browse files
leogdionclaude
andcommitted
Modernize Swift CI tutorial (current versions, refreshed pins, cross-platform)
- Update example Swift versions from 5.2/5.3/5.3.3 to 5.10/6.0/6.1 - Refresh swift-actions/setup-swift pin from a 2021 commit to v2.4.0 - Add "Building and testing on Linux with a container" section, linking the release (swift) and nightly (swiftlang/swift) Docker images, with a nightly example - Add native "Building and testing for Apple platforms with Xcode" section (xcodebuild + simulator destinations), including how to check available Xcode/simulators on the macOS runner and how to download missing platform runtimes - Add "Building and testing on Windows" (compnerd/gha-setup-swift) and "Building and testing on Android" (skiptools/swift-android-action) sections - Add "Building and testing across multiple platforms with one action" (brightdigit/swift-build) with per-platform examples for Apple, Linux, Windows, Android, and WebAssembly, including the download-platform input Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d19ad45 commit 8eeb89f

1 file changed

Lines changed: 280 additions & 8 deletions

File tree

  • content/actions/tutorials/build-and-test-code

content/actions/tutorials/build-and-test-code/swift.md

Lines changed: 280 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ jobs:
107107
strategy:
108108
matrix:
109109
os: [ubuntu-latest, macos-latest]
110-
swift: ["5.2", "5.3"]
110+
swift: ["6.1", "6.2", "6.3"]
111111
runs-on: {% raw %}${{ matrix.os }}{% endraw %}
112112
steps:
113-
- uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
113+
- uses: swift-actions/setup-swift@7ca6abe6b3b0e8b5421b88be48feee39cbf52c6a # v2.4.0
114114
with:
115115
swift-version: {% raw %}${{ matrix.swift }}{% endraw %}
116116
- uses: {% data reusables.actions.action-checkout %}
@@ -122,16 +122,16 @@ jobs:
122122

123123
### Using a single specific Swift version
124124

125-
You can configure your job to use a single specific version of Swift, such as `5.3.3`.
125+
You can configure your job to use a single specific version of Swift, such as `6.3`.
126126

127127
```yaml copy
128128
{% data reusables.actions.actions-not-certified-by-github-comment %}
129129
steps:
130-
- uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
130+
- uses: swift-actions/setup-swift@7ca6abe6b3b0e8b5421b88be48feee39cbf52c6a # v2.4.0
131131
with:
132-
swift-version: "5.3.3"
132+
swift-version: "6.3"
133133
- name: Get swift version
134-
run: swift --version # Swift 5.3.3
134+
run: swift --version # Swift 6.3
135135
```
136136

137137
## Building and testing your code
@@ -142,11 +142,283 @@ You can use the same commands that you use locally to build and test your code u
142142
{% data reusables.actions.actions-not-certified-by-github-comment %}
143143
steps:
144144
- uses: {% data reusables.actions.action-checkout %}
145-
- uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
145+
- uses: swift-actions/setup-swift@7ca6abe6b3b0e8b5421b88be48feee39cbf52c6a # v2.4.0
146146
with:
147-
swift-version: "5.3.3"
147+
swift-version: "6.3"
148148
- name: Build
149149
run: swift build
150150
- name: Run tests
151151
run: swift test
152152
```
153+
154+
## Building and testing on Linux with a container
155+
156+
On {% data variables.product.prodname_dotcom %}-hosted Linux runners, you can run your job inside an official Swift container image, which already includes the Swift toolchain. This means you don't need a separate step to install Swift.
157+
158+
Released Swift versions are published as the [`swift`](https://hub.docker.com/_/swift) official images on Docker Hub (for example, `swift:6.3` or `swift:6.3-noble`). To test against unreleased toolchains, use the nightly snapshot images published as [`swiftlang/swift`](https://hub.docker.com/r/swiftlang/swift) (for example, `swiftlang/swift:nightly-main` or `swiftlang/swift:nightly-6.2-noble`). For more information, see [Swift on Docker](https://www.swift.org/documentation/docker/) on Swift.org.
159+
160+
```yaml copy
161+
name: Swift
162+
163+
on: [push]
164+
165+
jobs:
166+
linux:
167+
runs-on: ubuntu-latest
168+
container: swift:6.3
169+
steps:
170+
- uses: {% data reusables.actions.action-checkout %}
171+
- name: Build
172+
run: swift build
173+
- name: Run tests
174+
run: swift test
175+
```
176+
177+
To test against a nightly toolchain, use a `swiftlang/swift` snapshot image instead.
178+
179+
```yaml copy
180+
name: Swift
181+
182+
on: [push]
183+
184+
jobs:
185+
nightly:
186+
runs-on: ubuntu-latest
187+
container: swiftlang/swift:nightly-main
188+
steps:
189+
- uses: {% data reusables.actions.action-checkout %}
190+
- name: Build
191+
run: swift build
192+
- name: Run tests
193+
run: swift test
194+
```
195+
196+
## Building and testing for Apple platforms with Xcode
197+
198+
To build and test for a specific Apple platform such as iOS, watchOS, tvOS, or visionOS, use `xcodebuild` on a macOS runner and select a simulator with the `-destination` option. {% data variables.product.prodname_dotcom %}-hosted macOS runners come with Xcode and Apple platform simulators preinstalled. To check which Xcode versions, platform SDKs, OS versions, and simulators are available on each macOS runner, see [AUTOTITLE](/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software).
199+
200+
```yaml copy
201+
name: Swift
202+
203+
on: [push]
204+
205+
jobs:
206+
ios:
207+
runs-on: macos-latest
208+
steps:
209+
- uses: {% data reusables.actions.action-checkout %}
210+
- name: Build and test for iOS
211+
run: |
212+
xcodebuild test \
213+
-scheme MyPackage \
214+
-destination 'platform=iOS Simulator,name=iPhone 15,OS=17.5'
215+
```
216+
217+
To target a different platform, change the `-destination` value. For example, use `platform=watchOS Simulator,name=Apple Watch Series 9 (45mm)`, `platform=tvOS Simulator,name=Apple TV`, `platform=visionOS Simulator,name=Apple Vision Pro`, or `platform=macOS` for native macOS. To select a specific installed Xcode version, run `sudo xcode-select -s /Applications/Xcode_16.4.app` before building.
218+
219+
If you target a platform whose simulator runtime is not preinstalled on the runner, the build fails until the runtime is available. Download it first with `xcodebuild -downloadPlatform iOS` (replacing `iOS` with the platform you need), or choose a simulator and OS version that the runner already provides.
220+
221+
## Building and testing on Windows
222+
223+
To build and test on Windows, install the Swift toolchain with the [`compnerd/gha-setup-swift`](https://github.com/compnerd/gha-setup-swift) action, then run `swift build` and `swift test`.
224+
225+
```yaml copy
226+
227+
{% data reusables.actions.actions-not-certified-by-github-comment %}
228+
229+
{% data reusables.actions.actions-use-sha-pinning-comment %}
230+
231+
name: Swift
232+
233+
on: [push]
234+
235+
jobs:
236+
windows:
237+
runs-on: windows-latest
238+
steps:
239+
- uses: compnerd/gha-setup-swift@eeda069c5bc95ac8a9ac5cea7d4f588ae5420ca5 # v0.4.0
240+
with:
241+
swift-version: swift-6.3-release
242+
swift-build: 6.3-RELEASE
243+
- uses: {% data reusables.actions.action-checkout %}
244+
- name: Build
245+
run: swift build
246+
- name: Run tests
247+
run: swift test
248+
```
249+
250+
## Building and testing on Android
251+
252+
To build and test on Android, use the [`skiptools/swift-android-action`](https://github.com/skiptools/swift-android-action) action, which installs the Swift SDK for Android and can run your tests on an emulator.
253+
254+
```yaml copy
255+
256+
{% data reusables.actions.actions-not-certified-by-github-comment %}
257+
258+
{% data reusables.actions.actions-use-sha-pinning-comment %}
259+
260+
name: Swift
261+
262+
on: [push]
263+
264+
jobs:
265+
android:
266+
runs-on: ubuntu-latest
267+
steps:
268+
- uses: {% data reusables.actions.action-checkout %}
269+
- uses: skiptools/swift-android-action@2044b79660f201ccef8cbce62877e4ec5409f9c8 # v2.9.5
270+
with:
271+
swift-version: "6.3"
272+
android-api-level: 28
273+
```
274+
275+
## Building and testing across multiple platforms with one action
276+
277+
If you want to build and test across several of the platforms above from a single workflow, you can use a community action that wraps the platform-specific setup. For example, the [`brightdigit/swift-build`](https://github.com/brightdigit/swift-build) action can target macOS, Linux, Windows, Apple devices (iOS, watchOS, tvOS, and visionOS), Android, and WebAssembly, selected with its `type` input. It uses `xcodebuild` for Apple platforms, [`compnerd/gha-setup-swift`](https://github.com/compnerd/gha-setup-swift) for Windows, and [`skiptools/swift-android-action`](https://github.com/skiptools/swift-android-action) for Android.
278+
279+
```yaml copy
280+
281+
{% data reusables.actions.actions-not-certified-by-github-comment %}
282+
283+
{% data reusables.actions.actions-use-sha-pinning-comment %}
284+
285+
name: Swift
286+
287+
on: [push]
288+
289+
jobs:
290+
test:
291+
runs-on: macos-latest
292+
steps:
293+
- uses: {% data reusables.actions.action-checkout %}
294+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
295+
with:
296+
scheme: MyPackage
297+
```
298+
299+
### Apple platforms
300+
301+
To target a specific Apple platform, set the `type` input (`ios`, `watchos`, `tvos`, `visionos`, or `macos`) and, for simulator-based platforms, the `deviceName` and `osVersion`. To check which simulators and OS versions are preinstalled on each macOS runner, see [AUTOTITLE](/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software). If the simulator runtime you request is not already installed on the runner, set `download-platform: true` so the action downloads it before testing.
302+
303+
```yaml copy
304+
305+
{% data reusables.actions.actions-not-certified-by-github-comment %}
306+
307+
{% data reusables.actions.actions-use-sha-pinning-comment %}
308+
309+
name: Swift
310+
311+
on: [push]
312+
313+
jobs:
314+
ios:
315+
runs-on: macos-latest
316+
steps:
317+
- uses: {% data reusables.actions.action-checkout %}
318+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
319+
with:
320+
scheme: MyPackage
321+
type: ios
322+
deviceName: iPhone 15
323+
osVersion: "17.5"
324+
download-platform: true
325+
```
326+
327+
### Linux
328+
329+
On Linux, run the action inside an official Swift container image.
330+
331+
```yaml copy
332+
333+
{% data reusables.actions.actions-not-certified-by-github-comment %}
334+
335+
{% data reusables.actions.actions-use-sha-pinning-comment %}
336+
337+
name: Swift
338+
339+
on: [push]
340+
341+
jobs:
342+
linux:
343+
runs-on: ubuntu-latest
344+
container: swift:6.3
345+
steps:
346+
- uses: {% data reusables.actions.action-checkout %}
347+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
348+
with:
349+
scheme: MyPackage
350+
```
351+
352+
### Windows
353+
354+
```yaml copy
355+
356+
{% data reusables.actions.actions-not-certified-by-github-comment %}
357+
358+
{% data reusables.actions.actions-use-sha-pinning-comment %}
359+
360+
name: Swift
361+
362+
on: [push]
363+
364+
jobs:
365+
windows:
366+
runs-on: windows-latest
367+
steps:
368+
- uses: {% data reusables.actions.action-checkout %}
369+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
370+
with:
371+
windows-swift-version: swift-6.3-release
372+
windows-swift-build: 6.3-RELEASE
373+
```
374+
375+
### Android
376+
377+
```yaml copy
378+
379+
{% data reusables.actions.actions-not-certified-by-github-comment %}
380+
381+
{% data reusables.actions.actions-use-sha-pinning-comment %}
382+
383+
name: Swift
384+
385+
on: [push]
386+
387+
jobs:
388+
android:
389+
runs-on: ubuntu-latest
390+
steps:
391+
- uses: {% data reusables.actions.action-checkout %}
392+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
393+
with:
394+
scheme: MyPackage
395+
type: android
396+
android-swift-version: "6.3"
397+
android-api-level: "28"
398+
```
399+
400+
### WebAssembly
401+
402+
WebAssembly builds require Swift 6.2.3 or later. Run the action inside a Swift container image and set `type` to `wasm`.
403+
404+
```yaml copy
405+
406+
{% data reusables.actions.actions-not-certified-by-github-comment %}
407+
408+
{% data reusables.actions.actions-use-sha-pinning-comment %}
409+
410+
name: Swift
411+
412+
on: [push]
413+
414+
jobs:
415+
wasm:
416+
runs-on: ubuntu-latest
417+
container: swift:6.3-jammy
418+
steps:
419+
- uses: {% data reusables.actions.action-checkout %}
420+
- uses: brightdigit/swift-build@b0a63a89ea85b47a8b43dff122e12856ad661f33 # v1.5.7
421+
with:
422+
scheme: MyPackage
423+
type: wasm
424+
```

0 commit comments

Comments
 (0)