133

Is there a way to access the current tag that has been pushed in a Github Action? In CircleCI you can access this value with the $CIRCLE_TAG variable.

My Workflow yaml is being triggered by a tag like so:

on:
  push:
    tags:
      - 'v*.*.*'

And I want to use that version number as a file path later on in the workflow.

8 Answers 8

166

As far as I know there is no tag variable. However, it can be extracted from GITHUB_REF which contains the checked out ref, e.g. refs/tags/v1.2.3

Try this workflow. It creates a new environment variable with the extracted version that you can use in later steps.

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set env
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Test
        run: |
          echo $RELEASE_VERSION
          echo ${{ env.RELEASE_VERSION }}

Alternatively, set a step output.

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set output
        id: vars
        run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
      - name: Check output
        env:
          RELEASE_VERSION: ${{ steps.vars.outputs.tag }}
        run: |
          echo $RELEASE_VERSION
          echo ${{ steps.vars.outputs.tag }}
11
  • 1
    That's perfect thank you, just one question what is the :10 referring to? string length?
    – Jon B
    Oct 1, 2019 at 5:32
  • 4
    It means it's extracting the substring starting at the 10th position (0-based indexing). So it skips refs/tags/ and just returns the last part of the string.
    – peterevans
    Oct 1, 2019 at 5:38
  • 1
    See the documentation here for what expressions are allowed. help.github.com/en/articles/…
    – peterevans
    Oct 1, 2019 at 6:55
  • 32
    Note that instead of using ${GITHUB_REF:10} to filter the name of the tag I would use the parameter expansion ${GITHUB_REF#refs/*/}. That would expand /refs/tags/v1.0.1 to v1.0.1 as expected, but would also work with branch names: /refs/heads/master would be expanded to master, see gnu.org/software/bash/manual/html_node/… Feb 14, 2020 at 8:43
  • 1
    this gives me the branch name instead of the tag it self e.g "1.2.0"
    – Moody Omar
    Oct 20, 2022 at 5:45
159

GitHub Contexts provides github.ref_name. You can use it like this: ${{github.ref_name}}.

Here is an example of this use in the artifact file name, which may be similar to the file path use that you asked about:

- name: Create tag artifact
  uses: actions/upload-artifact@v2
  with:
    name: ${{github.ref_name}}
    path: Release
6
  • 16
    thanks, should be a solution in 2021 onwards Nov 23, 2021 at 10:57
  • 1
    Agree, this should be the accepted answer.
    – alsami
    Dec 6, 2021 at 11:29
  • This should be the accepted answer as of May/2022. OP is limiting the workflow to run on pushes to a tag, so ${{ github.ref_name }} will always contain the name of the tag being pushed. May 12, 2022 at 22:51
  • 5
    Does this also work for on.release workflows or is it better to use ${{ github.event.release.tag_name }} in that case? May 16, 2022 at 13:49
  • For me ${{ github.ref_name}} returns the string "master", not the tag
    – El Sampsa
    Jun 21 at 19:03
43

Here is the 2022 answer. No need to do weird parsing

on:
  push:
    tags:
      - '*'
jobs:
  github-example-tags:
    runs-on: ubuntu-latest
    steps:
     - name: GitHub Tag Name example
       run: |
         echo "Tag name from GITHUB_REF_NAME: $GITHUB_REF_NAME"
         echo "Tag name from github.ref_name: ${{  github.ref_name }}"

See

https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables

1
  • error: A job requires either runs-on or uses
    – Bersan
    Dec 4, 2022 at 18:36
8

What worked for me:

run: echo "GIT_TAG=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_ENV
3
  • 1
    Worth noting that the above fails when no version tag is present which may not be suitable in all cases (other answers provide alternatives that fallback to empty string) but could help if it is not supposed to fail.
    – nfroidure
    Dec 8, 2020 at 9:15
  • this is a nice trick when you want to get a tag for a workflow that is manually dispatched Oct 14, 2021 at 8:35
  • this didn't work for me, GIT_TAG didn't contain the tag
    – knocte
    Oct 28, 2021 at 9:48
7

So thanks to all the help from @peterevans I managed to achieve the result I wanted which was:

  • to tag a commit
  • push the tag to trigger the github action
  • github action sets the git tag as an env var
  • run install & build
  • use chrislennon/action-aws-cli action to install aws cli using secrets for keys
  • run command to sync the build to a new S3 bucket using the tag env var as the dir name

Here is an example of the what I ran using Chris Lennon's action:

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set env
        run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:10})
      - name: yarn install & build
        run: |
          yarn install
          yarn build
      - uses: chrislennon/[email protected]
      - name: Publish to AWS S3
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
        run: aws s3 sync dist s3://$AWS_S3_BUCKET/$RELEASE_VERSION/ --acl public-read
2
  • What does this part ${GITHUB_REF:10} does ? Jun 19, 2020 at 16:34
  • 11
    Even though this question is pretty old, I'll answer regardless for future reference: The :10 strips the first ten characters from the environment variable GITHUB_REF which contains refs/tags/v1.2.3 so it yields v1.2.3 Jul 24, 2020 at 18:09
4

Here's a workflow run showing that the GITHUB_REF environment variable contains refs/tags/v0.0.2:

I ran that by creating the tag, then doing git push origin v0.0.2.

Here's a snippet of the workflow you see in that log:

steps:
- uses: actions/checkout@v1
- name: Dump GitHub context
  env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
  run: echo "$GITHUB_CONTEXT"
  if: runner.os != 'Windows'
- name: Show GitHub ref
  run: echo "$GITHUB_REF"
  if: runner.os != 'Windows'
- name: Dump event JSON
  env:
    EVENT_JSON_FILENAME: ${{ github.event_path }}
  run: cat "$EVENT_JSON_FILENAME"
  if: runner.os != 'Windows'

Since the log has been deleted, here's a screenshot for evidence:

enter image description here

4

You can use shell expansion:

echo "${GITHUB_REF##*/}"
0
1
steps:
- name: Checkout Repository
  uses: actions/checkout@v2
- name: Get Tags
  id: tags
  run: |
    git fetch --tags
    echo "Tags fetched"      
- name: Get Latest Tag
  id: latest-tag
  run: |
    latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`)
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 26 at 7:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.