|
| 1 | +# Copyright 2022-2024, 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 | + |
| 14 | +name: Release |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + |
| 19 | +# This task will run whenever you workflow_dispatch with a tag that looks like a version |
| 20 | +# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc. |
| 21 | +# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where |
| 22 | +# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION |
| 23 | +# must be a Cargo-style SemVer Version (must have at least major.minor.patch). |
| 24 | +# |
| 25 | +# If PACKAGE_NAME is specified, then the announcement will be for that |
| 26 | +# package (erroring out if it doesn't have the given version or isn't cargo-dist-able). |
| 27 | +# |
| 28 | +# If PACKAGE_NAME isn't specified, then the announcement will be for all |
| 29 | +# (cargo-dist-able) packages in the workspace with that version (this mode is |
| 30 | +# intended for workspaces with only one dist-able package, or with all dist-able |
| 31 | +# packages versioned/released in lockstep). |
| 32 | +# |
| 33 | +# If you push multiple tags at once, separate instances of this workflow will |
| 34 | +# spin up, creating an independent announcement for each one. However, GitHub |
| 35 | +# will hard limit this to 3 tags per commit, as it will assume more tags is a |
| 36 | +# mistake. |
| 37 | +# |
| 38 | +# If there's a prerelease-style suffix to the version, then the release(s) |
| 39 | +# will be marked as a prerelease. |
| 40 | +on: |
| 41 | + workflow_dispatch: |
| 42 | + inputs: |
| 43 | + tag: |
| 44 | + description: Release Tag |
| 45 | + required: true |
| 46 | + default: dry-run |
| 47 | + type: string |
| 48 | + |
| 49 | +jobs: |
| 50 | + # Run 'cargo dist plan' (or host) to determine what tasks we need to do |
| 51 | + plan: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + outputs: |
| 54 | + val: ${{ steps.plan.outputs.manifest }} |
| 55 | + tag: ${{ (inputs.tag != 'dry-run' && inputs.tag) || '' }} |
| 56 | + tag-flag: ${{ inputs.tag && inputs.tag != 'dry-run' && format('--tag={0}', inputs.tag) || '' }} |
| 57 | + publishing: ${{ inputs.tag && inputs.tag != 'dry-run' }} |
| 58 | + env: |
| 59 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + steps: |
| 61 | + - uses: actions/checkout@v4 |
| 62 | + with: |
| 63 | + submodules: recursive |
| 64 | + - name: Install cargo-dist |
| 65 | + # we specify bash to get pipefail; it guards against the `curl` command |
| 66 | + # failing. otherwise `sh` won't catch that `curl` returned non-0 |
| 67 | + shell: bash |
| 68 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh" |
| 69 | + # sure would be cool if github gave us proper conditionals... |
| 70 | + # so here's a doubly-nested ternary-via-truthiness to try to provide the best possible |
| 71 | + # functionality based on whether this is a pull_request, and whether it's from a fork. |
| 72 | + # (PRs run on the *source* but secrets are usually on the *target* -- that's *good* |
| 73 | + # but also really annoying to build CI around when it needs secrets to work right.) |
| 74 | + - id: plan |
| 75 | + run: | |
| 76 | + cargo dist ${{ (inputs.tag && inputs.tag != 'dry-run' && format('host --steps=create --tag={0}', inputs.tag)) || 'plan' }} --output-format=json > plan-dist-manifest.json |
| 77 | + echo "cargo dist ran successfully" |
| 78 | + cat plan-dist-manifest.json |
| 79 | + echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 80 | + - name: "Upload dist-manifest.json" |
| 81 | + uses: actions/upload-artifact@v4 |
| 82 | + with: |
| 83 | + name: artifacts-plan-dist-manifest |
| 84 | + path: plan-dist-manifest.json |
| 85 | + |
| 86 | + custom-build-binaries: |
| 87 | + needs: |
| 88 | + - plan |
| 89 | + if: ${{ needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload' || inputs.tag == 'dry-run' }} |
| 90 | + uses: ./.github/workflows/build-binaries.yml |
| 91 | + with: |
| 92 | + plan: ${{ needs.plan.outputs.val }} |
| 93 | + secrets: inherit |
| 94 | + |
| 95 | + custom-build-docker: |
| 96 | + needs: |
| 97 | + - plan |
| 98 | + if: ${{ needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload' || inputs.tag == 'dry-run' }} |
| 99 | + uses: ./.github/workflows/build-docker.yml |
| 100 | + with: |
| 101 | + plan: ${{ needs.plan.outputs.val }} |
| 102 | + secrets: inherit |
| 103 | + |
| 104 | + # Build and package all the platform-agnostic(ish) things |
| 105 | + build-global-artifacts: |
| 106 | + needs: |
| 107 | + - plan |
| 108 | + - custom-build-binaries |
| 109 | + - custom-build-docker |
| 110 | + runs-on: "ubuntu-20.04" |
| 111 | + env: |
| 112 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json |
| 114 | + steps: |
| 115 | + - uses: actions/checkout@v4 |
| 116 | + with: |
| 117 | + submodules: recursive |
| 118 | + - name: Install cargo-dist |
| 119 | + shell: bash |
| 120 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh" |
| 121 | + # Get all the local artifacts for the global tasks to use (for e.g. checksums) |
| 122 | + - name: Fetch local artifacts |
| 123 | + uses: actions/download-artifact@v4 |
| 124 | + with: |
| 125 | + pattern: artifacts-* |
| 126 | + path: target/distrib/ |
| 127 | + merge-multiple: true |
| 128 | + - id: cargo-dist |
| 129 | + shell: bash |
| 130 | + run: | |
| 131 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json |
| 132 | + echo "cargo dist ran successfully" |
| 133 | +
|
| 134 | + # Parse out what we just built and upload it to scratch storage |
| 135 | + echo "paths<<EOF" >> "$GITHUB_OUTPUT" |
| 136 | + jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT" |
| 137 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 138 | +
|
| 139 | + cp dist-manifest.json "$BUILD_MANIFEST_NAME" |
| 140 | + - name: "Upload artifacts" |
| 141 | + uses: actions/upload-artifact@v4 |
| 142 | + with: |
| 143 | + name: artifacts-build-global |
| 144 | + path: | |
| 145 | + ${{ steps.cargo-dist.outputs.paths }} |
| 146 | + ${{ env.BUILD_MANIFEST_NAME }} |
| 147 | + # Determines if we should publish/announce |
| 148 | + host: |
| 149 | + needs: |
| 150 | + - plan |
| 151 | + - custom-build-binaries |
| 152 | + - custom-build-docker |
| 153 | + - build-global-artifacts |
| 154 | + # Only run if we're "publishing", and only if local and global didn't fail (skipped is fine) |
| 155 | + if: ${{ always() && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.custom-build-binaries.result == 'skipped' || needs.custom-build-binaries.result == 'success') && (needs.custom-build-docker.result == 'skipped' || needs.custom-build-docker.result == 'success') }} |
| 156 | + env: |
| 157 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 158 | + runs-on: "ubuntu-20.04" |
| 159 | + outputs: |
| 160 | + val: ${{ steps.host.outputs.manifest }} |
| 161 | + steps: |
| 162 | + - uses: actions/checkout@v4 |
| 163 | + with: |
| 164 | + submodules: recursive |
| 165 | + - name: Install cargo-dist |
| 166 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh" |
| 167 | + # Fetch artifacts from scratch-storage |
| 168 | + - name: Fetch artifacts |
| 169 | + uses: actions/download-artifact@v4 |
| 170 | + with: |
| 171 | + pattern: artifacts-* |
| 172 | + path: target/distrib/ |
| 173 | + merge-multiple: true |
| 174 | + # This is a harmless no-op for GitHub Releases, hosting for that happens in "announce" |
| 175 | + - id: host |
| 176 | + shell: bash |
| 177 | + run: | |
| 178 | + cargo dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json |
| 179 | + echo "artifacts uploaded and released successfully" |
| 180 | + cat dist-manifest.json |
| 181 | + echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 182 | + - name: "Upload dist-manifest.json" |
| 183 | + uses: actions/upload-artifact@v4 |
| 184 | + with: |
| 185 | + # Overwrite the previous copy |
| 186 | + name: artifacts-dist-manifest |
| 187 | + path: dist-manifest.json |
| 188 | + |
| 189 | + custom-publish-pypi: |
| 190 | + needs: |
| 191 | + - plan |
| 192 | + - host |
| 193 | + if: ${{ !fromJson(needs.plan.outputs.val).announcement_is_prerelease || fromJson(needs.plan.outputs.val).publish_prereleases }} |
| 194 | + uses: ./.github/workflows/publish-pypi.yml |
| 195 | + with: |
| 196 | + plan: ${{ needs.plan.outputs.val }} |
| 197 | + secrets: inherit |
| 198 | + # publish jobs get escalated permissions |
| 199 | + permissions: |
| 200 | + id-token: write |
| 201 | + packages: write |
| 202 | + |
| 203 | + # Create a GitHub Release while uploading all files to it |
| 204 | + announce: |
| 205 | + needs: |
| 206 | + - plan |
| 207 | + - host |
| 208 | + - custom-publish-pypi |
| 209 | + # use "always() && ..." to allow us to wait for all publish jobs while |
| 210 | + # still allowing individual publish jobs to skip themselves (for prereleases). |
| 211 | + # "host" however must run to completion, no skipping allowed! |
| 212 | + if: ${{ always() && needs.host.result == 'success' && (needs.custom-publish-pypi.result == 'skipped' || needs.custom-publish-pypi.result == 'success') }} |
| 213 | + runs-on: "ubuntu-20.04" |
| 214 | + env: |
| 215 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 216 | + steps: |
| 217 | + - uses: actions/checkout@v4 |
| 218 | + with: |
| 219 | + submodules: recursive |
| 220 | + - name: "Download GitHub Artifacts" |
| 221 | + uses: actions/download-artifact@v4 |
| 222 | + with: |
| 223 | + pattern: artifacts-* |
| 224 | + path: artifacts |
| 225 | + merge-multiple: true |
| 226 | + - name: Cleanup |
| 227 | + run: | |
| 228 | + # Remove the granular manifests |
| 229 | + rm -f artifacts/*-dist-manifest.json |
| 230 | + - name: Create GitHub Release |
| 231 | + uses: ncipollo/release-action@v1 |
| 232 | + with: |
| 233 | + tag: ${{ needs.plan.outputs.tag }} |
| 234 | + name: ${{ fromJson(needs.host.outputs.val).announcement_title }} |
| 235 | + body: ${{ fromJson(needs.host.outputs.val).announcement_github_body }} |
| 236 | + prerelease: ${{ fromJson(needs.host.outputs.val).announcement_is_prerelease }} |
| 237 | + artifacts: "artifacts/*" |
| 238 | + |
| 239 | + custom-notify-dependents: |
| 240 | + needs: |
| 241 | + - plan |
| 242 | + - announce |
| 243 | + uses: ./.github/workflows/notify-dependents.yml |
| 244 | + with: |
| 245 | + plan: ${{ needs.plan.outputs.val }} |
| 246 | + secrets: inherit |
0 commit comments