Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 19

How to get just the tag name?

Solved! Go to Solution.

I trigger my workflow using

 

on:
  push:
    tags:

 

and I just want the name of the current tag that is just building.

 

Sadly, the `github` context variable's `ref` has this value:

"ref": "refs/tags/0.3.1"

However, I only want the last part, 0.3.1.

 

There exists no function in GitHub Actions that allows me to extract that.

 

Am I overlooking something? How do I get that?

18 Replies
Highlighted
Copilot Lvl 3
Message 2 of 19

Re: How to get just the tag name?

Highlighted
Copilot Lvl 3
Message 3 of 19

Re: How to get just the tag name?

Nothing indicates that GitHub Actions use bash to evaluate their variables. Are you quite that this is supposed to work?

Highlighted
Pilot Lvl 1
Message 4 of 19

Re: How to get just the tag name?

You could use actions/github-script

 

jobs:
  test:
    runs-on: ubuntu-18.04
    steps:
      - name: Extract tag name
        id: tag
        uses: actions/github-script@0.2.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            return context.payload.ref.replace(/\/refs\/tags\//, '');
      - name: Echo
        run: echo ${{ steps.tag.outputs.result }}
Highlighted
Copilot Lvl 2
Message 5 of 19

Re: How to get just the tag name?

Have you tested this? This produced an empty value for me for some reason.

Highlighted
Mission Specialist Lvl 1
Message 6 of 19

Re: How to get just the tag name?

Any idea how to get the tag name to the artifact file name? I have been trying to use this steps.tag.output.result to the artifact filename but its returing null.

 

Highlighted
Copilot Lvl 2
Message 7 of 19

Re: How to get just the tag name?

This is how I'm currently doing it:

 

- name: Get the version
  id: get_version
  run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)

 

Then you have the output of that step and any later step in the job can use this by referring to it either in the run part or in an input. 

 

${{ steps.get_version.outputs.VERSION }}
Highlighted
Solution
GitHub Staff
Message 8 of 19

Re: How to get just the tag name?

Note that tags can contain a `/` character in them.  For completeness, I'd actually recommend explicitly removing the `refs/tags/` instead of splitting on the `/` as a delimeter.  For example:

 

- name: Get the version
  id: get_version
  run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

 

Highlighted
Copilot Lvl 3
Message 9 of 19

Re: How to get just the tag name?

Thanks. Could you add that as a general context/environment variable? It seems to me to be a very common use case and this step of truncating the string is just really annoying IMO.

 

Highlighted
GitHub Staff
Message 10 of 19

Re: How to get just the tag name?

At the moment we're basically populating the data from the webhook.  If we start adding additional data beyond that then this seems like a good candidate.  Thanks for the feedback!