Gajus
Gajus

Reputation: 73998

How to get a git ref from within GitHub Actions?

This is what I am trying:

- name: Create tag
  uses: actions/github-script@v3
  with:
    script: |
      console.log(await github.git.getRef({
        owner: context.repo.owner,
        ref: "refs/tags/v0.0.1",
        repo: context.repo.repo,
      }));

However, it is producing "Not Found" error.

If I check using git, I can see that tag exists:

$ git ls-remote
From [email protected]:contra/contra-deploy-tools.git
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891    HEAD
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891    refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18    refs/tags/v0.0.1

It appears like this is a permission issue. However, I am able to create a tag, and workflow permissions are configured to "Read and write permissions".

What's more surprising is that I am able to list references within the workflow:

Run git ls-remote
From https://github.com/contra/contra-deploy-tools
f0191b469168aa48ee177d270fa2af507d3f7710    HEAD
f0191b469168aa48ee177d270fa2af507d3f7710    refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18    refs/tags/v0.0.1

This seems to be API specific issue.

What am I missing?

Upvotes: 0

Views: 3092

Answers (1)

rethab
rethab

Reputation: 8443

You don't need the refs/ prefix in that API. Looking at the docs, it says that:

Returns a single reference from your Git database. The :ref in the URL must be formatted as heads/ for branches and tags/ for tags.

So if you change your step to the below, it should work.

- name: Create tag
  uses: actions/github-script@v3
  with:
    script: |
      console.log(await github.git.getRef({
        owner: context.repo.owner,
        ref: "tags/v0.0.1",
        repo: context.repo.repo,
      }));

NB: You're using github-script version 3, but there's already v5. If you want to keep your actions up to date, I previously described how that can be done using dependabot: https://stackoverflow.com/a/70196496/1080523

Upvotes: 1

Related Questions