Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Pilot Lvl 1
Message 11 of 19

Re: How to get just the tag name?

I prefer this syntax:

 

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

 

Highlighted
Copilot Lvl 3
Message 12 of 19

Re: How to get just the tag name?

My approach is setting three vars:

SOURCE_NAME: The branch or the tag

SOURCE_BRANCH: The branch or empty

SOURCE_TAG: The tag or empty

      - name: Branch name
        id: branch_name
        run: |
          echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/}
          echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/}
          echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}

And to use it, just declare as ENV vars in each task

      - name: Build base image
        run: |
          echo $SOURCE_NAME
          echo $SOURCE_BRANCH
          echo $SOURCE_TAG          
        env:
          SOURCE_NAME: ${{ steps.branch_name.outputs.SOURCE_NAME }}
          SOURCE_BRANCH: ${{ steps.branch_name.outputs.SOURCE_BRANCH }}
          SOURCE_TAG: ${{ steps.branch_name.outputs.SOURCE_TAG }}
Highlighted
Ground Controller Lvl 1
Message 13 of 19

Re: How to get just the tag name?

This is awefull and annoying, I been wasting my time and breaking my head over this for hours. Gitlab has a dead simple straight forward to the point

CI_COMMIT_SLUG

that gives you a short represenstation of the current build without any charcters you do not want for filename and the likes. Just [a-z0-9-] the fact that we have to use this super ugly and annoying to read syntax

echo ::set-output name=SOURCE_NAME::${...

 

BELOW THE job env declaration sucks. And its not finished there, still need to cut out characters.

 

Github shoud just copy Gitlab here and provide a slug.

 

If anyone has a idea hwo I in a most siple way copy wtat gitlab may to to output the slug I would appreciate it.

Highlighted
Pilot Lvl 1
Message 14 of 19

Re: How to get just the tag name?

@ethomson how would you do for a Windows runner ?
Highlighted
GitHub Staff
Message 15 of 19

Re: How to get just the tag name?

I'm not very familiar with PowerShell, so what I'd do is just run it in bash on Windows.  You can do this by setting `shell: bash` for that run step.

Highlighted
Ground Controller Lvl 1
Message 16 of 19

Re: How to get just the tag name?

With Windows (runs-on: windows-latest) it would be

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

and you can use it with PowerShell e.g.

- name: Zip .exe
  run: Compress-Archive -Path some.exe -DestinationPath Release_${{ steps.get_version.outputs.VERSION }}.zip -CompressionLevel Optimal

 

Highlighted
Copilot Lvl 3
Message 17 of 19

Re: How to get just the tag name?

I use this for windows

$SOURCE_NAME = ${env:GITHUB_REF} -replace 'refs\/\w+\/', ''
$SOURCE_BRANCH = ${env:GITHUB_REF} -replace 'refs\/heads\/', ''
$SOURCE_TAG = ${env:GITHUB_REF} -replace 'refs\/tags\/', ''

echo "::set-env name=SOURCE_NAME::$SOURCE_NAME"
echo "::set-env name=SOURCE_BRANCH::$SOURCE_BRANCH"
echo "::set-env name=SOURCE_TAG::$SOURCE_TAG"
Highlighted
Copilot Lvl 2
Message 18 of 19

Re: How to get just the tag name?

> Note that tags can contain a `/` character in them.

 

Can they? When I run `docker build . -t foo:bar/baz` I get `invalid argument "foo:bar/baz" for "-t, --tag" flag: invalid reference format` due to the forward slash.

Highlighted
Ground Controller Lvl 1
Message 19 of 19

Re: How to get just the tag name?

Any update on that? I'm strugglin because I want to publish artifacts from the build but for some reason they have to be unique. Okay, I can use tag, but there is no variable for it. And I cannot use suggested workarounds becuase i need to use tag in matrix, before all the jobs are run.