githubcontinuous-integrationgithub-actions

Get the current pushed tag in Github Actions


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.


Solution

  • 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 }}