diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml new file mode 100644 index 0000000..ec1b1b2 --- /dev/null +++ b/.github/dependabot.yaml @@ -0,0 +1,8 @@ +version: 2 +updates: +- package-ecosystem: github-actions + directory: "/" + schedule: + interval: "daily" + reviewers: + - slashmo diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..f7be9ec --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,24 @@ +name: CI +on: + push: + +jobs: + ci: + strategy: + fail-fast: false + matrix: + os: [ubuntu-18.04, ubuntu-20.04] + swift: [5.5, swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a] + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install Swift + uses: ./ + with: + version: ${{ matrix.swift }} + + - name: Print Swift Version + run: swift --version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..ab4362f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Moritz Lang + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..be16889 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# install-swift + +This action allows installing Swift toolchains, with support for both release and development versions. + +## Usage + +### Inputs + +- `version` - The Swift version you want to install. This may either be a release version like `5.5`, or a development +snapshot like `swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a`. + +### Example + +```yaml +- name: Install Swift + uses: slashmo/install-swift@v0.1.0 + with: + version: 5.5 +``` + +After adding this step, all following steps in this job will automatically use the newly installed Swift version: + +```yaml +- name: Run Tests + run: swift test # <-- uses Swift 5.5 +``` + +### Multiple Swift Versions + +In case you want to run your GitHub Actions workflow using different versions of Swift, define a +[GitHub Action's matrix](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) +to spawn multiple instances of the same job: + +```yaml +jobs: + test: + name: Run Tests + strategy: + matrix: + swift: [5.5, swift-DEVELOPMENT-SNAPSHOT-2021-11-12-a] + ubuntu: [ubuntu-18.04, ubuntu-20.04] + fail-fast: false + runs-on: ${{ matrix.ubuntu }} + steps: + - name: Install Swift + uses: slashmo/install-swift@v0.1.0 + with: + version: ${{ matrix.swift }} + - name: Checkout + uses: actions/checkout@v2 + - name: Run Tests + run: swift test +``` + +The action will automatically detect the Ubuntu version and install the correct toolchain. + +### Caching + +`install-swift` automatically caches toolchains based on the [version input](#inputs) and the detected Ubuntu version. diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..7d847a8 --- /dev/null +++ b/action.yaml @@ -0,0 +1,42 @@ +name: 'Install Swift' +description: 'Install the given Swift version.' + +inputs: + version: + description: 'The Swift version to install.' + required: true + +runs: + using: "composite" + steps: + - name: Determine Ubuntu Version + run: | + UBUNTU_VERSION=$(lsb_release -rs) + + if [ $UBUNTU_VERSION != "18.04" ] && [ $UBUNTU_VERSION != "20.04" ]; then + echo "No Swift Toolchain available for Ubuntu version '$UBUNTU_VERSION'." + echo "Visit https://swift.org/download for more information." + exit 1; + fi + + echo "UBUNTU_VERSION=$UBUNTU_VERSION" >> $GITHUB_ENV + shell: bash + + - name: Cache Swift Toolchain + id: swift-toolchain-cache + uses: actions/cache@v2 + with: + path: /usr/share/swift-toolchain + key: ubuntu-${{ env.UBUNTU_VERSION }}-${{ inputs.version }} + + - name: Install Swift Toolchain + run: SWIFT_VERSION=${{ inputs.version }} $GITHUB_ACTION_PATH/install-swift.sh + shell: bash + + - name: Print Swift Version + run: swift --version + shell: bash + +branding: + icon: 'briefcase' + color: 'orange' diff --git a/install-swift.sh b/install-swift.sh new file mode 100755 index 0000000..6ef4401 --- /dev/null +++ b/install-swift.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +if [[ $SWIFT_VERSION == swift-DEVELOPMENT-SNAPSHOT* ]]; then + TOOLCHAIN_BASE_URL="https://download.swift.org/development/ubuntu${UBUNTU_VERSION//[.]/}/$SWIFT_VERSION" + TOOLCHAIN_PATH="$SWIFT_VERSION-ubuntu$UBUNTU_VERSION" +else + TOOLCHAIN_BASE_URL="https://download.swift.org/swift-$SWIFT_VERSION-release/ubuntu${UBUNTU_VERSION//[.]/}/swift-$SWIFT_VERSION-RELEASE" + TOOLCHAIN_PATH="swift-$SWIFT_VERSION-RELEASE-ubuntu$UBUNTU_VERSION" +fi + +TOOLCHAIN_TAR="$TOOLCHAIN_PATH.tar.gz" +TOOLCHAIN_SIG="$TOOLCHAIN_TAR.sig" +TOOLCHAIN_TAR_URL="$TOOLCHAIN_BASE_URL/$TOOLCHAIN_TAR" +TOOLCHAIN_SIG_URL="$TOOLCHAIN_BASE_URL/$TOOLCHAIN_SIG" + +echo "Installing system dependencies 📦" +sudo apt-get install \ +binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 \ +libz3-dev pkg-config tzdata uuid-dev zlib1g-dev + +if [ -d "/usr/share/swift-toolchain" ]; then + echo "Toolchain already exists, skipping download ✅" +else + echo "Reading toolchain version from .swift-version 📄" + echo "Detected Swift toolchain version '$(cat .swift-version)' 📄" + + echo "Downloading Swift toolchain ☁️" + wget $TOOLCHAIN_TAR_URL + + echo "Verifying Swift toolchain 🔑" + wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import - + wget $TOOLCHAIN_SIG_URL + gpg --verify $TOOLCHAIN_SIG + + echo "Installing Swift toolchain 💻" + tar xzf $TOOLCHAIN_TAR + + mv $TOOLCHAIN_PATH /usr/share/swift-toolchain + echo "Successfully installed Swift toolchain 🎉" +fi + +export PATH=/usr/share/swift-toolchain/usr/bin:${PATH} +echo "PATH=$PATH" >> $GITHUB_ENV