5

I need to pass branch name as a parameter to .ps1 script using Github Actions. Github Actions part:

    runs-on: windows-latest
    env:
      DOTNET_CLI_TELEMETRY_OPTOUT: 'true'

    steps:
    - name: Extract branch name
      shell: bash
      run: echo "${GITHUB_REF}"
      id: extract_branch

    - uses: actions/checkout@v1
    - name: Run a one-line script
      run: .\Scripts\my_script.ps1 -branch_name ${GITHUB_REF}
      shell: powershell

.ps1 part:

##############################################
#Script Title: Download File PowerShell Tool
#Script File Name: Download-File.ps1 
#Author: Ron Ratzlaff  
#Date Created: 4/21/2016 
##############################################

#Requires -Version 3.0
param($branch_name)


"Current branch"
$branch = git rev-parse --abbrev-ref HEAD
"$branch"
"${branch}"
"${GITHUB_REF}"
"$GITHUB_REF"
"$branch_name"
"{branch_name}"
Write-Host $branch_name

.ps1 part is shown by me in order to display branch name inside of the .ps1 script using different ways of displaying. Because of branch name depends a lot of scripts logic. So, it does not display anything. But:

  1. run: echo "${GITHUB_REF}" in Extract branch name part fully displays the branch name
  2. If I pass hardcoded argument like this run: .\Scripts\my_script.ps1 -branch_name customtext it will be passed to the script and successfully displayed in different ways.

How to achieve passing value of GITHUB_REF and read/display it inside .ps1?

1 Answer 1

5

Accessing environment variables in Powershell is different from bash, you need to prefix it with env:. For your script, you can access the ref using $env:GITHUB_REF.

Alternatively, you can use the templating that GitHub Actions expose by replacing the run property with .\Scripts\my_script.ps1 -branch_name ${{github.ref}} to populate branch_name parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.