将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用 [英] Passing variable argument to .ps1 script not working from Github Actions

查看:27
本文介绍了将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

##############################################
#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 脚本中显示分支名称.因为分支名称取决于很多脚本逻辑.因此,它不显示任何内容.但是:

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

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

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

推荐答案

在Powershell中访问环境变量与bash不同,需要在前面加上env:前缀.对于您的脚本,您可以使用 $env:GITHUB_REF 访问引用.

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.

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

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.

这篇关于将变量参数传递给 .ps1 脚本在 Github Actions 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆