|
| 1 | +on: |
| 2 | + push: |
| 3 | + tags: |
| 4 | + - "starknet/v*.*.*" |
| 5 | + - "starknet-*/v*.*.*" |
| 6 | + |
| 7 | +name: "Release" |
| 8 | + |
| 9 | +jobs: |
| 10 | + crate-info: |
| 11 | + name: "Extract crate info" |
| 12 | + runs-on: "ubuntu-latest" |
| 13 | + outputs: |
| 14 | + crate: ${{ steps.derive.outputs.crate }} |
| 15 | + version: ${{ steps.derive.outputs.version }} |
| 16 | + |
| 17 | + steps: |
| 18 | + - id: "derive" |
| 19 | + name: "Derive crate info from Git tag" |
| 20 | + run: | |
| 21 | + FULL_REF="${{ github.ref }}" |
| 22 | + REGEX="^refs\/tags\/([a-z\\-]*)\/v(.*)$" |
| 23 | + [[ $FULL_REF =~ $REGEX ]]; |
| 24 | +
|
| 25 | + echo "crate=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT |
| 26 | + echo "version=${BASH_REMATCH[2]}" >> $GITHUB_OUTPUT |
| 27 | +
|
| 28 | + # Just in case we accidentally release something not on master. |
| 29 | + commit-branch-check: |
| 30 | + name: "Check commit branch" |
| 31 | + runs-on: "ubuntu-latest" |
| 32 | + needs: ["crate-info"] |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: "Checkout source code" |
| 36 | + uses: "actions/checkout@v3" |
| 37 | + with: |
| 38 | + fetch-depth: 0 |
| 39 | + |
| 40 | + - name: "Check if commit is on master" |
| 41 | + run: | |
| 42 | + COMMIT_HASH=$(git log -1 --format=%H ${{ github.ref }}) |
| 43 | + GREP_OUTPUT=$(git log origin/master --format=%H | grep "$COMMIT_HASH") |
| 44 | +
|
| 45 | + if [ -z "$GREP_OUTPUT" ]; then |
| 46 | + echo "Cannot release commits not on the master branch" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + crate-version-check: |
| 51 | + name: "Check crate version" |
| 52 | + runs-on: "ubuntu-latest" |
| 53 | + needs: ["crate-info"] |
| 54 | + |
| 55 | + steps: |
| 56 | + - name: "Checkout source code" |
| 57 | + uses: "actions/checkout@v3" |
| 58 | + |
| 59 | + - name: "Check against Cargo.toml" |
| 60 | + run: | |
| 61 | + if [ "starknet" != "${{ needs.crate-info.outputs.crate }}" ]; then |
| 62 | + cd ${{ needs.crate-info.outputs.crate }} |
| 63 | + fi |
| 64 | +
|
| 65 | + GREP_OUTPUT=$(cat Cargo.toml | grep "^version = \"${{ needs.crate-info.outputs.version }}\"$") |
| 66 | +
|
| 67 | + if [ -z "$GREP_OUTPUT" ]; then |
| 68 | + echo "Crate version mismatch" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + build: |
| 73 | + name: "Build for ${{ matrix.os }}" |
| 74 | + runs-on: "${{ matrix.os }}" |
| 75 | + needs: ["crate-info"] |
| 76 | + |
| 77 | + strategy: |
| 78 | + matrix: |
| 79 | + os: |
| 80 | + - "ubuntu-latest" |
| 81 | + - "windows-latest" |
| 82 | + - "macos-latest" |
| 83 | + |
| 84 | + steps: |
| 85 | + - name: "Checkout source code" |
| 86 | + uses: "actions/checkout@v3" |
| 87 | + |
| 88 | + - name: "Setup stable toolchain" |
| 89 | + uses: "actions-rs/toolchain@v1" |
| 90 | + with: |
| 91 | + toolchain: "stable" |
| 92 | + profile: "minimal" |
| 93 | + override: true |
| 94 | + |
| 95 | + - name: "Build crate" |
| 96 | + run: | |
| 97 | + cargo build --package ${{ needs.crate-info.outputs.crate }} --all-targets |
| 98 | +
|
| 99 | + crates-io-release: |
| 100 | + name: "Release to crates.io" |
| 101 | + runs-on: "ubuntu-latest" |
| 102 | + |
| 103 | + needs: |
| 104 | + - "crate-info" |
| 105 | + - "commit-branch-check" |
| 106 | + - "crate-version-check" |
| 107 | + - "build" |
| 108 | + |
| 109 | + steps: |
| 110 | + - name: "Checkout source code" |
| 111 | + uses: "actions/checkout@v3" |
| 112 | + |
| 113 | + - name: "Login to crates.io" |
| 114 | + run: | |
| 115 | + cargo login ${{ secrets.CRATES_IO_API_TOKEN }} |
| 116 | +
|
| 117 | + - name: "Public crate" |
| 118 | + run: | |
| 119 | + cargo publish --package ${{ needs.crate-info.outputs.crate }} |
0 commit comments