【问题标题】:Setting environment variable value from .ps1 script not working in Github Actions从 .ps1 脚本设置环境变量值在 Github Actions 中不起作用
【发布时间】:2020-10-10 04:12:21
【问题描述】:

我在 Github Actions 中有两个 ps1 脚本。 我的场景:

  1. 第一个脚本在构建之前执行
  2. 项目构建
  3. 第二个脚本在构建后执行。

我需要在第一个脚本中设置值并在第二个脚本中使用它。 所以我决定使用BUILD_NUMBER 环境变量并将其设置为10 作为默认值。

jobs:
  Droid:
    runs-on: windows-latest
    env:
      BUILD_NUMBER: "10"

在第一个脚本中,我尝试以多种方式设置此变量,但在第二个脚本中,BUILD_NUMBER 的值是 10。

我尝试设置它:

[Environment]::SetEnvironmentVariable($env:BUILD_NUMBER, $buildNumber, 'Machine')

$env:BUILD_NUMBER: '123'

但在第二个脚本中,我通过 $newName = "${env:BUILD_NUMBER}" 获得了 10 个值

Github Actions 端的完整代码:

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: 
    - 'master'
    - 'develop'
    - 'feature/*'
    - 'rc/*'
  pull_request:
    branches: 
    - 'master'
    - 'develop'
    - 'feature/*'
    - 'rc/*'


jobs:
  Droid:
    runs-on: windows-latest
    env:
      DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
      BUILD_NUMBER: "10"

    steps:
    - uses: actions/checkout@v1
    - name: Run a calculate version and set sign in password script
      run: .\Scripts\CalculateVersionAndSetSignPassword.ps1
      shell: powershell

# Build goes here. It is skipped by me for testing purposes

    - uses: actions/checkout@v1
    - name: Run a change apk name script
      run: |
       .\Scripts\ChangeApkName.ps1
      shell: powershell

【问题讨论】:

  • 您是否在这两个脚本之间重新启动?环境变量在启动时加载,因此如果您更改现有变量,则不必重新启动,但如果您创建新变量,则需要重新启动才能使变量可用。
  • @NekoMuseme 如果我理解正确的话,我似乎不会在这两个步骤之间重新开始
  • 从 Powershell 初始化新的环境变量需要重启。尝试改用cmd /c "setx name value"
  • 我不需要初始化一个新的。我需要为第一个脚本中的现有值设置一个值。我应该试试你的解决方案吗?
  • 如果您要更改现有的,只需使用$env:name = "value" 而不是[Environment]::SetEnvironmentVariable($env:BUILD_NUMBER, $buildNumber, 'Machine')

标签: powershell environment-variables arguments github-actions


【解决方案1】:

set-env 已被贬低 - 请检查 GitHub Actions: Deprecating set-env and add-path commands

作为替代品,您可以使用

echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV

然后:

jobs:
  show:
    runs-on: ubuntu-latest
    steps:
      - name: Is variable exported?
        run: |
          echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
      - name: PowerShell script
        # You may pin to the exact commit or the version.
        # uses: Amadevus/pwsh-script@25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
        uses: Amadevus/pwsh-script@v2.0.0
        continue-on-error: true
        with: 
          # PowerShell script to execute in Actions-hydrated context
          script: | 
            Write-Host $env:BUILD_NUMBER
      - name: Read exported variable
        run: |
          echo "${{ env.BUILD_NUMBER}}"

【讨论】:

  • 我相信 Windows 上的 Powershell 应该是 echo "BUILD_NUMBER=yellow" >> $env:GITHUB_ENV
【解决方案2】:

在 Michael Stum 的 repo 中找到了他在this question 中提供的解决方案: 密钥是 .yml 中的 Get-ChildItem Env: | Where-Object {$_.Name -Match "^MH_"} | %{ echo "::set-output name=$($_.Name)::$($_.Value)" } 和他存储库中 .ps1 脚本文件中的 $Env:MH_BUILD_VERSION = $version。 所以我成功地从 .ps1 脚本中检索到一个输出,并在 Github Actions 中使用它。

【讨论】:

    【解决方案3】:

    要在一个步骤中设置可以在另一个步骤中引用的环境变量,您需要使用::set-env syntax

    在你的情况下,你的第一个脚本必须运行这个命令:

    Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
    

    第二个脚本应该可以用$env:BUILD_NUMBER引用它。


    [6/20/20] 更新完整示例。

    动作 yaml 文件(内联 powershell 将具有与 ps1 类似的行为):

    name: StackOverFlow
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      build:
        runs-on: windows-latest
    
        steps:
        - run: | 
            $buildNumber = "12345"
            Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
    
        - run: Write-Output "Doing something else..."
          
        - run: Write-Output "The build number is $env:BUILD_NUMBER"
    

    输出日志:

    2020-06-20T23:13:23.3209811Z ##[section]Starting: Request a runner to run this job
    2020-06-20T23:13:23.5144969Z Can't find any online and idle self-hosted runner in current repository that matches the required labels: 'windows-latest'
    2020-06-20T23:13:23.5145013Z Can't find any online and idle self-hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
    2020-06-20T23:13:23.5145038Z Found online and idle hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
    2020-06-20T23:13:23.6348644Z ##[section]Finishing: Request a runner to run this job
    2020-06-20T23:13:29.9867339Z Current runner version: '2.263.0'
    2020-06-20T23:13:29.9982614Z ##[group]Operating System
    2020-06-20T23:13:29.9983190Z Microsoft Windows Server 2019
    2020-06-20T23:13:29.9983380Z 10.0.17763
    2020-06-20T23:13:29.9983515Z Datacenter
    2020-06-20T23:13:29.9983691Z ##[endgroup]
    2020-06-20T23:13:29.9983875Z ##[group]Virtual Environment
    2020-06-20T23:13:29.9984067Z Environment: windows-2019
    2020-06-20T23:13:29.9984247Z Version: 20200608.1
    2020-06-20T23:13:29.9984524Z Included Software: https://github.com/actions/virtual-environments/blob/win19/20200608.1/images/win/Windows2019-Readme.md
    2020-06-20T23:13:29.9984752Z ##[endgroup]
    2020-06-20T23:13:29.9985890Z Prepare workflow directory
    2020-06-20T23:13:30.0151643Z Prepare all required actions
    2020-06-20T23:13:30.9154166Z ##[group]Run $buildNumber = "12345"
    2020-06-20T23:13:30.9154566Z [36;1m$buildNumber = "12345"[0m
    2020-06-20T23:13:30.9154784Z [36;1mWrite-Output "::set-env name=BUILD_NUMBER::$buildNumber"[0m
    2020-06-20T23:13:30.9820753Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
    2020-06-20T23:13:30.9821156Z ##[endgroup]
    2020-06-20T23:13:43.2981407Z ##[group]Run Write-Output "Doing something else..."
    2020-06-20T23:13:43.2981812Z [36;1mWrite-Output "Doing something else..."[0m
    2020-06-20T23:13:43.3022226Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
    2020-06-20T23:13:43.3022501Z env:
    2020-06-20T23:13:43.3022706Z   BUILD_NUMBER: 12345
    2020-06-20T23:13:43.3022906Z ##[endgroup]
    2020-06-20T23:13:43.8091340Z Doing something else...
    2020-06-20T23:13:43.8671648Z ##[group]Run Write-Output "The build number is $env:BUILD_NUMBER"
    2020-06-20T23:13:43.8671986Z [36;1mWrite-Output "The build number is $($env:BUILD_NUMBER)"[0m
    2020-06-20T23:13:43.8717102Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
    2020-06-20T23:13:43.8717288Z env:
    2020-06-20T23:13:43.8718175Z   BUILD_NUMBER: 12345
    2020-06-20T23:13:43.8718286Z ##[endgroup]
    2020-06-20T23:13:44.4148124Z The build number is 12345
    2020-06-20T23:13:44.4368449Z Cleaning up orphan processes
    

    【讨论】:

    • 不幸的是它没有帮助。第二个脚本仍然获取旧的默认值 BUILD_NUMBER
    • @Lukas 有什么想法吗?
    • 您可以尝试删除您设置为 10 的 yml 行吗?它可能会在加载最后第二步时覆盖该值...
    • @Lukas 我在这里找到了答案。关键是 .yml 中的 Get-ChildItem Env: | Where-Object {$_.Name -Match "^MH_"} | %{ echo "::set-output name=$($_.Name)::$($_.Value)" } 和他存储库中 .ps1 脚本文件中的 $Env:MH_BUILD_VERSION = $version。感谢您的帮助。
    • 你应该为你的问题写一个答案,以防其他人遇到类似的事情:)
    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 2022-11-20
    • 2020-10-03
    • 2022-01-28
    • 1970-01-01
    • 2021-12-25
    • 2014-01-29
    • 2012-01-19
    相关资源
    最近更新 更多