|
1 | | -name: Publish on crates.io |
| 1 | +name: Publish to crates.io |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
5 | | - types: |
6 | | - - closed |
| 4 | + push: |
7 | 5 | branches: |
8 | 6 | - main |
| 7 | + paths: |
| 8 | + - 'Cargo.toml' |
9 | 9 |
|
10 | 10 | jobs: |
11 | | - publish: |
12 | | - if: github.event.pull_request.merged == true |
| 11 | + check-version: |
13 | 12 | runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + should_publish: ${{ steps.compare_versions.outputs.should_publish }} |
14 | 15 |
|
15 | 16 | steps: |
16 | | - - name: Checkout repository |
17 | | - uses: actions/checkout@v2 |
| 17 | + - uses: actions/checkout@v4 |
18 | 18 | with: |
19 | | - fetch-depth: 0 |
20 | | - ref: main |
| 19 | + fetch-depth: 0 # Fetch all history for comparing versions |
| 20 | + |
| 21 | + - name: Compare versions |
| 22 | + id: compare_versions |
| 23 | + run: | |
| 24 | + # Get the current version from Cargo.toml |
| 25 | + CURRENT_VERSION=$(grep '^version = ' Cargo.toml | cut -d '"' -f 2) |
| 26 | + echo "Current version: $CURRENT_VERSION" |
| 27 | +
|
| 28 | + # Get the previous version from the last commit |
| 29 | + git checkout HEAD~1 |
| 30 | + PREVIOUS_VERSION=$(grep '^version = ' Cargo.toml | cut -d '"' -f 2) |
| 31 | + echo "Previous version: $PREVIOUS_VERSION" |
| 32 | +
|
| 33 | + # Compare versions |
| 34 | + if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then |
| 35 | + echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION" |
| 36 | + echo "should_publish=true" >> "$GITHUB_OUTPUT" |
| 37 | + else |
| 38 | + echo "Version unchanged" |
| 39 | + echo "should_publish=false" >> "$GITHUB_OUTPUT" |
| 40 | + fi |
| 41 | +
|
| 42 | + publish: |
| 43 | + needs: check-version |
| 44 | + if: needs.check-version.outputs.should_publish == 'true' |
| 45 | + runs-on: ubuntu-latest |
| 46 | + |
| 47 | + steps: |
| 48 | + - uses: actions/checkout@v4 |
21 | 49 |
|
22 | 50 | - name: Setup Rust |
23 | | - uses: actions-rs/toolchain@v1 |
24 | | - with: |
25 | | - profile: minimal |
26 | | - toolchain: stable |
27 | | - override: true |
28 | | - |
| 51 | + uses: dtolnay/rust-toolchain@stable |
| 52 | + |
| 53 | + - name: Check if package is publishable |
| 54 | + run: cargo publish --dry-run |
| 55 | + |
29 | 56 | - name: Publish to crates.io |
30 | | - if: steps.check_version.outputs.version_updated == 'true' |
31 | 57 | run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }} |
| 58 | + |
| 59 | + |
0 commit comments