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

问题描述 投票:0回答:3

是否有一种 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
3个回答
131
投票

set-output
可用于定义步骤的输出。然后,输出可以在后续步骤中使用,并在
with
env
输入部分中进行评估。此外,返回输出的步骤应该有一个
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 的最新方法。

注意: 如需在不同工作之间共享价值观,请参阅此问题


38
投票

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 }}"

0
投票

如果有人无法在Windows上设置环境变量,语法略有不同,我花了很多时间试图找出为什么我无法让它工作,希望如果你有同样的情况,这会节省时间。

如何在 Windows 上设置环境变量以在操作之间共享:

$date = Get-Date -Format 'yyyy-MM-dd-HH-mm'
$zipFileName = "your_zip_file_$date.zip"
echo "ZIPFILE=$zipFileName" >> $env:GITHUB_ENV

之后,您可以在其他操作中访问它,例如:

${{ env.ZIPFILE }}
© www.soinside.com 2019 - 2024. All rights reserved.