Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github action how to remove starting 'v' from variable

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

like image 933
Tiago Conceição Avatar asked Jan 23 '26 13:01

Tiago Conceição


2 Answers

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
like image 129
Eliezio Oliveira Avatar answered Jan 26 '26 23:01

Eliezio Oliveira


I just wanted to follow up with a version that is even simpler and works as of 2022.

# Store the version, stripping any v-prefix
- name: Write release version
  run: |
    VERSION=${GITHUB_REF_NAME#v}
    echo Version: $VERSION
    echo "VERSION=$VERSION" >> $GITHUB_ENV

# Use version in other step
- name: Read version
  run: echo Version now: ${VERSION}

The GITHUB_REF_NAME will contain the name of the tag. When your on-part looks like this:

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
like image 36
Markus Knappen Johansson Avatar answered Jan 26 '26 23:01

Markus Knappen Johansson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!