Search code examples
github-actions

How do I set an enviroment variable in GitHub action on a Windows Server?


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


Solution

  • 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: $env:GITHUB_ENV

    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