|
| 1 | +# Copyright 2022-2024, axodotdev |
| 2 | +# SPDX-License-Identifier: MIT or Apache-2.0 |
| 3 | +# |
1 | 4 | # CI that: |
2 | 5 | # |
3 | 6 | # * checks for a Git Tag that looks like a release |
4 | | -# * creates a Github Release™ and fills in its text |
5 | | -# * builds artifacts with cargo-dist (executable-zips, installers) |
6 | | -# * uploads those artifacts to the Github 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 |
7 | 10 | # |
8 | | -# Note that the Github Release™ will be created before the artifacts, |
9 | | -# so there will be a few minutes where the release has no artifacts |
10 | | -# and then they will slowly trickle in, possibly failing. To make |
11 | | -# this more pleasant we mark the release as a "draft" until all |
12 | | -# artifacts have been successfully uploaded. This allows you to |
13 | | -# choose what to do with partial successes and avoids spamming |
14 | | -# anyone with notifications before the release is actually ready. |
| 11 | +# Note that the GitHub Release will be created with a generated |
| 12 | +# title/body based on your changelogs. |
| 13 | + |
15 | 14 | name: Release |
16 | 15 |
|
17 | 16 | permissions: |
18 | 17 | contents: write |
19 | 18 |
|
20 | 19 | # This task will run whenever you push a git tag that looks like a version |
21 | | -# like "v1", "v1.2.0", "v0.1.0-prerelease01", "my-app-v1.0.0", etc. |
22 | | -# The version will be roughly parsed as ({PACKAGE_NAME}-)?v{VERSION}, where |
| 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 |
23 | 22 | # PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION |
24 | | -# must be a Cargo-style SemVer Version. |
| 23 | +# must be a Cargo-style SemVer Version (must have at least major.minor.patch). |
25 | 24 | # |
26 | | -# If PACKAGE_NAME is specified, then we will create a Github Release™ for that |
| 25 | +# If PACKAGE_NAME is specified, then the announcement will be for that |
27 | 26 | # package (erroring out if it doesn't have the given version or isn't cargo-dist-able). |
28 | 27 | # |
29 | | -# If PACKAGE_NAME isn't specified, then we will create a Github Release™ for all |
30 | | -# (cargo-dist-able) packages in the workspace with that version (this is mode is |
| 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 |
31 | 30 | # intended for workspaces with only one dist-able package, or with all dist-able |
32 | 31 | # packages versioned/released in lockstep). |
33 | 32 | # |
34 | 33 | # If you push multiple tags at once, separate instances of this workflow will |
35 | | -# spin up, creating an independent Github Release™ for each one. |
| 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. |
36 | 37 | # |
37 | | -# If there's a prerelease-style suffix to the version then the Github Release™ |
| 38 | +# If there's a prerelease-style suffix to the version, then the release(s) |
38 | 39 | # will be marked as a prerelease. |
39 | 40 | on: |
40 | 41 | push: |
41 | 42 | tags: |
42 | | - - '*-?v[0-9]+*' |
| 43 | + - '**[0-9]+.[0-9]+.[0-9]+*' |
| 44 | + pull_request: |
43 | 45 |
|
44 | 46 | jobs: |
45 | | - # Create the Github Release™ so the packages have something to be uploaded to |
46 | | - create-release: |
| 47 | + # Run 'cargo dist plan' (or host) to determine what tasks we need to do |
| 48 | + plan: |
47 | 49 | runs-on: ubuntu-latest |
48 | 50 | outputs: |
49 | | - has-releases: ${{ steps.create-release.outputs.has-releases }} |
| 51 | + val: ${{ steps.plan.outputs.manifest }} |
| 52 | + tag: ${{ !github.event.pull_request && github.ref_name || '' }} |
| 53 | + tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} |
| 54 | + publishing: ${{ !github.event.pull_request }} |
50 | 55 | env: |
51 | 56 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
52 | 57 | steps: |
53 | | - - uses: actions/checkout@v3 |
54 | | - - name: Install Rust |
55 | | - run: rustup update 1.67.1 --no-self-update && rustup default 1.67.1 |
| 58 | + - uses: actions/checkout@v4 |
| 59 | + with: |
| 60 | + submodules: recursive |
56 | 61 | - name: Install cargo-dist |
57 | | - run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh |
58 | | - - id: create-release |
| 62 | + # we specify bash to get pipefail; it guards against the `curl` command |
| 63 | + # failing. otherwise `sh` won't catch that `curl` returned non-0 |
| 64 | + shell: bash |
| 65 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.13.1/cargo-dist-installer.sh | sh" |
| 66 | + # sure would be cool if github gave us proper conditionals... |
| 67 | + # so here's a doubly-nested ternary-via-truthiness to try to provide the best possible |
| 68 | + # functionality based on whether this is a pull_request, and whether it's from a fork. |
| 69 | + # (PRs run on the *source* but secrets are usually on the *target* -- that's *good* |
| 70 | + # but also really annoying to build CI around when it needs secrets to work right.) |
| 71 | + - id: plan |
59 | 72 | run: | |
60 | | - cargo dist plan --tag=${{ github.ref_name }} --output-format=json > dist-manifest.json |
61 | | - echo "dist plan ran successfully" |
62 | | - cat dist-manifest.json |
| 73 | + cargo dist ${{ (!github.event.pull_request && format('host --steps=create --tag={0}', github.ref_name)) || 'plan' }} --output-format=json > plan-dist-manifest.json |
| 74 | + echo "cargo dist ran successfully" |
| 75 | + cat plan-dist-manifest.json |
| 76 | + echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 77 | + - name: "Upload dist-manifest.json" |
| 78 | + uses: actions/upload-artifact@v4 |
| 79 | + with: |
| 80 | + name: artifacts-plan-dist-manifest |
| 81 | + path: plan-dist-manifest.json |
63 | 82 |
|
64 | | - # Create the Github Release™ based on what cargo-dist thinks it should be |
65 | | - ANNOUNCEMENT_TITLE=$(jq --raw-output ".announcement_title" dist-manifest.json) |
66 | | - IS_PRERELEASE=$(jq --raw-output ".announcement_is_prerelease" dist-manifest.json) |
67 | | - jq --raw-output ".announcement_github_body" dist-manifest.json > new_dist_announcement.md |
68 | | - gh release create ${{ github.ref_name }} --draft --prerelease="$IS_PRERELEASE" --title="$ANNOUNCEMENT_TITLE" --notes-file=new_dist_announcement.md |
69 | | - echo "created announcement!" |
| 83 | + # Build and packages all the platform-specific things |
| 84 | + build-local-artifacts: |
| 85 | + name: build-local-artifacts (${{ join(matrix.targets, ', ') }}) |
| 86 | + # Let the initial task tell us to not run (currently very blunt) |
| 87 | + needs: |
| 88 | + - plan |
| 89 | + if: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix.include != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }} |
| 90 | + strategy: |
| 91 | + fail-fast: false |
| 92 | + # Target platforms/runners are computed by cargo-dist in create-release. |
| 93 | + # Each member of the matrix has the following arguments: |
| 94 | + # |
| 95 | + # - runner: the github runner |
| 96 | + # - dist-args: cli flags to pass to cargo dist |
| 97 | + # - install-dist: expression to run to install cargo-dist on the runner |
| 98 | + # |
| 99 | + # Typically there will be: |
| 100 | + # - 1 "global" task that builds universal installers |
| 101 | + # - N "local" tasks that build each platform's binaries and platform-specific installers |
| 102 | + matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }} |
| 103 | + runs-on: ${{ matrix.runner }} |
| 104 | + env: |
| 105 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 106 | + BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v4 |
| 109 | + with: |
| 110 | + submodules: recursive |
| 111 | + - uses: swatinem/rust-cache@v2 |
| 112 | + with: |
| 113 | + key: ${{ join(matrix.targets, '-') }} |
| 114 | + - name: Install cargo-dist |
| 115 | + run: ${{ matrix.install_dist }} |
| 116 | + # Get the dist-manifest |
| 117 | + - name: Fetch local artifacts |
| 118 | + uses: actions/download-artifact@v4 |
| 119 | + with: |
| 120 | + pattern: artifacts-* |
| 121 | + path: target/distrib/ |
| 122 | + merge-multiple: true |
| 123 | + - name: Install dependencies |
| 124 | + run: | |
| 125 | + ${{ matrix.packages_install }} |
| 126 | + - name: Build artifacts |
| 127 | + run: | |
| 128 | + # Actually do builds and make zips and whatnot |
| 129 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json |
| 130 | + echo "cargo dist ran successfully" |
| 131 | + - id: cargo-dist |
| 132 | + name: Post-build |
| 133 | + # We force bash here just because github makes it really hard to get values up |
| 134 | + # to "real" actions without writing to env-vars, and writing to env-vars has |
| 135 | + # inconsistent syntax between shell and powershell. |
| 136 | + shell: bash |
| 137 | + run: | |
| 138 | + # Parse out what we just built and upload it to scratch storage |
| 139 | + echo "paths<<EOF" >> "$GITHUB_OUTPUT" |
| 140 | + jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT" |
| 141 | + echo "EOF" >> "$GITHUB_OUTPUT" |
70 | 142 |
|
71 | | - # Upload the manifest to the Github Release™ |
72 | | - gh release upload ${{ github.ref_name }} dist-manifest.json |
73 | | - echo "uploaded manifest!" |
| 143 | + cp dist-manifest.json "$BUILD_MANIFEST_NAME" |
| 144 | + - name: "Upload artifacts" |
| 145 | + uses: actions/upload-artifact@v4 |
| 146 | + with: |
| 147 | + name: artifacts-build-local-${{ join(matrix.targets, '_') }} |
| 148 | + path: | |
| 149 | + ${{ steps.cargo-dist.outputs.paths }} |
| 150 | + ${{ env.BUILD_MANIFEST_NAME }} |
74 | 151 |
|
75 | | - # Disable all the upload-artifacts tasks if we have no actual releases |
76 | | - HAS_RELEASES=$(jq --raw-output ".releases != null" dist-manifest.json) |
77 | | - echo "has-releases=$HAS_RELEASES" >> "$GITHUB_OUTPUT" |
| 152 | + # Build and package all the platform-agnostic(ish) things |
| 153 | + build-global-artifacts: |
| 154 | + needs: |
| 155 | + - plan |
| 156 | + - build-local-artifacts |
| 157 | + runs-on: "ubuntu-20.04" |
| 158 | + env: |
| 159 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 160 | + BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json |
| 161 | + steps: |
| 162 | + - uses: actions/checkout@v4 |
| 163 | + with: |
| 164 | + submodules: recursive |
| 165 | + - name: Install cargo-dist |
| 166 | + shell: bash |
| 167 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.13.1/cargo-dist-installer.sh | sh" |
| 168 | + # Get all the local artifacts for the global tasks to use (for e.g. checksums) |
| 169 | + - name: Fetch local artifacts |
| 170 | + uses: actions/download-artifact@v4 |
| 171 | + with: |
| 172 | + pattern: artifacts-* |
| 173 | + path: target/distrib/ |
| 174 | + merge-multiple: true |
| 175 | + - id: cargo-dist |
| 176 | + shell: bash |
| 177 | + run: | |
| 178 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json |
| 179 | + echo "cargo dist ran successfully" |
78 | 180 |
|
79 | | - # Build and packages all the things |
80 | | - upload-artifacts: |
81 | | - # Let the initial task tell us to not run (currently very blunt) |
82 | | - needs: create-release |
83 | | - if: ${{ needs.create-release.outputs.has-releases == 'true' }} |
84 | | - strategy: |
85 | | - matrix: |
86 | | - # For these target platforms |
87 | | - include: |
88 | | - - os: ubuntu-20.04 |
89 | | - dist-args: --artifacts=global |
90 | | - install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh |
91 | | - - os: macos-11 |
92 | | - dist-args: --artifacts=local --target=aarch64-apple-darwin --target=x86_64-apple-darwin |
93 | | - install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh |
94 | | - - os: ubuntu-20.04 |
95 | | - dist-args: --artifacts=local --target=x86_64-unknown-linux-gnu |
96 | | - install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh |
97 | | - - os: windows-2019 |
98 | | - dist-args: --artifacts=local --target=x86_64-pc-windows-msvc |
99 | | - install-dist: irm https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.ps1 | iex |
| 181 | + # Parse out what we just built and upload it to scratch storage |
| 182 | + echo "paths<<EOF" >> "$GITHUB_OUTPUT" |
| 183 | + jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT" |
| 184 | + echo "EOF" >> "$GITHUB_OUTPUT" |
100 | 185 |
|
101 | | - runs-on: ${{ matrix.os }} |
| 186 | + cp dist-manifest.json "$BUILD_MANIFEST_NAME" |
| 187 | + - name: "Upload artifacts" |
| 188 | + uses: actions/upload-artifact@v4 |
| 189 | + with: |
| 190 | + name: artifacts-build-global |
| 191 | + path: | |
| 192 | + ${{ steps.cargo-dist.outputs.paths }} |
| 193 | + ${{ env.BUILD_MANIFEST_NAME }} |
| 194 | + # Determines if we should publish/announce |
| 195 | + host: |
| 196 | + needs: |
| 197 | + - plan |
| 198 | + - build-local-artifacts |
| 199 | + - build-global-artifacts |
| 200 | + # Only run if we're "publishing", and only if local and global didn't fail (skipped is fine) |
| 201 | + if: ${{ always() && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.build-local-artifacts.result == 'skipped' || needs.build-local-artifacts.result == 'success') }} |
102 | 202 | env: |
103 | 203 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 204 | + runs-on: "ubuntu-20.04" |
| 205 | + outputs: |
| 206 | + val: ${{ steps.host.outputs.manifest }} |
104 | 207 | steps: |
105 | | - - uses: actions/checkout@v3 |
106 | | - - name: Install Rust |
107 | | - run: rustup update 1.67.1 --no-self-update && rustup default 1.67.1 |
| 208 | + - uses: actions/checkout@v4 |
| 209 | + with: |
| 210 | + submodules: recursive |
108 | 211 | - name: Install cargo-dist |
109 | | - run: ${{ matrix.install-dist }} |
110 | | - - name: Run cargo-dist |
111 | | - # This logic is a bit janky because it's trying to be a polyglot between |
112 | | - # powershell and bash since this will run on windows, macos, and linux! |
113 | | - # The two platforms don't agree on how to talk about env vars but they |
114 | | - # do agree on 'cat' and '$()' so we use that to marshal values between commands. |
| 212 | + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.13.1/cargo-dist-installer.sh | sh" |
| 213 | + # Fetch artifacts from scratch-storage |
| 214 | + - name: Fetch artifacts |
| 215 | + uses: actions/download-artifact@v4 |
| 216 | + with: |
| 217 | + pattern: artifacts-* |
| 218 | + path: target/distrib/ |
| 219 | + merge-multiple: true |
| 220 | + # This is a harmless no-op for GitHub Releases, hosting for that happens in "announce" |
| 221 | + - id: host |
| 222 | + shell: bash |
115 | 223 | run: | |
116 | | - # Actually do builds and make zips and whatnot |
117 | | - cargo dist build --tag=${{ github.ref_name }} --output-format=json ${{ matrix.dist-args }} > dist-manifest.json |
118 | | - echo "dist ran successfully" |
| 224 | + cargo dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json |
| 225 | + echo "artifacts uploaded and released successfully" |
119 | 226 | cat dist-manifest.json |
| 227 | + echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 228 | + - name: "Upload dist-manifest.json" |
| 229 | + uses: actions/upload-artifact@v4 |
| 230 | + with: |
| 231 | + # Overwrite the previous copy |
| 232 | + name: artifacts-dist-manifest |
| 233 | + path: dist-manifest.json |
120 | 234 |
|
121 | | - # Parse out what we just built and upload it to the Github Release™ |
122 | | - jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json > uploads.txt |
123 | | - echo "uploading..." |
124 | | - cat uploads.txt |
125 | | - gh release upload ${{ github.ref_name }} $(cat uploads.txt) |
126 | | - echo "uploaded!" |
127 | | -
|
128 | | - # Mark the Github Release™ as a non-draft now that everything has succeeded! |
129 | | - publish-release: |
130 | | - # Only run after all the other tasks, but it's ok if upload-artifacts was skipped |
131 | | - needs: [create-release, upload-artifacts] |
132 | | - if: ${{ always() && needs.create-release.result == 'success' && (needs.upload-artifacts.result == 'skipped' || needs.upload-artifacts.result == 'success') }} |
133 | | - runs-on: ubuntu-latest |
| 235 | + # Create a GitHub Release while uploading all files to it |
| 236 | + announce: |
| 237 | + needs: |
| 238 | + - plan |
| 239 | + - host |
| 240 | + # use "always() && ..." to allow us to wait for all publish jobs while |
| 241 | + # still allowing individual publish jobs to skip themselves (for prereleases). |
| 242 | + # "host" however must run to completion, no skipping allowed! |
| 243 | + if: ${{ always() && needs.host.result == 'success' }} |
| 244 | + runs-on: "ubuntu-20.04" |
134 | 245 | env: |
135 | 246 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
136 | 247 | steps: |
137 | | - - uses: actions/checkout@v3 |
138 | | - - name: mark release as non-draft |
| 248 | + - uses: actions/checkout@v4 |
| 249 | + with: |
| 250 | + submodules: recursive |
| 251 | + - name: "Download GitHub Artifacts" |
| 252 | + uses: actions/download-artifact@v4 |
| 253 | + with: |
| 254 | + pattern: artifacts-* |
| 255 | + path: artifacts |
| 256 | + merge-multiple: true |
| 257 | + - name: Cleanup |
139 | 258 | run: | |
140 | | - gh release edit ${{ github.ref_name }} --draft=false |
| 259 | + # Remove the granular manifests |
| 260 | + rm -f artifacts/*-dist-manifest.json |
| 261 | + - name: Create GitHub Release |
| 262 | + uses: ncipollo/release-action@v1 |
| 263 | + with: |
| 264 | + tag: ${{ needs.plan.outputs.tag }} |
| 265 | + name: ${{ fromJson(needs.host.outputs.val).announcement_title }} |
| 266 | + body: ${{ fromJson(needs.host.outputs.val).announcement_github_body }} |
| 267 | + prerelease: ${{ fromJson(needs.host.outputs.val).announcement_is_prerelease }} |
| 268 | + artifacts: "artifacts/*" |
0 commit comments