GitHub Action:使用PowerShell将环境变量传递给Action

时间:2020-11-03 20:52:59

标签: powershell environment-variables github-actions

我正在尝试构建将在PowerShell中运行的工作流。我正在为我的分支名称设置一个环境,以便在结帐其他存储库的步骤中使用。

run: |
  $branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
  echo "CURRENT_BRANCH=${branchName}" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

在以后的步骤中,我尝试传递变量:

- name: Checkout repo  
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${env:CURRENT_BRANCH}

我尝试了使用大括号的不同格式,但是我不断从构建中获取输出,该输出将确切的文本作为路径。我不确定如何评估它。
当我做${{ env:CURRENT_BRANCH }}时,出现以下错误:

工作流程无效。 .github / workflows / publish.yml(第54行,第14行): 意外的符号:“ env:CURRENT_BRANCH”。位于位置1 在表达式中:env:CURRENT_BRANCH

3 个答案:

答案 0 :(得分:2)

要在GitHub Actions工作流中引用给定上下文(在这种情况下为<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://www.service-now.com/u_hr_is"> <soapenv:Body> <u:insert> <u:u_email_domain>Test.eu</u:u_email_domain> <u:u_cost_center>costcenter123</u:u_cost_center> <u:u_department>ItDepartment</u:u_department> <u:u_family_name>Donald</u:u_family_name> <u:u_first_name>Trump</u:u_first_name> <u:u_hris_id>1000091</u:u_hris_id> <u:u_job_title>Manager</u:u_job_title> <u:u_location>newyork</u:u_location> <u:u_manager_hris_id>10000421</u:u_manager_hris_id> <u:u_vip>NO</u:u_vip> </u:insert> </soapenv:Body>`enter code here` </soapenv:Envelope> )中的变量,我们必须使用点(env)字符,但使用冒号({{1 }}。要解决以上工作流程中的错误,应进行调整:

.

此外,您不必检测当前分支并将其传递给checkout操作。 actions/checkout@v2将默认使用当前分支。因此,您只需要:

:

答案 1 :(得分:0)

我在其他地方找到了一个解决方案,并将其作为选项发布在这里,尽管我想知道在我的场景中是否可能使用环境变量。 解决方案是从步骤中使用Outputs

- name: Output Variables
  id: SetVariables
  run: |
    $branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
    echo "Branch: ${branchName}"
    echo "::set-output name=branch::${branchName}"

- name: Checkout Repo 2
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${{ steps.SetVariables.outputs.branch }}

答案 2 :(得分:0)

这是一种可行的方法,more info here

echo "action_state=yellow" >> $GITHUB_ENV

下面的那个不再对我有用。一些研究表明,这是由于security issue造成的。

echo "::set-output name=action_state::yellow"