Pass variable into step using PowerShell

I am trying to set up a variable in one step and use it in other steps.
I’m using Windows/PowerShell.
I saw documentation how things changed with setting variables so I’m not sure which is correct!
In the first step, I’m setting it.
In the second step, I’m just trying to output it
In the third step I’m trying to pass it into the checkout action

- name: Set Variables
  run: |
    echo "Working Path: $($Env:GITHUB_WORKSPACE)"
    $branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
    echo "Branch: ${branchName}"
    echo "CURRENT_BRANCH=${branchName}" >> $GITHUB_ENV

- name: Output Variables
  run: |
    echo "${env:CURRENT_BRANCH}"

- name: Checkout Repo
  uses: actions/checkout@v2
  with:
    repository: 'MyRepoName'
    ref: '$($Env:CURRENT_BRANCH)'

Thank you for your assistance!

You can use outputs, see:

You can also use environment files. Either way, you cannot use PowerShell code in with. You can use a GitHub Actions expression however.

- name: Set Variables
  run: |
    echo "Working Path: ${Env:GITHUB_WORKSPACE}"
    $branchName = $Env:GITHUB_REF -replace 'refs/heads/', ''
    echo "Branch: ${branchName}"
    echo "CURRENT_BRANCH=${branchName}" >> $Env:GITHUB_ENV

- name: Output Variables
  run: |
    echo "${Env:CURRENT_BRANCH}"

- name: Checkout Repo
  uses: actions/checkout@v2
  with:
    repository: 'MyRepoName'
    ref: ${{ env.CURRENT_BRANCH }}

According to the documentation, you should actually use this for setting the environment variable:

echo "CURRENT_BRANCH=${branchName}" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

The reason is probably because in anything below PowerShell Core (pwsh) version 6 there are issues with character encoding and byte order marks, e.g. if you use >> or Add-Content instead of Out-File -Encoding utf8 -Append.

This is what I get if I try your solution @Simran-B

The workflow is not valid. .github/workflows/project-check.yml (Line: 58, Col: 22): A mapping was not expected

Here’s my code:

    - name: Set env to development
      if: endsWith(github.ref, '/daily')
      run: |
        echo "{environment}={development}" >> $GITHUB_ENV
    - name: Set output
      run: |
        echo "${Env:environment}"

    - name: Notify deploy to Rollbar
      uses: rollbar/github-deploy-action@1.1.0
      id: rollbar_deploy
      with:
        environment: {{ env.environment }}
        version: ${{ github.sha }}
      env:
        ROLLBAR_ACCESS_TOKEN: ${{ secrets.ROLLBAR_ACCESS_TOKEN }}
        ROLLBAR_USERNAME: ${{ github.actor }}

I missed a $ sign, my bad.

The curly braces in the line where you write to $GITHUB_ENV indicate that it’s a placeholder for a value you are supposed to supply. This should work:

    - name: Set env to development
      if: endsWith(github.ref, '/daily')
      run: |
        echo "environment=development" >> $GITHUB_ENV
    - name: Echo env var
      run: |
        echo "${Env:environment}"
    - name: Notify deploy to Rollbar
      uses: rollbar/github-deploy-action@1.1.0
      id: rollbar_deploy
      with:
        environment: ${{ env.environment }}
        version: ${{ github.sha }}
      env:
        ROLLBAR_ACCESS_TOKEN: ${{ secrets.ROLLBAR_ACCESS_TOKEN }}
        ROLLBAR_USERNAME: ${{ github.actor }}
1 Like

@Simran-B Thanks a bunch, It’s working! :white_check_mark: