|
| 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 | +# temporary title/body if the Tag Body is empty. You should update |
| 13 | +# the Release notes before announcing it. |
| 14 | + |
| 15 | +name: Release |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: write |
| 19 | + # For GitHub attestations |
| 20 | + id-token: write |
| 21 | + attestations: write |
| 22 | + |
| 23 | +# This task will run whenever you push a tag to your repo |
| 24 | +# that looks like a version number. |
| 25 | +on: |
| 26 | + push: |
| 27 | + tags: |
| 28 | + - 'v[0-9]+.*' |
| 29 | + pull_request: |
| 30 | + |
| 31 | +env: |
| 32 | + CARGO_TERM_COLOR: always |
| 33 | + |
| 34 | +jobs: |
| 35 | + # Run 'cargo dist plan' to determine what tasks we need to do |
| 36 | + plan: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + outputs: |
| 39 | + val: ${{ steps.plan.outputs.manifest }} |
| 40 | + tag: ${{ !github.event.pull_request && github.ref_name || '' }} |
| 41 | + tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} |
| 42 | + publishing: ${{ !github.event.pull_request }} |
| 43 | + steps: |
| 44 | + - uses: actions/checkout@v4 |
| 45 | + with: |
| 46 | + submodules: recursive |
| 47 | + - name: Install cargo-dist |
| 48 | + uses: taiki-e/install-action@v2 |
| 49 | + with: |
| 50 | + tool: cargo-dist@0.27.0 |
| 51 | + - name: Cache cargo registry |
| 52 | + uses: actions/cache@v4 |
| 53 | + with: |
| 54 | + path: | |
| 55 | + ~/.cargo/registry/index/ |
| 56 | + ~/.cargo/registry/cache/ |
| 57 | + ~/.cargo/git/db/ |
| 58 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} |
| 59 | + - id: plan |
| 60 | + run: | |
| 61 | + cargo dist plan ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} --output-format=json > plan-dist-manifest.json |
| 62 | + echo "manifest=$(jq -c '.' plan-dist-manifest.json)" >> "$GITHUB_OUTPUT" |
| 63 | + - name: Upload dist-manifest.json |
| 64 | + uses: actions/upload-artifact@v4 |
| 65 | + with: |
| 66 | + name: artifacts-plan-dist-manifest |
| 67 | + path: plan-dist-manifest.json |
| 68 | + |
| 69 | + # Build and upload artifacts for each platform |
| 70 | + build-local-artifacts: |
| 71 | + name: build-local-artifacts (${{ matrix.targets }}) |
| 72 | + needs: plan |
| 73 | + 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') }} |
| 74 | + strategy: |
| 75 | + fail-fast: false |
| 76 | + matrix: |
| 77 | + include: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix.include }} |
| 78 | + runs-on: ${{ matrix.runner }} |
| 79 | + env: |
| 80 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json |
| 82 | + steps: |
| 83 | + - uses: actions/checkout@v4 |
| 84 | + with: |
| 85 | + submodules: recursive |
| 86 | + - name: Install cargo-dist |
| 87 | + uses: taiki-e/install-action@v2 |
| 88 | + with: |
| 89 | + tool: cargo-dist@0.27.0 |
| 90 | + - name: Install cross for Linux ARM builds |
| 91 | + if: ${{ contains(matrix.targets, 'aarch64-unknown-linux') }} |
| 92 | + run: cargo install cross --git https://github.com/cross-rs/cross |
| 93 | + - name: Cache cargo registry |
| 94 | + uses: actions/cache@v4 |
| 95 | + with: |
| 96 | + path: | |
| 97 | + ~/.cargo/registry/index/ |
| 98 | + ~/.cargo/registry/cache/ |
| 99 | + ~/.cargo/git/db/ |
| 100 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} |
| 101 | + - name: Build artifacts |
| 102 | + run: | |
| 103 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json |
| 104 | + echo "dist manifest: $(cat dist-manifest.json)" |
| 105 | + cp dist-manifest.json "$BUILD_MANIFEST_NAME" |
| 106 | + - name: Upload artifacts |
| 107 | + uses: actions/upload-artifact@v4 |
| 108 | + with: |
| 109 | + name: artifacts-build-local-${{ join(matrix.targets, '_') }} |
| 110 | + path: | |
| 111 | + target/distrib/* |
| 112 | +
|
| 113 | + # Build and upload global artifacts |
| 114 | + build-global-artifacts: |
| 115 | + needs: |
| 116 | + - plan |
| 117 | + - build-local-artifacts |
| 118 | + runs-on: ubuntu-latest |
| 119 | + env: |
| 120 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + steps: |
| 122 | + - uses: actions/checkout@v4 |
| 123 | + with: |
| 124 | + submodules: recursive |
| 125 | + - name: Install cargo-dist |
| 126 | + uses: taiki-e/install-action@v2 |
| 127 | + with: |
| 128 | + tool: cargo-dist@0.27.0 |
| 129 | + - name: Download local artifacts |
| 130 | + uses: actions/download-artifact@v4 |
| 131 | + with: |
| 132 | + pattern: artifacts-* |
| 133 | + path: target/distrib/ |
| 134 | + merge-multiple: true |
| 135 | + - name: Build global artifacts |
| 136 | + run: | |
| 137 | + cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json |
| 138 | + cat dist-manifest.json |
| 139 | + - name: Upload artifacts |
| 140 | + uses: actions/upload-artifact@v4 |
| 141 | + with: |
| 142 | + name: artifacts-build-global |
| 143 | + path: | |
| 144 | + target/distrib/* |
| 145 | +
|
| 146 | + # Generate GitHub attestations for build provenance |
| 147 | + generate-attestations: |
| 148 | + needs: |
| 149 | + - plan |
| 150 | + - build-local-artifacts |
| 151 | + - build-global-artifacts |
| 152 | + if: needs.plan.outputs.publishing == 'true' |
| 153 | + runs-on: ubuntu-latest |
| 154 | + env: |
| 155 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 156 | + steps: |
| 157 | + - name: Download all artifacts |
| 158 | + uses: actions/download-artifact@v4 |
| 159 | + with: |
| 160 | + pattern: artifacts-* |
| 161 | + path: target/distrib/ |
| 162 | + merge-multiple: true |
| 163 | + - name: Generate attestations |
| 164 | + uses: actions/attest-build-provenance@v2 |
| 165 | + with: |
| 166 | + subject-path: | |
| 167 | + target/distrib/*.tar.gz |
| 168 | + target/distrib/*.zip |
| 169 | + target/distrib/*.ps1 |
| 170 | + target/distrib/*.sh |
| 171 | +
|
| 172 | + # Publish to GitHub Releases |
| 173 | + publish: |
| 174 | + needs: |
| 175 | + - plan |
| 176 | + - build-local-artifacts |
| 177 | + - build-global-artifacts |
| 178 | + - generate-attestations |
| 179 | + if: needs.plan.outputs.publishing == 'true' |
| 180 | + runs-on: ubuntu-latest |
| 181 | + env: |
| 182 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 183 | + steps: |
| 184 | + - uses: actions/checkout@v4 |
| 185 | + - name: Download artifacts |
| 186 | + uses: actions/download-artifact@v4 |
| 187 | + with: |
| 188 | + pattern: artifacts-* |
| 189 | + path: target/distrib/ |
| 190 | + merge-multiple: true |
| 191 | + - name: Publish to GitHub Releases |
| 192 | + run: | |
| 193 | + gh release create ${{ needs.plan.outputs.tag }} target/distrib/* --title "${{ needs.plan.outputs.tag }}" --generate-notes |
0 commit comments