【问题标题】:Passing variable argument to .ps1 script not working from Github Actions将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用
【发布时间】:2020-10-05 03:34:51
【问题描述】:

我需要使用 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}"Extract branch name 部分完全显示分支名称
  2. 如果我传递像 run: .\Scripts\my_script.ps1 -branch_name customtext 这样的硬编码参数,它将被传递给脚本并以不同的方式成功显示。

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

【问题讨论】:

    标签: powershell environment-variables arguments github-actions


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2019-11-06
      • 1970-01-01
      相关资源
      最近更新 更多