Github action how to remove starting 'v' from variable

Viewed 796

I have an workflow to publish nuget package on a release event, but i'm not able to strip the 'v' char from tagname. All my tag names are v${version} so i need to strip that 'v' and get the version only.

I'm with this workflow:

name: Nuget package publish

on:
  release:
    types: [published]

jobs:

  nuget:
    name: Nuget - Publish package
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Verify commit exists in origin/master
        run: |
          git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
          git branch --remote --contains | grep origin/master
      - name: Set VERSION variable from tag
        run: | 
          echo "VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
          echo "VERSION=${VERSION:1}" >> $GITHUB_ENV
      - name: Build
        run: dotnet build --configuration Release
      - name: Pack
        run: dotnet pack UVtools.Core --configuration Release --no-build --output .
      - name: Push nuget.org
        run: dotnet nuget push UVtools.Core.${VERSION}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${NUGET_TOKEN}
    env:
      NUGET_TOKEN: ${{ secrets.NUGET_TOKEN }}

Problem at:

Run echo "VERSION=v3.2.0" >> $GITHUB_ENV
echo "VERSION=v3.2.0" >> $GITHUB_ENV
echo "VERSION=${VERSION:1}" >> $GITHUB_ENV
shell: /usr/bin/bash -e {0}

On my attempt to strip the 'v' VERSION is set to empty

error: File does not exist (UVtools.Core..nupkg).

How could i strip the 'v' from variable?

PS: Under a bash script on my machine i tested:

VERSION=v1.5.0
echo $VERSION
echo "${VERSION:1}"

Which produces:

v1.5.0
1.5.0

2 Answers

You do not need to export the initial version string to the environment. It seems like the second call is not able locate it correctly in your example.

This works just fine in GitHub Actions:

VERSION=${{ github.event.release.tag_name }}
echo "VERSION=${VERSION:1}" >> $GITHUB_ENV

The answer from @timmeinerzhagen works, but to make sure only a possible "v" prefix will be stripped out, you can use:

TAG=${{ github.event.release.tag_name }}
echo "VERSION=${TAG#v}" >> $GITHUB_ENV
Related