如何在拉取请求关闭而不合并时不运行作业

问题描述 投票:0回答:2

我有一个包含 2 个作业的工作流程,一个作业应在创建没有标签的拉取请求时运行 Terraform Plan,另一个作业应在合并拉取请求且没有标签时运行 Terraform Apply。目前我的工作流程在这两种情况下都在运行。我遇到的问题是,当拉取请求关闭而不合并时,Terraform Plan 也在运行。我只希望 Terraform Plan 在创建没有标签的拉取请求时运行。 如果我遗漏了什么,下面是触发器的片段

name: Plan and merge to Main
on:
  pull_request: 
    types: [ labeled, closed, opened ]

jobs:
  Plan:
    name: "Terraform Plan"'
    if: github.base_ref == 'main' && join(github.event.pull_request.labels.*.name, '') == ''

Apply:
    name: "Run Terraform Apply"
    if: github.base_ref == 'main' && github.event.pull_request.merged == true && join(github.event.pull_request.labels.*.name, '') == ''
github-actions
2个回答
0
投票

无法指定合并拉取请求时应触发工作流程。但是,由于合并的拉取请求始终会导致推送,因此您可以使用推送事件来实现您的目标。

例如,假设您希望在拉取请求合并到主分支时运行工作流程。你可以做这样的事情:

on:
  push:
    branches:
      - main

如果你想阻止直接推送到 main,这是 github 专业计划的一部分。


0
投票

感谢@marOne,这就是我如何仅在合并拉取请求时才能实现部署。

name: Deploy CI
on:
  pull_request:
    branches:
      - master
    types:
      - closed

jobs:
  deploy:
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      # Adicione aqui os passos necessários para o deploy
      - name: Deploy
        run: echo "Deploying..."
© www.soinside.com 2019 - 2024. All rights reserved.