To reference a variable from the given context (env in this case) in the GitHub Actions workflow we have to use a dot (.) character, but you used a colon (:). To fix the error above the workflow should be adjusted:
- name: Checkout repo
uses: actions/checkout@v2
with:
repository: 'MyOrg/MyRepo'
ref: ${{env.CURRENT_BRANCH}}
Additionally, you don't have to detect the current branch on and pass it to the checkout action. actions/checkout@v2 will use the current branch by default. So you only have to have:
- name: Checkout repo
uses: actions/checkout@v2
- name: Next Step
[...]
Answer from Marcin Kłopotek on Stack OverflowTo reference a variable from the given context (env in this case) in the GitHub Actions workflow we have to use a dot (.) character, but you used a colon (:). To fix the error above the workflow should be adjusted:
- name: Checkout repo
uses: actions/checkout@v2
with:
repository: 'MyOrg/MyRepo'
ref: ${{env.CURRENT_BRANCH}}
Additionally, you don't have to detect the current branch on and pass it to the checkout action. actions/checkout@v2 will use the current branch by default. So you only have to have:
- name: Checkout repo
uses: actions/checkout@v2
- name: Next Step
[...]
This is a working method now, more info here:
echo "action_state=yellow" >> $GITHUB_ENV
The one below no longer works for me. A bit of research showed it is due to a security issue.
echo "::set-output name=action_state::yellow"
If you are using a Windows/PowerShell environment, you have to use $env:GITHUB_ENV instead of $GITHUB_ENV:
echo "GENERATOR_PLATFORM=$generator" >> $env:GITHUB_ENV
This way, you can access your env var through $env:GENERATOR_PLATFORM, eg:
run: echo $env:GENERATOR_PLATFORM
To follow up on @soltex answer: The proposed solution only works if the encoding is set to utf-8. If your runner is using Windows PowerShell (i.e. not PowerShell v7+, which uses utf-8 by default), utf16-le is written to the environment file, which causes the variable to not being set.
The correct solution is this:
echo "GENERATOR_PLATFORM=$generator" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
From: https://github.com/actions/runner-images/issues/5251#issuecomment-1071030822
Further reading: Changing PowerShell's default output encoding to UTF-8
Set Environment Variable using Powershell
How to use github variables with powershell 7?
powershell - Setting environment variable value from .ps1 script not working in Github Actions - Stack Overflow
Environment variables value pass to powershell script in prep command
Videos
I have the following workflow, just to test. But all it does is fail. Any help is appreciated:
name: Test Run
on: [push, pull_request]
jobs:
Updates:
name: Run Test Script
runs-on: ubuntu-latest
steps:
- name: Test
shell: pwsh
run: gci {{ GITHUB_WORKSPACE }}Doesn't work though on my local runner.
set-env was depricated - please check GitHub Actions: Deprecating set-env and add-path commands
As a replacement you may use
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
and then:
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Is variable exported?
run: |
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
- name: PowerShell script
# You may pin to the exact commit or the version.
# uses: Amadevus/pwsh-script@25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
uses: Amadevus/[email protected]
continue-on-error: true
with:
# PowerShell script to execute in Actions-hydrated context
script: |
Write-Host $env:BUILD_NUMBER
- name: Read exported variable
run: |
echo "${{ env.BUILD_NUMBER}}"
To set environment variables in a step that can be referenced in another, you will need to use the ::set-env syntax.
In your case, your first script will have to run this command:
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
And the second script should be able to reference it with $env:BUILD_NUMBER.
[6/20/20] Update with full example.
Action yaml file (Inline powershell will have similar behavior than with a ps1):
name: StackOverFlow
on:
push:
branches: [ master ]
jobs:
build:
runs-on: windows-latest
steps:
- run: |
$buildNumber = "12345"
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
- run: Write-Output "Doing something else..."
- run: Write-Output "The build number is $env:BUILD_NUMBER"
Output logs:
2020-06-20T23:13:23.3209811Z ##[section]Starting: Request a runner to run this job
2020-06-20T23:13:23.5144969Z Can't find any online and idle self-hosted runner in current repository that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145013Z Can't find any online and idle self-hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145038Z Found online and idle hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.6348644Z ##[section]Finishing: Request a runner to run this job
2020-06-20T23:13:29.9867339Z Current runner version: '2.263.0'
2020-06-20T23:13:29.9982614Z ##[group]Operating System
2020-06-20T23:13:29.9983190Z Microsoft Windows Server 2019
2020-06-20T23:13:29.9983380Z 10.0.17763
2020-06-20T23:13:29.9983515Z Datacenter
2020-06-20T23:13:29.9983691Z ##[endgroup]
2020-06-20T23:13:29.9983875Z ##[group]Virtual Environment
2020-06-20T23:13:29.9984067Z Environment: windows-2019
2020-06-20T23:13:29.9984247Z Version: 20200608.1
2020-06-20T23:13:29.9984524Z Included Software: https://github.com/actions/virtual-environments/blob/win19/20200608.1/images/win/Windows2019-Readme.md
2020-06-20T23:13:29.9984752Z ##[endgroup]
2020-06-20T23:13:29.9985890Z Prepare workflow directory
2020-06-20T23:13:30.0151643Z Prepare all required actions
2020-06-20T23:13:30.9154166Z ##[group]Run $buildNumber = "12345"
2020-06-20T23:13:30.9154566Z [36;1m$buildNumber = "12345"[0m
2020-06-20T23:13:30.9154784Z [36;1mWrite-Output "::set-env name=BUILD_NUMBER::$buildNumber"[0m
2020-06-20T23:13:30.9820753Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:30.9821156Z ##[endgroup]
2020-06-20T23:13:43.2981407Z ##[group]Run Write-Output "Doing something else..."
2020-06-20T23:13:43.2981812Z [36;1mWrite-Output "Doing something else..."[0m
2020-06-20T23:13:43.3022226Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.3022501Z env:
2020-06-20T23:13:43.3022706Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.3022906Z ##[endgroup]
2020-06-20T23:13:43.8091340Z Doing something else...
2020-06-20T23:13:43.8671648Z ##[group]Run Write-Output "The build number is $env:BUILD_NUMBER"
2020-06-20T23:13:43.8671986Z [36;1mWrite-Output "The build number is $($env:BUILD_NUMBER)"[0m
2020-06-20T23:13:43.8717102Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.8717288Z env:
2020-06-20T23:13:43.8718175Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.8718286Z ##[endgroup]
2020-06-20T23:13:44.4148124Z The build number is 12345
2020-06-20T23:13:44.4368449Z Cleaning up orphan processes
The original answer to this question used the Actions runner function set-env. Due to a security vulnerability set-env is being deprecated and should no longer be used.
This is the new way to set environment variables.
name: my workflow
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
- name: Test
run: echo $GITHUB_SHA_SHORT
Setting an environment variable
echo "{name}={value}" >> $GITHUB_ENVCreates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.
(From https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable)
Example using the output to $GITHUB_ENV method:
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
This is an alternative way to reference the environment variable in workflows.
- name: Test
run: echo ${{ env.GITHUB_SHA_SHORT }}
- name: Set and Retrieve Github ENV variables
shell: bash
run: |
# define variables
tests=16
failures=2
# set them as GitHub ENV variables
echo "Tests=$tests" >> $GITHUB_ENV
echo "Failures=$failures" >> $GITHUB_ENV
# retrieve these GitHub ENV variables
echo "${{ env.Failures }} out of ${{ env.Tests }} tests failed on CI"
Output
2 out of 16 test failed on CI