如何使用GitHub Actions从另一个工作流触发工作流?



我想从文件中读取版本,并使用工作流创建标记为v11.0.5.1.aws。然后我想在docker图像中使用那个标签。为此,我创建了一个分支devops。

首先创建一个VERSION文件作为

1.1.3 20 Apr, 2022

创建一个工作流作为release-version.yml

name: Release Version
on:
push:
branches:
- devops
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Bump version and push tag
uses: melheffe/version_release_composer@master
env:
PREPEND: 'v'
APPEND: '.aws' # must include '.' or it will append without separation
DRAFT: 'false'
PRERELEASE: 'true'
TOKEN: ${{ secrets.AUTH_TOKEN }}
TRIGGER: ${{ github.event.pull_request.base.ref }} # can use the triggering branch or define a     fixed one like this: 'master'
REPO_OWNER: rohit
VERSION_FILE_NAME: 'VERSION'

然后创建另一个工作流ci。从发布版本工作流中获取标签的Yml

name: CI
# Only trigger, when the build workflow succeeded
on:
workflow_run:
workflows: ["Release Version"]
types:
- completed
jobs:
# This workflow contains a single job called "build"
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
DeployDev:
# Steps represent a sequence of tasks that will be executed as part of the job
name: Deploy to Dev
needs: [Build]
runs-on: ubuntu-latest
environment:
name: Dev
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.AUTH_TOKEN }}
- name: Build, tag, and push image to Amazon ECR
id: build-image
#env:
#  IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and push it to ECR so that it can
# be deployed to ECS.
echo "$GITHUB_REF_NAME"
docker build -t ${{secrets.ECR_REPO_URI}}/${{secrets.REPO_NAME}}:$GITHUB_REF_NAME .
docker push ${{secrets.ECR_REPO_URI}}/${{secrets.REPO_NAME}}:$GITHUB_REF_NAME

我能够在devops分支上进行更改后触发发布版本工作流,但ci工作流在触发发布版本后没有被触发。任何建议都会对我有帮助。

如果workflow_run触发器没有像预期的那样工作,还有其他两种方法可以实现您想要的(从另一个工作流触发一个工作流,从第一个工作流发送一个输入参数以在第二个工作流中使用)。

  • workflow_dispatch事件。
  • repository y_dispatch事件。

关于如何使用它们的文档非常好,但是我将在这里添加一些可以帮助的参考:

  • 使用POST请求触发Github动作(Github REST API)
  • 如何从Github API触发workflow_dispatch?
  • 使用ghCLI触发GitHub工作流

正如你所看到的,你可以在一个步骤中直接使用Github API来触发这些事件(使用CURL请求),或者使用Github Marketplace中的一些操作来执行相同的操作。

下面的答案也解释了这两个事件之间的区别(因为它们很相似,而CURL有效负载的差异可能会令人困惑)

  • 使用客户端有效负载在github操作中运行workflow_dispatch的正确请求

我还将在这里添加一个示例,该示例有助于理解如何使用repository_dispatch事件从另一个工作流接收到第一个工作流的callback:

工作流
  • 工作流B

注意,您还需要使用PAT来使用调度事件触发工作流。

相关内容

最新更新