powershell - 将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用

标签 powershell environment-variables arguments github-actions

我需要使用 Github Actions 将分支名称作为参数传递给 .ps1 脚本。 Github Actions 部分:

    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部分:

##############################################
#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 部分由我展示,以便使用不同的显示方式在 .ps1 脚本中显示分支名称。因为分支名称取决于很多脚本逻辑。 因此,它不显示任何内容。 但是:

  1. run: echo "${GITHUB_REF}" in Extract branch name 部分完整显示分支名
  2. 如果我像这样传递硬编码参数,run: .\Scripts\my_script.ps1 -branch_name customtext 它将被传递到脚本并以不同的方式成功显示。

如何实现GITHUB_REF的传值并在.ps1中读取/显示?

最佳答案

Accessing environment variables in Powershell与bash不同,你需要在它前面加上env:。对于您的脚本,您可以使用 $env:GITHUB_REF 访问 ref。

或者,您可以使用 GitHub Actions 公开的模板,方法是将 run 属性替换为 .\Scripts\my_script.ps1 -branch_name ${{github.ref}} 填充 branch_name 参数。

关于powershell - 将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62393036/

相关文章:

Java jdk - 类路径、java home、PATH 变量已设置,但 javac 命令不起作用

python - 使用 Python 设置 cx_Oracle 环境变量

c++ - 作为函数参数的大小数组的目的是 C 和 C++?

powershell - 如何从 PowerShell 启动远程桌面?

file - Powershell比较两个文件并生成第三个文件

powershell - 如何使用PowerShell远程检查Web应用程序池的状态?

javascript - 在 Javascript 中用参数替换局部变量是否会影响性能?

powershell - 使用Powershell将字符串ISO 8601日期时间标准转换为SQL日期时间?

windows - 如何在 PowerShell 中更改环境变量并启动应用程序

Powershell 参数列表传递类似于 -a <args list> -d <args list>