Is it possible to update files as part of a merge via GitHub Actions without creating a second commit?

Viewed 744

Currently, I'm trying to automate the bump versions for the applications as part of the PR Merge using the GitHub Actions. There is a custom script that would identify the current version of the application and the label attached with the PR and bump the major|minor|patch version accordingly on the file where the version numbers are stored. It is important that the version bump happens only at the time when PR is merged and as part of GitHub Actions because it helps in avoiding the merge conflict in the version file and takes away the manual way of bumping the version.

The GitHub Actions code snippet is given below.

jobs:
  release:
    # Skip on Pull Request Close event.
    if: "!(github.event_name == 'pull_request' && !github.event.pull_request.merged)"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}

      - id: bumpversion
        if: github.event.pull_request.merged
        env:
          PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
        run: bash .github/scripts/bump_version.sh -l "${PR_LABELS}"

The bump_version.sh script has a function given below that makes the version changes and then pushes them to the main branch.

  git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

  echo "commiting the version bump changes..."
  git add .
  git commit -m "Bump Version v${current_version} -> v${incremented_version}"
  echo "pushing the version bump changes..."
  git push origin dev

This runs fine but the problem is that it makes 2 commits on the main branch when a PR is merged to it. There is a CI/CD pipeline that listens to this main branch for changes and it gets triggered when there is a new commit to it. Since this PR merge makes 2 commits, it is triggered twice.

2 commits for single PR merge image

The question: Is it possible to update files as part of a merge via GitHub Actions without creating a second commit? Is there any other way to achieve a solution to this problem that will help me to bump the version as part of the PR Merge?

1 Answers

My immediate thought is that you could alter the trigger ("on:") in CI/CD to ignore the first of the 2 commits, and only run CI/CD on the second one.

There's a number of ways you could do that, utilizing any of the metadata that comes with a commit. Here's an idea that utilizes the tag. You'd have to refactor the code that defines incremented_version probably, but shouldn't be hard to do in bash.

Here's a CI/CD that would ignore something tagged *.0.0, but would trigger on anything like *.1 or *.0.1:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - !v*.0.0
      - !v*.0  # Do not push events to tags v1.0.0, v1.0, v2.0.0, and v2.0, etc.

Personally I like the idea of making the 1st of the 2 commits have an extra .0 and ignoring those. It seems cleaner. After configuring the bash script to do that,:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - !v*.0.0 # Do not push events to tags v1.0.0, v2.0.0, etc.
Related