gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:34:37 25 4
gpt4 key购买 nike

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

最佳答案

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
这是另一个示例,显示了如何动态设置 Action 要使用的多个变量。
      - 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 - Github Actions,如何在工作步骤之间共享计算值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57819539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com