set-output https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter可用于定义步骤的输出。然后可以在后续步骤中使用输出并在with
and 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 }}
Update:第一个示例中的 docker 操作已弃用。请参见这个答案 https://stackoverflow.com/questions/58350578/new-location-of-docker-actions/58350591#58350591了解在 GitHub Actions 中使用 docker 的最新方法。
Note:为了在人与人之间分享价值观不同的职位,请参阅这个问题 https://stackoverflow.com/questions/59175332/using-output-from-a-previous-job-in-a-new-one-in-a-github-action.