2

is there any github actions for updating the packages version is package.json [the packages own version , not dependency] ? Ideally for push action , it should update the version and check in the changes back to repo

Janier
  • 3,982
  • 9
  • 43
  • 96

1 Answers1

2

I don't know of any actions that handle this complete flow. However, if you are able to update the version in a run script command it's fairly straightforward to commit back to the repository yourself in a workflow. See the following answer for how to prepare the checked out repository and git config in order to push to the remote.

Push to origin from GitHub action

Alternatively, you might find create-pull-request action useful for this use case. It will commit changes to the Actions workspace to a new branch and raise a pull request. So if you call create-pull-request action after npm version during a workflow, you can have that change raised as a PR for you to review and merge.

For example:

on: push
name: Update Version
jobs:
  createPullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Bump version
        run: npm version patch
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Bump version
          title: Bump version

Note: Using on: push may not be the best trigger for this use case.

peterevans
  • 34,297
  • 7
  • 84
  • 83
  • Thanks for the answer.But I am looking to update my own version on commits ;not dependency versions...almost like just run a npm version command and commit back to repo – Janier Oct 10 '19 at 18:13
  • I misunderstood what you wanted to do. Updated the answer. – peterevans Oct 10 '19 at 23:29
  • You will need to set a username/email with the following: git config --local user.email "some@email.com". git config --local user.name "GitHub Action". – zzarbi May 13 '20 at 23:22
  • If you need to increment the old version of one, you could use this action to retrieve the current one https://github.com/marketplace/actions/ga-project-version – EuberDeveloper Aug 18 '21 at 21:22
  • So if I use `on: push` to update package version and then push to origin, it re-triggers the workflow again, which does a new push and again the workflow. Sort of an endless loop. Any idea how to resolve this. @peterevans – Subham Saha Mar 22 '22 at 06:33
  • 1
    @SubhamSaha you can use something like this add a keword in the commit `git commit -m "some commit message [ci-skip]"` and then check in your GA action file if it includes that in the commit messsage `if: "!contains(toJSON(github.event.commits.*.message), '[ci-skip]')"` (or do this https://docs.github.com/en/actions/managing-workflow-runs/skipping-workflow-runs) – pgarzina Mar 23 '22 at 14:30
  • Yes @pgarzina I finally was able to do so. So I was updating the package version as part of the ci job with the help of standard-version library. And it required to make a push, which was triggering the github bot to restart the workflow with a self formed commit message (`chore(release):version`), thus didnot have the opportunity to add any specific string like the one you mentioned. But later put the check against that string. – Subham Saha Mar 24 '22 at 16:14