|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + # --- 1. Read version from Cargo.toml; skip if this version was already released --- |
| 12 | + check-version: |
| 13 | + name: Check version |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + tag: ${{ steps.read.outputs.tag }} |
| 17 | + should_release: ${{ steps.check.outputs.should_release }} |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v6 |
| 20 | + |
| 21 | + - name: Read version |
| 22 | + id: read |
| 23 | + run: | |
| 24 | + VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/') |
| 25 | + echo "tag=${VERSION}" >> "$GITHUB_OUTPUT" |
| 26 | +
|
| 27 | + - name: Check if release already exists |
| 28 | + id: check |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 31 | + run: | |
| 32 | + if gh release view "${{ steps.read.outputs.tag }}" \ |
| 33 | + --repo "${{ github.repository }}" > /dev/null 2>&1; then |
| 34 | + echo "Release ${{ steps.read.outputs.tag }} already exists - skipping." |
| 35 | + echo "should_release=false" >> "$GITHUB_OUTPUT" |
| 36 | + else |
| 37 | + echo "should_release=true" >> "$GITHUB_OUTPUT" |
| 38 | + fi |
| 39 | +
|
| 40 | + # --- 2. Run tests (gate the release on green tests) ---------------------------- |
| 41 | + test: |
| 42 | + name: Test |
| 43 | + needs: check-version |
| 44 | + if: needs.check-version.outputs.should_release == 'true' |
| 45 | + runs-on: ubuntu-latest |
| 46 | + steps: |
| 47 | + - uses: actions/checkout@v6 |
| 48 | + - uses: dtolnay/rust-toolchain@stable |
| 49 | + - uses: Swatinem/rust-cache@v2 |
| 50 | + - run: cargo test |
| 51 | + |
| 52 | + # --- 3. Build for every target platform ---------------------------------------- |
| 53 | + build: |
| 54 | + name: Build · ${{ matrix.target }} |
| 55 | + needs: [ check-version, test ] |
| 56 | + runs-on: ${{ matrix.os }} |
| 57 | + strategy: |
| 58 | + fail-fast: false |
| 59 | + matrix: |
| 60 | + include: |
| 61 | + - { target: x86_64-unknown-linux-musl, os: ubuntu-latest, archive: tar.gz } |
| 62 | + - { target: aarch64-unknown-linux-musl, os: ubuntu-latest, archive: tar.gz } |
| 63 | + - { target: x86_64-apple-darwin, os: macos-latest, archive: tar.gz } |
| 64 | + - { target: aarch64-apple-darwin, os: macos-latest, archive: tar.gz } |
| 65 | + - { target: x86_64-pc-windows-msvc, os: windows-latest, archive: zip } |
| 66 | + |
| 67 | + steps: |
| 68 | + - uses: actions/checkout@v6 |
| 69 | + |
| 70 | + - uses: dtolnay/rust-toolchain@stable |
| 71 | + with: |
| 72 | + targets: ${{ matrix.target }} |
| 73 | + |
| 74 | + - uses: Swatinem/rust-cache@v2 |
| 75 | + with: |
| 76 | + key: ${{ matrix.target }} |
| 77 | + |
| 78 | + # Linux: cargo-zigbuild gives us static musl binaries for both x86_64 and |
| 79 | + # aarch64 without needing Docker or a cross-compiler toolchain. |
| 80 | + - name: Cache cargo-zigbuild |
| 81 | + if: runner.os == 'Linux' |
| 82 | + id: cache-zigbuild |
| 83 | + uses: actions/cache@v4 |
| 84 | + with: |
| 85 | + path: ~/.cargo/bin/cargo-zigbuild |
| 86 | + key: cargo-zigbuild-${{ runner.os }} |
| 87 | + |
| 88 | + - name: Install Zig |
| 89 | + if: runner.os == 'Linux' |
| 90 | + run: pip install ziglang |
| 91 | + |
| 92 | + - name: Install cargo-zigbuild |
| 93 | + if: runner.os == 'Linux' && steps.cache-zigbuild.outputs.cache-hit != 'true' |
| 94 | + run: cargo install cargo-zigbuild --locked |
| 95 | + |
| 96 | + - name: Build (Linux) |
| 97 | + if: runner.os == 'Linux' |
| 98 | + run: cargo zigbuild --release --target ${{ matrix.target }} |
| 99 | + |
| 100 | + # macOS and Windows use standard cargo - the runner already has the right toolchain. |
| 101 | + - name: Build (macOS / Windows) |
| 102 | + if: runner.os != 'Linux' |
| 103 | + run: cargo build --release --target ${{ matrix.target }} |
| 104 | + |
| 105 | + # -- Package --------------------------------------------------------------- |
| 106 | + - name: Package (Unix) |
| 107 | + if: runner.os != 'Windows' |
| 108 | + run: | |
| 109 | + tar -czf pycu-${{ matrix.target }}.tar.gz \ |
| 110 | + -C target/${{ matrix.target }}/release pycu |
| 111 | +
|
| 112 | + - name: Package (Windows) |
| 113 | + if: runner.os == 'Windows' |
| 114 | + shell: pwsh |
| 115 | + run: | |
| 116 | + Compress-Archive ` |
| 117 | + -Path "target\${{ matrix.target }}\release\pycu.exe" ` |
| 118 | + -DestinationPath "pycu-${{ matrix.target }}.zip" |
| 119 | +
|
| 120 | + - uses: actions/upload-artifact@v7 |
| 121 | + with: |
| 122 | + name: pycu-${{ matrix.target }} |
| 123 | + path: pycu-${{ matrix.target }}.${{ matrix.archive }} |
| 124 | + if-no-files-found: error |
| 125 | + |
| 126 | + # --- 4. Create the GitHub Release and attach all binaries ---------------------- |
| 127 | + release: |
| 128 | + name: Publish release |
| 129 | + needs: [ check-version, build ] |
| 130 | + runs-on: ubuntu-latest |
| 131 | + steps: |
| 132 | + - uses: actions/checkout@v6 |
| 133 | + |
| 134 | + - uses: actions/download-artifact@v7 |
| 135 | + with: |
| 136 | + path: artifacts |
| 137 | + merge-multiple: true |
| 138 | + |
| 139 | + - name: Generate SHA256 checksums |
| 140 | + run: | |
| 141 | + cd artifacts |
| 142 | + sha256sum * > checksums.sha256 |
| 143 | + cat checksums.sha256 |
| 144 | +
|
| 145 | + - name: Create GitHub Release |
| 146 | + env: |
| 147 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 148 | + run: | |
| 149 | + gh release create "${{ needs.check-version.outputs.tag }}" \ |
| 150 | + --title "${{ needs.check-version.outputs.tag }}" \ |
| 151 | + --generate-notes \ |
| 152 | + artifacts/* |
| 153 | +
|
| 154 | + # --- 5. Publish to crates.io ------------------------------------------------ |
| 155 | + publish-crate: |
| 156 | + name: Publish to crates.io |
| 157 | + needs: [ check-version, release ] |
| 158 | + runs-on: ubuntu-latest |
| 159 | + steps: |
| 160 | + - uses: actions/checkout@v6 |
| 161 | + - uses: dtolnay/rust-toolchain@stable |
| 162 | + - uses: Swatinem/rust-cache@v2 |
| 163 | + - name: Publish |
| 164 | + run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }} |
0 commit comments