Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 438
Simon Aberle Avatar asked May 17 '20 20:05

Simon Aberle


People also ask

How do I set an environment variable on a server?

In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable. Click Edit to modify an existing environment variable.

Where do I put environment variables in GitHub?

You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file. The contents of a job within a workflow, by using jobs.<job_id>.env . A specific step within a job, by using jobs.<job_id>.steps[*].env .


1 Answers

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
like image 157
Sal Avatar answered Jan 04 '23 11:01

Sal