Jon B
Jon B

Reputation: 2834

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.

Upvotes: 159

Views: 131195

Answers (9)

Mostafa Wael
Mostafa Wael

Reputation: 3846

The commit tag is expected to be in $GITHUB_REF (e.g. refs/tags/v1.2.3)

I use a simple line to extract just the tag name (v1.2.3):

# This condition is optional indeed
if [[ -z "${GITHUB_REF}" ]]; then
    echo "GITHUB_REF environment variable not set"
    exit 1
fi

COMMIT_TAG=$(basename "$GITHUB_REF") # The only line you need

echo "This commit tag is: ($COMMIT_TAG)"

Upvotes: 0

Allen Pestaluky
Allen Pestaluky

Reputation: 3724

GitHub Contexts and Variables provide both the github.ref (i.e., "refs/tags/my_tag") and github.ref_name (i.e., "my_tag"). You can use them like this: ${{ github.ref_name }}.

However, take in mind that their value may depend on the event that triggered the workflow:

  • workflow_dispatch: the selected branch (e.g., "refs/heads/my_branch" and "my_branch")

  • pull_request: the pull request merge branch (e.g., "refs/pull/1/merge" and "1/merge", where "1" is the pull request number)

  • push: the pushed branch (e.g., "refs/heads/my_branch" and "my_branch") or tag (e.g., "refs/tags/my_tag" and "my_tag")

  • release: the release tag (e.g., "refs/tags/my_tag")

    NOTE: The github.ref_name for workflows triggered by this event does not return the tag name. Use instead the github.event.release.tag_name (e.g., "my_tag").

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

Upvotes: 202

Gaurav Khatri
Gaurav Khatri

Reputation: 11

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`)

Upvotes: 1

Jirawat Uttayaya
Jirawat Uttayaya

Reputation: 1312

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

Upvotes: 57

peterevans
peterevans

Reputation: 42160

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

Upvotes: 171

Thiago Falcao
Thiago Falcao

Reputation: 5023

You can use shell expansion:

echo "${GITHUB_REF##*/}"

Upvotes: 4

lewislbr
lewislbr

Reputation: 1182

What worked for me:

run: echo "GIT_TAG=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_ENV

Upvotes: 13

rmunn
rmunn

Reputation: 36708

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

Upvotes: 4

Jon B
Jon B

Reputation: 2834

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

Upvotes: 7

Related Questions