|
| 1 | +# Copyright 2022-2023, axodotdev |
| 2 | +# SPDX-License-Identifier: MIT or Apache-2.0 |
| 3 | +# |
| 4 | +# CI that: |
| 5 | +# |
| 6 | +# * checks for a Git Tag that looks like a release |
| 7 | +# * builds artifacts with cargo-dist (archives, installers, hashes) |
| 8 | +# * uploads those artifacts to temporary workflow zip |
| 9 | +# * on success, uploads the artifacts to a Github Release™ |
| 10 | +# |
| 11 | +# Note that the Github Release™ will be created with a generated |
| 12 | +# title/body based on your changelogs. |
| 13 | +name: Release |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: write |
| 17 | + |
| 18 | +# This task will run whenever you push a git tag that looks like a version |
| 19 | +# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc. |
| 20 | +# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where |
| 21 | +# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION |
| 22 | +# must be a Cargo-style SemVer Version (must have at least major.minor.patch). |
| 23 | +# |
| 24 | +# If PACKAGE_NAME is specified, then the release will be for that |
| 25 | +# package (erroring out if it doesn't have the given version or isn't cargo-dist-able). |
| 26 | +# |
| 27 | +# If PACKAGE_NAME isn't specified, then the release will be for all |
| 28 | +# (cargo-dist-able) packages in the workspace with that version (this mode is |
| 29 | +# intended for workspaces with only one dist-able package, or with all dist-able |
| 30 | +# packages versioned/released in lockstep). |
| 31 | +# |
| 32 | +# If you push multiple tags at once, separate instances of this workflow will |
| 33 | +# spin up, creating an independent Github Release™ for each one. However Github |
| 34 | +# will hard limit this to 3 tags per commit, as it will assume more tags is a |
| 35 | +# mistake. |
| 36 | +# |
| 37 | +# If there's a prerelease-style suffix to the version, then the Github Release™ |
| 38 | +# will be marked as a prerelease. |
| 39 | +on: |
| 40 | + push: |
| 41 | + tags: |
| 42 | + - '**[0-9]+.[0-9]+.[0-9]+*' |
| 43 | + pull_request: |
| 44 | + |
| 45 | +jobs: |
| 46 | + # Run 'cargo dist plan' to determine what tasks we need to do |
| 47 | + plan: |
| 48 | + runs-on: ubuntu-latest |
| 49 | + outputs: |
| 50 | + val: ${{ steps.plan.outputs.manifest }} |
| 51 | + tag: ${{ !github.event.pull_request && github.ref_name || '' }} |
| 52 | + tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} |
| 53 | + publishing: ${{ !github.event.pull_request }} |
| 54 | + env: |
| 55 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + with: |
| 59 | + submodules: recursive |
| 60 | + - name: Install cargo-dist |
| 61 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.4.2/cargo-dist-installer.sh | sh" |
| 62 | + - id: plan |
| 63 | + run: | |
| 64 | + cargo dist plan ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} --output-format=json > dist-manifest.json |
| 65 | + echo "cargo dist plan ran successfully" |
| 66 | + cat dist-manifest.json |
| 67 | + echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 68 | + - name: "Upload dist-manifest.json" |
| 69 | + uses: actions/upload-artifact@v3 |
| 70 | + with: |
| 71 | + name: artifacts |
| 72 | + path: dist-manifest.json |
| 73 | + |
| 74 | + # Build and packages all the platform-specific things |
| 75 | + upload-local-artifacts: |
| 76 | + # Let the initial task tell us to not run (currently very blunt) |
| 77 | + needs: plan |
| 78 | + if: ${{ fromJson(needs.plan.outputs.val).releases != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }} |
| 79 | + strategy: |
| 80 | + fail-fast: false |
| 81 | + # Target platforms/runners are computed by cargo-dist in create-release. |
| 82 | + # Each member of the matrix has the following arguments: |
| 83 | + # |
| 84 | + # - runner: the github runner |
| 85 | + # - dist-args: cli flags to pass to cargo dist |
| 86 | + # - install-dist: expression to run to install cargo-dist on the runner |
| 87 | + # |
| 88 | + # Typically there will be: |
| 89 | + # - 1 "global" task that builds universal installers |
| 90 | + # - N "local" tasks that build each platform's binaries and platform-specific installers |
| 91 | + matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }} |
| 92 | + runs-on: ${{ matrix.runner }} |
| 93 | + env: |
| 94 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 95 | + BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json |
| 96 | + steps: |
| 97 | + - uses: actions/checkout@v4 |
| 98 | + with: |
| 99 | + submodules: recursive |
| 100 | + - uses: swatinem/rust-cache@v2 |
| 101 | + - name: Install cargo-dist |
| 102 | + run: ${{ matrix.install_dist }} |
| 103 | + - name: Install dependencies |
| 104 | + run: | |
| 105 | + ${{ matrix.packages_install }} |
| 106 | + - name: Build artifacts |
| 107 | + run: | |
| 108 | + # Actually do builds and make zips and whatnot |
| 109 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json |
| 110 | + echo "cargo dist ran successfully" |
| 111 | + - id: cargo-dist |
| 112 | + name: Post-build |
| 113 | + # We force bash here just because github makes it really hard to get values up |
| 114 | + # to "real" actions without writing to env-vars, and writing to env-vars has |
| 115 | + # inconsistent syntax between shell and powershell. |
| 116 | + shell: bash |
| 117 | + run: | |
| 118 | + # Parse out what we just built and upload it to the Github Release™ |
| 119 | + echo "paths<<EOF" >> "$GITHUB_OUTPUT" |
| 120 | + jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT" |
| 121 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 122 | +
|
| 123 | + cp dist-manifest.json "$BUILD_MANIFEST_NAME" |
| 124 | + - name: "Upload artifacts" |
| 125 | + uses: actions/upload-artifact@v3 |
| 126 | + with: |
| 127 | + name: artifacts |
| 128 | + path: | |
| 129 | + ${{ steps.cargo-dist.outputs.paths }} |
| 130 | + ${{ env.BUILD_MANIFEST_NAME }} |
| 131 | +
|
| 132 | + should-publish: |
| 133 | + needs: |
| 134 | + - plan |
| 135 | + - upload-local-artifacts |
| 136 | + if: ${{ needs.plan.outputs.publishing == 'true' }} |
| 137 | + runs-on: ubuntu-latest |
| 138 | + steps: |
| 139 | + - name: print tag |
| 140 | + run: echo "ok we're publishing!" |
| 141 | + |
| 142 | + # Create a Github Release with all the results once everything is done |
| 143 | + publish-release: |
| 144 | + needs: [plan, should-publish] |
| 145 | + runs-on: ubuntu-latest |
| 146 | + env: |
| 147 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 148 | + steps: |
| 149 | + - uses: actions/checkout@v4 |
| 150 | + with: |
| 151 | + submodules: recursive |
| 152 | + - name: "Download artifacts" |
| 153 | + uses: actions/download-artifact@v3 |
| 154 | + with: |
| 155 | + name: artifacts |
| 156 | + path: artifacts |
| 157 | + - name: Cleanup |
| 158 | + run: | |
| 159 | + # Remove the granular manifests |
| 160 | + rm artifacts/*-dist-manifest.json |
| 161 | + - name: Create Release |
| 162 | + uses: ncipollo/release-action@v1 |
| 163 | + with: |
| 164 | + tag: ${{ needs.plan.outputs.tag }} |
| 165 | + name: ${{ fromJson(needs.plan.outputs.val).announcement_title }} |
| 166 | + body: ${{ fromJson(needs.plan.outputs.val).announcement_github_body }} |
| 167 | + prerelease: ${{ fromJson(needs.plan.outputs.val).announcement_is_prerelease }} |
| 168 | + artifacts: "artifacts/*" |
0 commit comments