Search code examples
angulargithubnpmgithub-actionscicd

How to bump NPM semantic version and create a tag using Pull Request Labels with GitHub Actions workflow


I've set up a GitHub Actions workflow that triggers whenever a pull request is created into the develop branch. The purpose of this workflow is to automatically bump the npm version of my Angular project and create a corresponding tag. Although the workflow jobs are executing successfully, there's an unexpected issue: when pushing the new tag (under the -name: Push Tag step), the develop branch is inadvertently getting deleted (refer to the attached image).

I have a general question too, Which branch, main or develop, is the optimal choice for bumping a version and creating a tag?

enter image description here

name: prettying, Semantic Versioning, and Tagging
on:
  pull_request:
    types: [opened, reopened, synchronize]
    branches:
      - develop

jobs:
  update-version:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set Git User Identity
        run: |
          # Create 'secrets' variables in Repository secrets(repository -> settings -> Secrets and Variables -> Actions -> Repository secrets)
          git config --global user.email "${{ secrets.GIT_USER_EMAIL }}"
          git config --global user.name "${{ secrets.GIT_USER_NAME }}"

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Pretty checking & auto formating
        run: |
          echo "executing prettier commands."
          npm ci
          npm run prettier:check
          npm run prettier:format

      - name: Determine Version Increment Level
        id: determine-version-level
        run: |
          # Check based on Conventional Commits.
          # The command git log --format=%B -n 1 retrieves the commit message of the latest commit (-n 1 specifies that we want the message of the most recent commit).
          commit_message=$(git log --format=%B -n 1)
          echo "commit_message...: $commit_message"
          if [[ $commit_message == *"breaking changes:"* || $commit_message == *"breaking change:"* || $commit_message == *"BREAKING CHANGE:"* ]]; then
            echo "version-level=major" >> $GITHUB_OUTPUT
          elif [[ $commit_message == *"feat:"* || $commit_message == *"feature:"* ]]; then
            echo "version-level=minor" >> $GITHUB_OUTPUT
          else
            echo "version-level=patch" >> $GITHUB_OUTPUT
          fi

      - name: Update Version Numbers using npm
        run: |
          version_level=${{ steps.determine-version-level.outputs.version-level }}
          echo "Version Level...: ${{ steps.determine-version-level.outputs.version-level }}"
          # Update version number and force increment
          npm version $version_level --force

      - name: Get Latest Tag & Save it
        id: get_tag
        run: echo "TAG=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV

      - name: Create Tag
        run: git tag ${{ steps.get_tag.outputs.TAG }}

      - name: Push Tag
        run: git push origin ${{ steps.get_tag.outputs.TAG }}:develop

I have tried with different git tag commands but have not succeeded.

Note: I do not want to use any package/library from Marketplace.


Solution

  • The "develop" branch is deleted because this is exactly what the "git push origin :develop" command should do.

    The "git push origin :develop" command deletes the branch because there's no value on the left side of the comma.

    There's no value on the left side of the comma because the TAG variable use is not correct:

    Change: ${{ steps.get_tag.outputs.TAG }}
    To:     ${{ env.TAG }}