|
1 |
| -name: Release |
| 1 | +name: Auto Release |
2 | 2 |
|
3 | 3 | on:
|
4 | 4 | push:
|
5 |
| - tags: |
6 |
| - - 'v*.*.*' |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - Cargo.toml |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
7 | 13 |
|
8 | 14 | jobs:
|
9 |
| - build: |
| 15 | + check-version: |
10 | 16 | runs-on: ubuntu-latest
|
11 |
| - |
| 17 | + outputs: |
| 18 | + version_changed: ${{ steps.check_version.outputs.version_changed }} |
| 19 | + new_version: ${{ steps.check_version.outputs.new_version }} |
12 | 20 | steps:
|
13 |
| - - name: Checkout code |
14 |
| - uses: actions/checkout@v3 |
| 21 | + - uses: actions/checkout@v3 |
| 22 | + with: |
| 23 | + fetch-depth: 2 |
| 24 | + - name: Check if version changed |
| 25 | + id: check_version |
| 26 | + run: | |
| 27 | + PACKAGE_NAME=$(grep '^name' Cargo.toml | sed 's/name = "\(.*\)"/\1/') |
| 28 | + RELEASED_VERSION=$(cargo search $PACKAGE_NAME --limit 1 | grep -oP '(?<=").*(?=")') |
| 29 | + if [ $? -ne 0 ]; then |
| 30 | + echo "Failed to fetch released version" |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | + NEW_VERSION=$(grep '^version' Cargo.toml | sed 's/version = "\(.*\)"/\1/') |
| 34 | + |
| 35 | + if [ "$RELEASED_VERSION" != "$NEW_VERSION" ]; then |
| 36 | + echo "Version changed from $RELEASED_VERSION to $NEW_VERSION" |
| 37 | + echo "version_changed=true" >> $GITHUB_OUTPUT |
| 38 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 39 | + else |
| 40 | + echo "No version change" |
| 41 | + fi |
15 | 42 |
|
16 |
| - - name: Set up Rust |
17 |
| - uses: actions-rs/toolchain@v1 |
18 |
| - with: |
19 |
| - toolchain: stable |
20 |
| - override: true |
21 |
| - |
22 |
| - - name: Build project |
23 |
| - run: cargo build --release |
| 43 | + create-release: |
| 44 | + needs: check-version |
| 45 | + if: needs.check-version.outputs.version_changed == 'true' |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - uses: actions/checkout@v3 |
| 49 | + - name: Create Release |
| 50 | + env: |
| 51 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + run: | |
| 53 | + gh release create v${{ needs.check-version.outputs.new_version }} \ |
| 54 | + --title "Release ${{ needs.check-version.outputs.new_version }}" \ |
| 55 | + --generate-notes |
24 | 56 |
|
25 |
| - # - name: Archive the build artifacts |
26 |
| - # run: tar -czvf build.tar.gz -C target/release . |
27 |
| - # |
28 |
| - # - name: Create Release |
29 |
| - # id: create_release |
30 |
| - # uses: softprops/action-gh-release@v1 |
31 |
| - # with: |
32 |
| - # tag_name: ${{ github.ref }} |
33 |
| - # files: build.tar.gz |
34 |
| - # env: |
35 |
| - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
36 |
| - - name: Publish to crates.io |
37 |
| - env: |
38 |
| - CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
39 |
| - run: cargo publish --token $CARGO_REGISTRY_TOKEN |
| 57 | + publish: |
| 58 | + needs: [check-version, create-release] |
| 59 | + runs-on: ubuntu-latest |
| 60 | + steps: |
| 61 | + - uses: actions/checkout@v3 |
| 62 | + - name: Set up Rust |
| 63 | + uses: actions-rs/toolchain@v1 |
| 64 | + with: |
| 65 | + toolchain: stable |
| 66 | + override: true |
| 67 | + - name: Publish to crates.io |
| 68 | + env: |
| 69 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
| 70 | + run: cargo publish --token $CARGO_REGISTRY_TOKEN |
0 commit comments