Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions create or update a Git Tag

Each time I commit to a Pull Request branch I want to tag it like 0.0.0-pr1 The below action works on initial commit. But not on subsequent commits I get Error: Unhandled error: HttpError: Reference already exists

Is there a create or update Ref API I could use?

    env:
      PR_NUMBER: ${{ github.event.number }}

    steps:

    - name: Create Git tag for PR
      uses: actions/github-script@v4
      with:
        script: |
          github.git.createRef({
            owner: context.repo.owner,
            repo: context.repo.repo,
            ref: "refs/tags/0.0.0-pr${{env.PR_NUMBER}}",
            sha: context.sha
          })
like image 532
DarVar Avatar asked Aug 10 '26 13:08

DarVar


1 Answers

A variation of Audun's solution:

You can catch the error and perform an updateRef() instead:

permissions:
  contents: write

steps:
  - name: 🏷️ Create/update tag
    uses: actions/github-script@v7
    with:
      script: |
        github.rest.git.createRef({
          owner: context.repo.owner,
          repo: context.repo.repo,
          ref: 'refs/tags/v${{ needs.version.outputs.version }}',
          sha: context.sha
        }).catch(err => {
          if (err.status !== 422) throw err;
          github.rest.git.updateRef({
            owner: context.repo.owner,
            repo: context.repo.repo,
            ref: 'tags/v${{ needs.version.outputs.version }}',
            sha: context.sha
          });
        })

I also used EndBug/latest-tag for a little while, but it is currently triggering warnings because it is still using an old version of node.

like image 77
9Rune5 Avatar answered Aug 12 '26 12:08

9Rune5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!