125

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
Jon B
  • 2,444
  • 2
  • 18
  • 19

8 Answers8

161

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 }}
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • 1
    That's perfect thank you, just one question what is the :10 referring to? string length? – Jon B Oct 01 '19 at 05: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 01 '19 at 05:38
  • Ok one more question sorry, I want to use that $RELEASE_VERSION variable to create a destination path for an S3 action like so: `DEST_PATH: "${{ secrets.AWS_S3_BUCKET }}/$RELEASE_VERSION"` but can't get the syntax correct, any ideas? (this is in an ENV for a 3rd party action that I use later in the yaml by the way) – Jon B Oct 01 '19 at 06:14
  • I think the issue is that you can't evaluate expressions when assigning values to `env` or `with`. You need to create an environment variable with `set-env` to evaluate the expression. If the 3rd party action accepts `env` then I don't think you need to explicitly set it. The action will find it from the environment. Alternatively, you can use `set-output` instead. I've updated the answer to show this method. – peterevans Oct 01 '19 at 06:47
  • 1
    See the documentation here for what expressions are allowed. https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions – peterevans Oct 01 '19 at 06:55
  • Just to clarify. Using `set-output` you should be able to set the `env` value for the action like this. `DEST_PATH: ${{ secrets.AWS_S3_BUCKET }}/${{ steps.vars.outputs.tag }}` – peterevans Oct 01 '19 at 07:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200266/discussion-between-jon-b-and-peterevans). – Jon B Oct 02 '19 at 01:25
  • 31
    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 https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html – Stefan Haberl Feb 14 '20 at 08:43
  • 1
    this gives me the branch name instead of the tag it self e.g "1.2.0" – Moody Omar Oct 20 '22 at 05:45
  • `ste-output` is [deprecated now and we should use environment files instead](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) – Krzysztof Madej Nov 29 '22 at 20:19
147

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
Allen Pestaluky
  • 3,126
  • 2
  • 20
  • 22
35

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

Gili
  • 86,244
  • 97
  • 390
  • 689
Jirawat Uttayaya
  • 1,039
  • 9
  • 11
7

What worked for me:

run: echo "GIT_TAG=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_ENV
lewislbr
  • 1,012
  • 14
  • 23
  • 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 08 '20 at 09:15
  • this is a nice trick when you want to get a tag for a workflow that is manually dispatched – Roman Dodin Oct 14 '21 at 08:35
  • this didn't work for me, GIT_TAG didn't contain the tag – knocte Oct 28 '21 at 09:48
6

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/action-aws-cli@v1.1
      - 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
Jon B
  • 2,444
  • 2
  • 18
  • 19
  • What does this part ${GITHUB_REF:10} does ? – Nitish Kumar Jun 19 '20 at 16:34
  • 10
    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` – Til Blechschmidt Jul 24 '20 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

yivi
  • 42,438
  • 18
  • 116
  • 138
rmunn
  • 34,942
  • 10
  • 74
  • 105
4

You can use shell expansion:

echo "${GITHUB_REF##*/}"
Thiago Falcao
  • 4,463
  • 39
  • 34
0
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
    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](/help/how-to-answer). – Community Aug 26 '23 at 07:34