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

ROH*_*SAL 8 version-control git-tag github-actions

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

首先创建一个 VERSION 文件作为

1.1.3 20 Apr, 2022
Run Code Online (Sandbox Code Playgroud)

创建了一个工作流程作为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'
Run Code Online (Sandbox Code Playgroud)

然后创建另一个工作流程作为 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
          
Run Code Online (Sandbox Code Playgroud)

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

Gui*_*urd 8

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

该文档非常详细地介绍了如何使用它们,但我将在此处添加一些也可以提供帮助的参考资料:

如您所见,您可以在步骤中直接使用 Github API(通过 CURL 请求)或使用 Github Marketplace 中执行相同操作的某些操作来触发这些事件。

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

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

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