Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Action: Pass Environment Variable to into Action using PowerShell

I am using trying to build a workflow that will run in PowerShell. I am setting an environment for my branch name to use in a step for checkout of a different repository.

run: |
  $branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
  echo "CURRENT_BRANCH=${branchName}" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

In a later step, I'm trying to pass in the variable:

- name: Checkout repo  
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${env:CURRENT_BRANCH}

I've tried different formats involving curly brackets, but I keep getting output from the build that shows that exact text as the path. I'm not sure sure how to get it to evaluate.
When I do ${{ env:CURRENT_BRANCH }} I received the following error:

The workflow is not valid. .github/workflows/publish.yml (Line: 54, Col: 14): Unexpected symbol: 'env:CURRENT_BRANCH'. Located at position 1 within expression: env:CURRENT_BRANCH

like image 558
Airn5475 Avatar asked Nov 03 '20 20:11

Airn5475


People also ask

How do I pass an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

How do I share variables between jobs in actions GitHub?

We don't support to share variables between jobs. As you metioned artifacts, we would suggest you use it . You can write the version into a file , upload it as artifact in one job and then download the artifacts in the other job , read the file content.

What is Github_env?

Setting an environment variable echo "{name}={value}" >> $GITHUB_ENV. Creates 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.


3 Answers

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
[...]
like image 89
Marcin Kłopotek Avatar answered Nov 14 '22 22:11

Marcin Kłopotek


One solution I found elsewhere and will post it here as an option, although I would like to know if using Environment Variables is possibly in my scenario. The solution is to use Outputs from a Step

- name: Output Variables
  id: SetVariables
  run: |
    $branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
    echo "Branch: ${branchName}"
    echo "::set-output name=branch::${branchName}"

- name: Checkout Repo 2
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${{ steps.SetVariables.outputs.branch }}
like image 45
Airn5475 Avatar answered Nov 14 '22 21:11

Airn5475


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"

like image 42
zinen Avatar answered Nov 14 '22 22:11

zinen