John Siu Blog

Tech - Business Tool, Personal Toys

Github Actions For Swift

☰ Table of Content

Using standard Github actions only.

Setup

Create following in swift repo to use Github Action:

1
2
mkdir .github/workflows
touch .github/workflows/swift.yml

Following is the dir structure:

1
2
3
.github
└── workflows
    └── swift.yml

swift.yml is the workflow/actions configuration.

Swift.yml

The swift.yml contain 3 main parts: name, on and jobs.

Name

name is the name of this workflow:

1
2
name: Swift

On

on is the trigger condition of this workflow. In this case, the workflow will be triggered whenever a new tag is pushed to Github.

1
2
3
4
on:
  push:
    tags:
      - "*"

Jobs

jobs consists of one build, which in turn contain runs-on and the steps section.

1
2
3
jobs:
  build:
    runs-on: macos-latest

Since we are building for MacOS, macos-latest is used here.

Steps

steps contain multiple steps, each with their own name.

Checkout

Checkout is standard across all workflow. This tell github compiling environment to checkout the repository.

1
2
      - name: Checkout
        uses: actions/checkout@v2
Build

Build is the command line use to build the binary.

1
2
      - name: Build
        run: swift build -v -c release
Zip

Zip step create the zip file and also output information for later steps.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
      - name: Zip
        id: zip
        run: |
          PRG=itpl
          TAG=${GITHUB_REF/refs\/tags\//}
          ZIP_NAME=${PRG}-${TAG}.zip
          ZIP_PATH=./${ZIP_NAME}
          echo ${TAG}
          echo ${ZIP_NAME}
          echo ${ZIP_PATH}
          zip -j ${ZIP_NAME} .build/release/${PRG}
          echo "::set-output name=name::${ZIP_NAME}"
          echo "::set-output name=path::${ZIP_PATH}"          
LineDescription
4PRG contains name of the output binary
12,13These are output/parameters to be used by later steps
Release - Create

Release - Create will create a release entry for the repository. This is pretty standard and no modification needed in most cases.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
      - name: Release - Create
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
Release - Upload

Release - Upload Asset will upload the zip file into the release entry. Again, this is pretty standard and mo modification needed in most cases.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
      - name: Release - Upload Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{ steps.zip.outputs.path }}
          asset_name: ${{ steps.zip.outputs.name }}
          asset_content_type: application/zip
LineDescription
8,9Assign parameter with output from the zip step.

Complete Listing

Following is the complete listing:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
name: Swift

on:
  push:
    tags:
      - "*"

jobs:
  build:
    runs-on: macos-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Build
        run: swift build -v -c release

      - name: Zip
        id: zip
        run: |
          PRG=itpl
          TAG=${GITHUB_REF/refs\/tags\//}
          ZIP_NAME=${PRG}-${TAG}.zip
          ZIP_PATH=./${ZIP_NAME}
          echo ${TAG}
          echo ${ZIP_NAME}
          echo ${ZIP_PATH}
          zip -j ${ZIP_NAME} .build/release/${PRG}
          echo "::set-output name=name::${ZIP_NAME}"
          echo "::set-output name=path::${ZIP_PATH}"          

      - name: Release - Create
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false

      - name: Release - Upload Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{ steps.zip.outputs.path }}
          asset_name: ${{ steps.zip.outputs.name }}
          asset_content_type: application/zip

itpl is my iTunes playlist command line utility.

John Siu

Update: 2020-10-15
comments powered by Disqus