【问题标题】:In GitHub Actions, can I return back a value to be used as a condition later?在 GitHub Actions 中,我可以返回一个值以供以后用作条件吗?
【发布时间】:2019-11-02 20:53:52
【问题描述】:

我正在将 GitHub Actions 设置为 one of my projects 的 CI,整个构建过程基本上是一个 PowerShell 脚本,由环境变量驱动。

这既是为了最大限度地减少供应商锁定,又是为了确保我可以使用几乎相同的过程在本地运行构建。

现在,我的构建脚本确定了一些内容并将其放入环境变量中 - 具体来说,我有一个 MH_IS_PROD_BUILD 变量,它可以是 True 或 False,并确定我推送到哪个 nuget 包存储库。

但是,当运行 shell 的步骤完成后,环境变量将不复存在,因为似乎在新环境中运行进一步的步骤。

想要做的是这样的事情(缩写):

  steps:
    - name: Run build script
      id: pwshbuild
      shell: pwsh
      run: |
        cd scripts
        ./build.ps1
        # The above sets $Env:MH_IS_PROD_BUILD to either True or False
    - name: Push Publish to GPR (Dev Package)
      if: steps.pwshbuild.outputs.MH_IS_PROD_BUILD == 'False'
      shell: pwsh
      run: |
        # omitted: determine $nupkgPath
        nuget push $nupkgPath -Source "GPR" -SkipDuplicate
    - name: Push Publish to Nuget.org (Release Package)
      if: steps.pwshbuild.outputs.MH_IS_PROD_BUILD == 'True' 
      shell: pwsh
      run: |
        # omitted: determine $nupkgPath
        nuget push $nupkgPath -Source "NugetOrg" -SkipDuplicate

看来outputs 是我需要的,但这似乎需要创建自定义操作?

当然,上述方法不起作用(因此问)。所以我想知道,最好的前进方式是什么?

  • 我可以从 PowerShell 设置步骤的输出吗? (首选)
  • 我是否必须创建一个自定义操作来封装我对 build.ps1 的调用,以便我可以通过输出返回内容?

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    我认为您可以通过将它们回显到控制台来设置 Powershell 的输出。 Powershell 有一个别名映射 echo 到 Write-Output

    jobs:
      windows-test:
        runs-on: windows-latest
        steps:
          - uses: actions/checkout@v1
          - name: Set outputs
            id: vars
            shell: pwsh
            run: echo "::set-output name=production::true"
          - name: Check outputs
            shell: pwsh
            run: echo ${{ steps.vars.outputs.production }}
    

    参考:https://help.github.com/en/github/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-output-parameter-set-output

    【讨论】:

    • 谢谢,这正是我想要的! Works perfectly
    • @peterevans, @Michael Stum 我在我的.ps1 脚​​本中设置$Env:MH_BUILD_OUTPUT = $buildNumber 并尝试从Github Actions 中回显它。 Github Actions 的代码:prnt.sc/t3isxb 与空的$mhPath 相呼应。但是在脚本执行之后,它会打印出带有所需值的MH_BUILD_OUTPUT。我错过了什么?
    • 2021 更新:请记住,此 "${{ steps.vars.outputs.production }}" 返回一个字符串而不是布尔值,因此在有条件的步骤中进行评估时,您应该检查"a == 'true'" 而不是 "a == true"。
    【解决方案2】:

    除了peterevans的回答之外,你仍然可以使用环境变量作为条件,只要它们是通过::set-env“command”设置的

    例子:

    - run:   |
             if [ -f FileMightNotExists.txt ]; then
                echo ::set-env name=HAVE_FILE::true
             fi
      shell: bash
    
    - run: echo "I have file!"
      if:  env.HAVE_FILE == 'true'
    

    如前所述,::set-output 已经存在,所以这主要是个人喜好问题。

    ::set-env 更容易使用的原因(在我看来)是你不需要为步骤设置id(更少的输入),引用变量要短得多(减少再次输入),您添加的所有变量都将在每个步骤中列出(折叠在 Run 块内,在尝试查找工作流中的错误时很有用),嗯......它最终只是常规变量,它可能更容易使用,取决于 shell。

    【讨论】:

    • 谢谢!是的,env 也很好,因为它在未来每个步骤的环境中都可用,而输出必须显式传递到这些步骤 - 这使得它们更“精确”,但不那么自动化。
    • set-env 已被声明为已弃用:github.blog/changelog/…
    • 你现在必须直接使用$GITHUB_ENVdocs.github.com/en/actions/learn-github-actions/…Note: Environment variables must be explicitly referenced using the env context in expression syntax or through use of the $GITHUB_ENV file directly; environment variables are not implicitly available in shell commands.
    猜你喜欢
    • 2019-11-28
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多