在GitHub Actions中,我可以返回一个值供以后使用吗? [英] In GitHub Actions, can I return back a value to be used as a condition later?

查看:123
本文介绍了在GitHub Actions中,我可以返回一个值供以后使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将GitHub Actions设置为我的一个项目,整个构建过程基本上是一个由环境变量驱动的PowerShell脚本.

I'm setting up GitHub Actions as a CI for one of my projects, and the entire build process is basically a PowerShell script, driven by environment variables.

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

This is both to minimize vendor lock in, and to make sure I can run a build locally with pretty much the same process.

现在,我的构建脚本确定了一些东西并将其放入环境变量中-具体来说,我有一个MH_IS_PROD_BUILD变量,该变量为True或False,并确定我要推送到哪个nuget软件包存储库.

Now, my build script determines some stuff and puts it into environment variables - specifically, I have an MH_IS_PROD_BUILD variable that's either True or False, and determines which nuget package repository I push to.

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

However, when the step that runs the shell is done, the environment variable ceases to exist, as further steps are run in a new environment it seems.

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

What I want to do is something like this (abbreviated):

  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

似乎输出是我所需要的,但这似乎需要创建自定义操作?

It appears that outputs is what I'd need, but that seems to require creation of a custom action?

以上内容当然不起作用(因此询问).所以我想知道,最好的前进方向是什么?

The above does not work, of course (hence asking). So I'm wondering, what is the best way forward?

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

推荐答案

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

I think you can set outputs from Powershell by just echoing them to the console. Powershell has an alias mapping echo to 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 }}

ref: 查看全文

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