Github Actions,如何在作业步骤之间共享计算值?

79

在 Github Actions 中,有没有一种DRY的方式来计算和在多个作业步骤中共享一个值呢?

在下面的工作流程yml文件中,echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA} 在多个步骤中反复出现。

name: Test, Build and Deploy
on:
  push:
    branches:
      - master
jobs:
  build_and_push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Docker Build
        uses: "actions/docker/cli@master"
        with:
          args: build . --file Dockerfile -t cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA}
      - name: Docker Tag Latest
        uses: "actions/docker/cli@master"
        with:
          args: tag cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA} cflynnus/blog:latest

请参阅以下链接以了解有关GitHub Actions的工作流命令的详细信息:https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable 和 https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/。 - 0 _
使用矩阵作业的输出 https://github.com/orgs/community/discussions/25634 - Richard Tyler Miles
2个回答

122

set-output 可以用于为步骤定义输出。这些输出可以在后续步骤中使用,并在withenv输入部分中进行评估。此外,返回输出的步骤应具有一个id,该id由使用输出的步骤引用。

以下是您的示例看起来像什么。

name: Test, Build and Deploy
on:
  push:
    branches:
      - master
jobs:
  build_and_push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Set tag var
        id: vars
        run: echo "docker_tag=$(echo ${GITHUB_REF} | cut -d'/' -f3)-${GITHUB_SHA}" >> $GITHUB_OUTPUT
      - name: Docker Build
        uses: "actions/docker/cli@master"
        with:
          args: build . --file Dockerfile -t cflynnus/blog:${{ steps.vars.outputs.docker_tag }}
      - name: Docker Tag Latest
        uses: "actions/docker/cli@master"
        with:
          args: tag cflynnus/blog:${{ steps.vars.outputs.docker_tag }} cflynnus/blog:latest

这里是另一个示例,展示了如何动态设置多个变量以供操作使用。

      - name: Set output variables
        id: vars
        run: |
          pr_title="[Test] Add report file $(date +%d-%m-%Y)"
          pr_body="This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
          echo "pr_title=$pr_title" >> $GITHUB_OUTPUT
          echo "pr_body=$pr_body" >> $GITHUB_OUTPUT
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          title: ${{ steps.vars.outputs.pr_title }}
          body: ${{ steps.vars.outputs.pr_body }}

另外,您可以创建环境变量。

      - name: Set environment variables
        run: |
          echo "PR_TITLE=[Test] Add report file $(date +%d-%m-%Y)" >> $GITHUB_ENV
          echo "PR_BODY=This PR was auto-generated on $(date +%d-%m-%Y) by [create-pull-request](https://github.com/peter-evans/create-pull-request)." >> $GITHUB_ENV
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          title: ${{ env.PR_TITLE }}
          body: ${{ env.PR_BODY }}

更新:第一个示例中的docker操作已被弃用。请查看此答案,了解在GitHub Actions中使用docker的最新方法。

注意:若需要在不同的作业之间共享值,请参见此问题


14
只是为了明确起见,这些解决方案不适用于“不同工作步骤”之间。 - grokpot
2
这些解决方案已经过时了吗?https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/ - Sandburg
@Sandburg 不,它们并没有被弃用。这些解决方案展示了创建步骤输出和环境变量的当前方式。 - peterevans
@ grokpot 输出在不同的作业中也似乎得到了支持,但您需要将步骤输出转发到作业输出:https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idoutputs - MakisH
2
谢谢!对我来说,不使用引号是答案。echo '::set-output $(date +%s) ...' 不起作用,但 echo ::set-output $(date +%s)... 可以替换值。 - NecipAllef
@NecipAllef 这是因为(一般情况下)在shell中,单引号会防止参数扩展。使用双引号("")也可以起到同样的作用。 - FWDekker

26

set-output已被弃用,现在更好的方法是:

echo "{name}={value}" >> $GITHUB_OUTPUT

来自Github文档的示例:

  - name: Set color
    id: random-color-generator
    run: echo "SELECTED_COLOR=green" >> $GITHUB_OUTPUT
  - name: Get color
    run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"

请注意,"SELECTED_COLOR=green"中没有任何空格在开头,因为我在处理一个输出变量时遇到了开头有空格的问题。 - Bachiri Taoufiq Abderrahman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接