【问题标题】:Github Actions, how to share a calculated value between job steps?Github Actions,如何在作业步骤之间共享计算值?
【发布时间】:2020-01-09 04:27:37
【问题描述】:

是否有一种 DRY 方法可以使用 Github Actions 在多个作业步骤中计算和共享值?

在下面的工作流 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 github-actions


【解决方案1】:

set-output 可用于定义步骤的输出。然后可以在后续步骤中使用输出,并在 withenv 输入部分进行评估。

以下是您的示例的样子。

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 ::set-output name=docker_tag::$(echo ${GITHUB_REF} | cut -d'/' -f3)-${GITHUB_SHA}
      - 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: |
          echo ::set-output name=pr_title::"[Test] Add report file $(date +%d-%m-%Y)"
          echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v2
        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@v2
        with:
          title: ${{ env.PR_TITLE }}
          body: ${{ env.PR_BODY }}

更新:第一个示例中的 docker 操作已弃用。请参阅this answer,了解在 GitHub Actions 中使用 docker 的最新方法。

注意:要在不同工作之间共享价值,请参阅this question

【讨论】:

  • 需要明确的是,这些解决方案在不同工作的步骤之间不起作用
  • 此解决方案是否已弃用? github.blog/changelog/…
  • @Sandburg 不,它们没有被弃用。这些解决方案展示了当前创建步骤输出和环境变量的方法。
  • @grokpot 跨不同作业的输出似乎也受支持,但您需要将步骤输出转发到作业输出:docs.github.com/en/actions/using-workflows/…
猜你喜欢
  • 2021-04-10
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多