|
| 1 | +name: Bump package version |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + workspace: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + description: 'Yarn workspace name' |
| 10 | + path: |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + description: 'Package directory relative to repo root' |
| 14 | + |
| 15 | +jobs: |
| 16 | + bump: |
| 17 | + runs-on: ubuntu-22.04 |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 23 | + ref: ${{ github.event.pull_request.head.ref }} |
| 24 | + |
| 25 | + - name: Check if package was modified and version needs bump |
| 26 | + id: check |
| 27 | + run: | |
| 28 | + # Exit cleanly if the package directory was not touched in this PR |
| 29 | + if git diff --quiet ${{ github.event.pull_request.base.sha }} HEAD -- ${{ inputs.path }}; then |
| 30 | + echo "No changes detected in ${{ inputs.path }}." |
| 31 | + echo "should-bump=false" >> $GITHUB_OUTPUT |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + # Compare old (base) and new (HEAD) version strings |
| 36 | + OLD_VERSION=$(git show ${{ github.event.pull_request.base.sha }}:${{ inputs.path }}/package.json | jq -r '.version') |
| 37 | + NEW_VERSION=$(jq -r '.version' ${{ inputs.path }}/package.json) |
| 38 | +
|
| 39 | + if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then |
| 40 | + echo "Version already bumped by developer ($OLD_VERSION → $NEW_VERSION)." |
| 41 | + echo "should-bump=false" >> $GITHUB_OUTPUT |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + echo "should-bump=true" >> $GITHUB_OUTPUT |
| 46 | +
|
| 47 | + - name: Determine bump level from conventional commits |
| 48 | + id: level |
| 49 | + if: steps.check.outputs.should-bump == 'true' |
| 50 | + run: | |
| 51 | + # Gather full commit messages (subject + body) only for commits touching this package |
| 52 | + COMMITS=$(git log ${{ github.event.pull_request.base.sha }}..HEAD --format=%B -- ${{ inputs.path }}) |
| 53 | +
|
| 54 | + if echo "$COMMITS" | grep -qE '^[a-z]+(\([^)]+\))?!:'; then |
| 55 | + LEVEL=major |
| 56 | + elif echo "$COMMITS" | grep -qE 'BREAKING[- ]CHANGE'; then |
| 57 | + LEVEL=major |
| 58 | + elif echo "$COMMITS" | grep -qE '^feat(\([^)]*\))?:'; then |
| 59 | + LEVEL=minor |
| 60 | + else |
| 61 | + LEVEL=patch |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "level=$LEVEL" >> $GITHUB_OUTPUT |
| 65 | + echo "Bumping ${{ inputs.workspace }} with $LEVEL level" |
| 66 | +
|
| 67 | + - uses: actions/setup-node@v4 |
| 68 | + if: steps.check.outputs.should-bump == 'true' |
| 69 | + with: |
| 70 | + node-version-file: '.nvmrc' |
| 71 | + |
| 72 | + - run: corepack enable |
| 73 | + if: steps.check.outputs.should-bump == 'true' |
| 74 | + |
| 75 | + - run: yarn |
| 76 | + if: steps.check.outputs.should-bump == 'true' |
| 77 | + |
| 78 | + - name: Bump ${{ inputs.workspace }} version |
| 79 | + if: steps.check.outputs.should-bump == 'true' |
| 80 | + run: yarn workspace ${{ inputs.workspace }} version ${{ steps.level.outputs.level }} |
| 81 | + |
| 82 | + - uses: EndBug/add-and-commit@v9 |
| 83 | + if: steps.check.outputs.should-bump == 'true' |
| 84 | + with: |
| 85 | + author_name: github-actions |
| 86 | + author_email: 41898282+github-actions[bot]@users.noreply.github.com |
| 87 | + message: 'chore: bump ${{ inputs.workspace }} version' |
| 88 | + pull: '--rebase --autostash' |
0 commit comments