Skip a GitHub Action workflow that is run on successful completion of previous workflow, when the initial trigger was a pull request containing 'WIP'

huangapple go评论40阅读模式
英文:

Skip a GitHub Action workflow that is run on successful completion of previous workflow, when the initial trigger was a pull request containing 'WIP'

问题

背景 / 上下文

我们有两个 GitHub Action 工作流程,1. "构建、测试和打包" 和 2. "部署到 UAT"。

工作流程 (1) 是基于以下触发条件启动的:

# 在拉取请求(更改或推送)或标签(用于发布)时运行
on:
  pull_request:
  release:

并构建我们的软件,运行单元测试等,并打包 Docker 容器,准备在环境中运行。

然后,工作流程 (2) 在工作流程 (1) 成功完成后触发:

on:
  # 当构建工作流程完成时(不一定是成功的 - 需要下面的 IF 条件)
  # 注意:这总是作为来自主分支的运行
  workflow_run:
    workflows: ["构建、测试和打包"]
    types:
      - completed

  # 或手动
  # 注意:这总是作为来自主分支的运行
  workflow_dispatch:
    inputs:
      PACKAGE_VERSION:
        description: "PACKAGE_VERSION"
        required: false

此外,这还需要在工作流程中的第一步上使用 'if' 来排除对工作流程 (1) 的 不成功 执行的运行:

if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}

问题

我想要做的是,在初始拉取请求中,如果包含 "WIP",则不运行部署工作流程 (2),而仍然允许我们的 CI 管道运行以验证所有拉取请求的测试,即使它们是 "WIP"。这是为了避免污染我们的 UAT 环境,除了当前指定的 'current' PR 之外的任何构建。

这是否可行?

英文:

Background / Context

We have two GitHub Action workflows 1. "Build, Test & Package" and 2. "Deploy to UAT".

Workflow (1) is triggered based on:

# runs on pull request (change or push) or tag (for release)
on:
  pull_request:
  release:

And builds our software, runs unit tests etc and packages up docker containers ready to be run in an environment.

Workflow (2) is then triggered on successful completion of workflow (1):

on:
  # when the build workflow finished (not necessarily succeeded - that needs the IF below)
  # NOTE: this *always* runs as if from the master branch
  workflow_run:
    workflows: ["Build, Test & Package"]
    types:
      - completed

  # or manually
  # NOTE: this *always* runs as if from the master branch
  workflow_dispatch:
    inputs:
      PACKAGE_VERSION:
        description: "PACKAGE_VERSION"
        required: false

Which also needs an 'if' on the first step in the workflow to exclude running for unsuccessful executions of workflow (1).

if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}

Question

What I'd like to do, is EXCLUDE running the deployment workflow (2) when the initial pull request that kicks off the whole pipeline starting with (1) has "WIP" in its name somewhere.

Basically this is to avoid polluting our UAT environment with any build other than a current nominated 'current' PR, whilst still allowing our CI pipeline to run to verify tests etc for ALL pull requests even if they are 'WIP'.

Is this possible?

答案1

得分: 2

只翻译代码部分:

# 在拉取请求(更改或推送)或标签(发布)上运行
on:
  pull_request:
  release:

jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: 设置 WIP 变量
        run: |
          if [[ ${{ github.event.pull_request.title }} == *"WIP"* ]]; then
            echo "拉取请求标题中包含 'WIP'"
            echo "wip=true" >> $GITHUB_OUTPUT
          else
            echo "拉取请求标题中不包含 'WIP'"
            echo "wip=false" >> $GITHUB_OUTPUT
          fi          
        id: wip

第二个作业:

  my-other-job:
    needs: my-job
    if: needs.my-job.outputs.wip == 'false'
    runs-on: ubuntu-latest
    steps:
      - name: 做其他事情
        run: |
                    echo "只有当 WIP 为 false 时才会运行此作业"

在第一和第二作业分开的工作流程中,您需要在第一步骤中使用像 "设置环境变量" 这样的操作:

  • 记录您的环境变量:
echo "wip=true" > ./.github/variables/myvars.env

然后在第二步骤中,还原您记录的环境变量:

- name: 设置环境变量
  uses: tw3lveparsecs/github-actions-setvars@latest
  with:
    envFilePath: ./.github/variables/myvars.env
英文:

Since you can get the title of a Pull Request, that means you can also set a variable (IE "set an output parameter") to 'true' in (1), your first job.

And you can use that same variable in (2), as a condition for its execution.

# runs on pull request (change or push) or tag (for release)
on:
  pull_request:
  release:


jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: Set WIP variable
        run: |
          if [[ ${{ github.event.pull_request.title }} == *"WIP"* ]]; then
            echo "WIP is in the pull request title"
            echo "wip=true" >> $GITHUB_OUTPUT
          else
            echo "WIP is not in the pull request title"
            echo "wip=false" >> $GITHUB_OUTPUT
          fi          
        id: wip

And for the second job:

  my-other-job:
    needs: my-job
    if: needs.my-job.outputs.wip == 'false'
    runs-on: ubuntu-latest
    steps:
      - name: Do something else
        run: |
                    echo "This job will only run if WIP is false"

But if those two jobs are in separate workflows, you will need to use in (1) an action like "Set environment variables"

  • record your environment variables:
    echo "wip=true" > ./.github/variables/myvars.env

Then in (2), restore you recorded environment variables:

- name: Set Environment Variables
  uses: tw3lveparsecs/github-actions-setvars@latest
  with:
    envFilePath: ./.github/variables/myvars.env

huangapple
  • 本文由 发表于 2023年2月14日 00:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定