8

First of all let me explain what I'm trying to do. I do write a c# .net application which I want to build if a tag was pushed to the master branch. This build should create a release named like Release {Tag}. The release should get all the artifacts which got build by the Windows Server. Right now I fail to get the tag only without the stuff in front of it.

I did find a question on how to set an environment variable here but this seems to work on linux only like this. I did try to find the information in the official documentation but I don't get it into a working state. Currently I'm using the following code trying to get the tag from the commit.

name: Live build

on: [push]
  #push:
  #  tags:
  #   - '*'

jobs:
   build:
     name: Create build artifact
     runs-on: windows-latest
     steps:
       - name: Clone repository
         uses: actions/checkout@v2
         with: 
           ref: develop
       - name: Get current tag
         run: echo '::set-env name=tag::${(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")}'
       - name: Show current tag
         run: echo "${env.tag}"

Unfortunately this is the result, which does not look correct to me

1

I did try to replace this part echo '::set-env name=tag::${(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")}' the call with the following test

  • echo '::set-env name=tag::(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")'
  • echo '::set-env name=tag::$(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")'
  • echo ::set-env name=tag::$(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")
  • echo ::set-env name=tag::(("${env:GITHUB_REF}" -split "/")[-1] -replace " ","")

Nothing did work just yet ... The default shell is set to powershell in this setup.

edit: Adding documentation from GitHub

Xanatos
  • 395
  • 2
  • 11

2 Answers2

24

With the latest changes to Github, the set-env command is deprecated. The new recommended way is to append text in UTF-8 encoding to a Github environmental file.

This is how I do it to get the current branch's name in windows powershell:

- run: |
   chcp 65001 #set code page to utf-8
   echo ("BRANCH_NAME=" + $env:GITHUB_REF.replace('refs/heads/', '')) >> $env:GITHUB_ENV

- run: echo "${{ env.BRANCH_NAME }}"
- run: echo $env:BRANCH_NAME
Sal
  • 5,129
  • 5
  • 27
  • 53
  • 8
    Key here is to use `$env:GITHUB_ENV` on Windows. This is not mentioned in the Github Actions docs, sadly. – varagrawal Aug 08 '21 at 17:49
  • 1
    There is a passing reference to using `$env` when running on Windows at https://docs.github.com/en/actions/learn-github-actions/environment-variables#detecting-the-operating-system – Appetere Sep 12 '22 at 10:19
2

The method described in this answer is now deprecated. Please use this other answer.

--- Use this:
run: echo "::set-env name=tag::$(($env:GITHUB_REF -split '/')[-1] -replace ' ','')"

To get the variable, you just access it as if it were an environment variable:

run: echo "${env:tag}"
smac89
  • 39,374
  • 15
  • 132
  • 179
  • 3
    `set-env` was removed on 11/19/2020 due to a security issue: https://github.blog/changelog/2020-11-09-github-actions-removing-set-env-and-add-path-commands-on-november-16/ See Sai's answer for the new recommended way. – Philipp Hansch Feb 07 '21 at 14:30
  • Changed the Answer :) Thank you for mentioning. Just took me a while to register it :D – Xanatos Aug 27 '22 at 17:59