|
| 1 | +name: Publish release |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [release/**] |
| 5 | +jobs: |
| 6 | + check_version_bump: |
| 7 | + name: Check version bump |
| 8 | + environment: CRATES_IO |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Checkout |
| 12 | + uses: actions/checkout@v2 |
| 13 | + - name: Setup Rust toolchain stable-x86_64-unknown-linux-gnu for ubuntu-latest |
| 14 | + uses: actions-rs/toolchain@v1 |
| 15 | + with: |
| 16 | + toolchain: stable-x86_64-unknown-linux-gnu |
| 17 | + override: true |
| 18 | + - name: Read source branch version |
| 19 | + id: source_version |
| 20 | + run: echo "version=$(cargo read-manifest | jq -r .version)" >> $GITHUB_OUTPUT |
| 21 | + - name: Update cargo index |
| 22 | + run: cargo search |
| 23 | + - name: Read crates.io version |
| 24 | + id: crates_io_version |
| 25 | + run: echo "version=$(cargo search --limit 1 $(cargo read-manifest | jq -r .name) | grep -oP '(?<=")([0-9]+.[0-9]+.[0-9]+)(?=")')" >> $GITHUB_OUTPUT |
| 26 | + - name: Parse and compare versions |
| 27 | + run: | |
| 28 | + source_version="${{ steps.source_version.outputs.version }}" |
| 29 | + crates_io_version="${{ steps.crates_io_version.outputs.version }}" |
| 30 | + if [ "$(printf '%s\n' "$crates_io_version" "$source_version" | sort -V | head -n1)" != "$source_version" ]; then |
| 31 | + echo "Source branch version ($source_version) is higher than crates.io version ($crates_io_version)." |
| 32 | + else |
| 33 | + echo "Source branch version ($source_version) is not higher than crates.io version ($crates_io_version)." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + crates_io: |
| 37 | + name: Publish crates.io |
| 38 | + needs: check_version_bump |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - name: Checkout |
| 42 | + uses: actions/checkout@v2 |
| 43 | + - name: Setup Rust toolchain stable-x86_64-unknown-linux-gnu for ubuntu-latest |
| 44 | + uses: actions-rs/toolchain@v1 |
| 45 | + with: |
| 46 | + toolchain: stable-x86_64-unknown-linux-gnu |
| 47 | + override: true |
| 48 | + - name: Setup Cargo cache |
| 49 | + uses: Swatinem/rust-cache@v2 |
| 50 | + - name: Login to crates.io |
| 51 | + uses: actions-rs/cargo@v1 |
| 52 | + with: |
| 53 | + command: login |
| 54 | + args: ${{ secrets.CRATES_IO_TOKEN }} |
| 55 | + - name: Publish to crates.io |
| 56 | + uses: actions-rs/cargo@v1 |
| 57 | + with: |
| 58 | + command: publish |
| 59 | + github: |
| 60 | + name: Publish GitHub |
| 61 | + needs: crates_io |
| 62 | + permissions: |
| 63 | + contents: write |
| 64 | + environment: GITHUB_RELEASE |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@v2 |
| 69 | + - name: Setup Rust toolchain stable-x86_64-unknown-linux-gnu for ubuntu-latest |
| 70 | + uses: actions-rs/toolchain@v1 |
| 71 | + with: |
| 72 | + toolchain: stable-x86_64-unknown-linux-gnu |
| 73 | + override: true |
| 74 | + - name: Setup Cargo cache |
| 75 | + uses: Swatinem/rust-cache@v2 |
| 76 | + - name: Package |
| 77 | + uses: actions-rs/cargo@v1 |
| 78 | + with: |
| 79 | + command: package |
| 80 | + args: --all-features |
| 81 | + - name: Read crate name |
| 82 | + id: crate_name |
| 83 | + run: echo "crate_name=$(cargo read-manifest | jq -r .name)" >> $GITHUB_OUTPUT |
| 84 | + - name: Read version |
| 85 | + id: version |
| 86 | + run: echo "version=$(cargo read-manifest | jq -r .version)" >> $GITHUB_OUTPUT |
| 87 | + - name: Create release |
| 88 | + env: |
| 89 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Provided by GitHub Actions |
| 90 | + run: gh release create "${{ steps.version.outputs.version }}" --repo="$GITHUB_REPOSITORY" --title="Release ${{ steps.version.outputs.version }}" --generate-notes --latest "./target/package/${{ steps.crate_name.outputs.crate_name }}-${{ steps.version.outputs.version }}.crate" |
0 commit comments