Search code examples
github-actions

Github action increment version on push to main


I would like to use a pure solution in a GitHub action to increment a version of the package. I don't want to use any existing actions from the GitHub marketplace such as "gh-action-bump-version ". I have this workflow, which will increase the version and create a tag.

name: Version Increment

on:
  push:
    branches:
      - main
    tags-ignore:
      - v*

jobs:
  version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
      - run: git config user.email "[email protected]"
      - run: git config user.name "$GITHUB_ACTOR"
      - run: npm version minor -m "v%s"
      - run: VERSION=$(node -p "require('./package.json').version")
      - run: git tag ${VERSION}
      - run: git push origin --tags
      - run: git push origin --follow-tags

It works, but it also cause a circular runs of the actions because of the last row. I know that I can use a custom message like "[RELEASE]" and put there a "if" condition and skip these commits. But my question is, is there any better solution to skip these commits from this action and do not use the "if" condition? Because the "tags-ignore" obviously doesn't work.


Solution

  • So I found several solutions. The first is that you can put "[skip actions]" to your commit message and that commit will skip any github action that should run within the commit. The second one is to use an address of the repository with access token.

    This works pretty well for me:

    name: Version Increment
    
    on:
      push:
        branches:
          - main
    
    jobs:
      version:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - run: git config user.email "[email protected]"
          - run: git config user.name "$GITHUB_ACTOR"
          - run: npm version minor -m "v%s"
          - run: VERSION=$(node -p "require('./package.json').version")
          - run: git tag ${VERSION}
          - run: git push "https://$GITHUB_ACTOR:${{ secrets.ACCESS_TOKEN }}@github.com/$GITHUB_REPOSITORY.git" --follow-tags
          - run: git push "https://$GITHUB_ACTOR:${{ secrets.ACCESS_TOKEN }}@github.com/$GITHUB_REPOSITORY.git" --tags