10

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

Airn5475
  • 2,452
  • 29
  • 51

3 Answers3

7

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
[...]
Marcin Kłopotek
  • 5,271
  • 4
  • 31
  • 51
  • 1
    To your second point: I am checking out two repositories. The first, which is where the action is executing from, will choose the right branch. However, the second repository is using `Master`. Is that what you would expect? – Airn5475 Nov 04 '20 at 13:27
  • I know that I had tried that before, but maybe I had something else screwed up. That worked this time! Actions are exhausting. – Airn5475 Nov 04 '20 at 16:31
  • This doesn't work. I get `Unrecognized named-value: 'env'` Although, I'm trying to call it from a Job calling a reusable workflow. – Brig Mar 09 '22 at 15:34
2

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"

zinen
  • 78
  • 8
1

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 }}
Airn5475
  • 2,452
  • 29
  • 51