当且仅当PR被合并且在主分支时执行workflow_call。

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

How to execute a workflow_call only when the PR is Merged and in the master?

问题

我创建了一个名为ci-cd的流程,在其中包含以下代码:

name: 工作流控制器 [CI/CD]

on: [workflow_dispatch, pull_request]

jobs:
  linters:
    uses: ./.github/workflows/linters.yml
  deploy:
    needs: linters
    uses: ./.github/workflows/githubpages.yml

在这段代码中,我想要实现以下功能:每当打开一个pull_request时,它将运行linters来检查是否一切正常。如果通过了这个测试,就会将PR合并到主分支中。

在将PR合并到主分支时,必须运行github页面部署流程。

英文:

I created a flow called ci-cd, in this guy we have the following code:

name: WORKFLOW CONTROLLER [CI/CD]

on: [workflow_dispatch, pull_request]

jobs:
  linters:
    uses: ./.github/workflows/linters.yml
  deploy:
    needs: linters
    uses: ./.github/workflows/githubpages.yml

In this code I want to do the following, whenever a pull_request is opened it will run the linters to check if everything is ok, passing this test, the merge to the PR in the master is released.

When merging the PR into the master, it must run the github pages deploy flow.

答案1

得分: 1

你可以通过在工作流文件的 on 键中使用 pull_request 事件 并使用 types 字段来实现你的用例。types 字段可用于指定触发工作流的 pull_request 事件的活动类型。

workflow_call 是 GitHub Actions 工作流文件中可用的一种事件类型,可允许另一个工作流调用它。

uses 关键字在工作流文件中作业内通常用于引用特定操作,而不是另一个工作流。

如果你想让 linters 和 deploy 工作流作为 CI/CD 工作流的一部分触发,你可以将它们定义为可重用的工作流,然后可以使用 uses 关键字在主要的 CI/CD 工作流中调用这些工作流。

name: WORKFLOW CONTROLLER [CI/CD]

on: 
  pull_request:
    types: [opened, synchronize, closed]

jobs:
  linters:
    uses: org/repo/.github/workflows/linters.yml@branch
    if: github.event_name == 'pull_request' && github.event.action != 'closed'
    
  deploy:
    uses: org/repo/.github/workflows/githubpages.yml@branch
    if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
    needs: linters

对于 org/repo/.github/workflows/linters.yml@branchorg/repo/.github/workflows/githubpages.yml@branch,这些是可重用工作流所在的实际仓库和分支的占位符。

但是,根据 这个回答,你不能从作业步骤中调用可重用工作流。

英文:

Your use case can be achieved by using the pull_request event with the types field for the on key in the workflow file. The types field can be used to specify which activity types for the pull_request event should trigger the workflow.

workflow_call is a type of event that can be used in a GitHub Actions workflow file to allow for another workflow to call it.

The uses keyword within a job in a workflow file is typically used to refer to a specific action, not another workflow.

If you want to have the linters and deploy workflows triggered as part of the CI/CD workflow, you could define them as reusable workflows, and then you can use the uses keyword to call these workflows in your main CI/CD workflow.

name: WORKFLOW CONTROLLER [CI/CD]

on: 
  pull_request:
    types: [opened, synchronize, closed]

jobs:
  linters:
    uses: org/repo/.github/workflows/linters.yml@branch
    if: github.event_name == 'pull_request' && github.event.action != 'closed'
    
  deploy:
    uses: org/repo/.github/workflows/githubpages.yml@branch
    if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
    needs: linters

With org/repo/.github/workflows/linters.yml@branch and org/repo/.github/workflows/githubpages.yml@branch placeholders for the actual repository and branch where your reusable workflows are located.

However, from this answer, you cannot call a reusable workflow from a job step.

huangapple
  • 本文由 发表于 2023年5月22日 02:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301377.html
匿名

发表评论

匿名网友

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

确定