|
| 1 | +name: Auto Version |
| 2 | + |
| 3 | +# This workflow automatically bumps the version of the project based on commit messages and PR titles |
| 4 | +# It follows the Semantic Versioning specification (https://semver.org/) |
| 5 | +# - feat: Minor version bump (0.1.0 -> 0.2.0) |
| 6 | +# - fix: Patch version bump (0.1.0 -> 0.1.1) |
| 7 | +# - BREAKING CHANGE or feat!: Major version bump (0.1.0 -> 1.0.0) |
| 8 | +# |
| 9 | +# The workflow is triggered when a PR is merged into the main branch. |
| 10 | +# It analyzes the commit messages and PR title to determine the appropriate version bump, |
| 11 | +# updates the version in Cargo.toml, generates release notes, and creates a new tag. |
| 12 | +# The tag creation triggers the release workflow, which builds and publishes the release. |
| 13 | + |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + types: [closed] |
| 17 | + branches: |
| 18 | + - main |
| 19 | + |
| 20 | +jobs: |
| 21 | + auto-version: |
| 22 | + if: github.event.pull_request.merged == true |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v3 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + - name: Setup Node.js |
| 32 | + uses: actions/setup-node@v3 |
| 33 | + with: |
| 34 | + node-version: '18' |
| 35 | + |
| 36 | + - name: Setup Rust |
| 37 | + uses: actions-rs/toolchain@v1 |
| 38 | + with: |
| 39 | + profile: minimal |
| 40 | + toolchain: stable |
| 41 | + override: true |
| 42 | + |
| 43 | + - name: Install cargo-edit |
| 44 | + run: cargo install cargo-edit |
| 45 | + |
| 46 | + - name: Install semantic-release and plugins |
| 47 | + run: | |
| 48 | + npm install -g semantic-release |
| 49 | + npm install -g @semantic-release/git |
| 50 | + npm install -g @semantic-release/changelog |
| 51 | + npm install -g @semantic-release/exec |
| 52 | +
|
| 53 | + - name: Create .releaserc.json |
| 54 | + run: | |
| 55 | + cat > .releaserc.json << 'EOF' |
| 56 | + { |
| 57 | + "branches": ["main"], |
| 58 | + "plugins": [ |
| 59 | + "@semantic-release/commit-analyzer", |
| 60 | + "@semantic-release/release-notes-generator", |
| 61 | + "@semantic-release/changelog", |
| 62 | + ["@semantic-release/exec", { |
| 63 | + "prepareCmd": "cargo set-version ${nextRelease.version} && sed -i 's/version = \"[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+\"/version = \"${nextRelease.version}\"/g' Cargo.toml" |
| 64 | + }], |
| 65 | + ["@semantic-release/git", { |
| 66 | + "assets": ["Cargo.toml", "Cargo.lock", "CHANGELOG.md"], |
| 67 | + "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" |
| 68 | + }] |
| 69 | + ] |
| 70 | + } |
| 71 | + EOF |
| 72 | +
|
| 73 | + - name: Run semantic-release |
| 74 | + env: |
| 75 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 76 | + run: semantic-release |
| 77 | + |
| 78 | + - name: Get new version |
| 79 | + id: get_version |
| 80 | + run: | |
| 81 | + VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/') |
| 82 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 83 | +
|
| 84 | + - name: Create tag |
| 85 | + if: steps.get_version.outputs.version != '' |
| 86 | + run: | |
| 87 | + git tag -a v${{ steps.get_version.outputs.version }} -m "Release v${{ steps.get_version.outputs.version }}" |
| 88 | + git push origin v${{ steps.get_version.outputs.version }} |
0 commit comments