简体   繁体   English

Github 操作条件触发器

[英]Github Actions Conditional Trigger

I need help figuring something, I am trying to trigger 2 different workflows based on 2 different release tags.我需要帮助解决一些问题,我正在尝试根据 2 个不同的发布标签触发 2 个不同的工作流程。 I want prod-* to trigger the production workflow and dev-* for the development workflow.我希望 prod-* 触发生产工作流,而 dev-* 触发开发工作流。

The problem is both tags trigger both workflows and I have no idea how to fix this问题是两个标签都触发了两个工作流程,我不知道如何解决这个问题

在此处输入图片说明

(I've canceled both actions but they triggered as you can see) (我已经取消了这两个操作,但如您所见,它们已触发) 在此处输入图片说明

The published trigger is used in both cases here (independently of the tag), because your workflow will start for published OR tags (with the informed pattern) events.此处的两种情况都使用published触发器(独立于标签),因为您的工作流将针对已发布的OR标签(具有知情模式)事件启动。

To perform an operation only for a specific tag, you would have to extract the tag version from the $GITHUB_RE (env-var), for example using a step as below with an output in a first job:要仅对特定标签执行操作,您必须从 $GITHUB_RE (env-var) 中提取标签版本,例如使用以下步骤和第一个作业中的输出:

- name: Get the version 
  id: get_tag_version 
  run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\//}

And then use an if condition on 2 others jobs to check if the tag version contains prod- or dev- (needing the first job) to perform the operation you want for each scenario.然后在其他 2 个作业上使用if condition来检查标签版本是否包含prod-dev- (需要第一个作业)来为每个场景执行您想要的操作。

Here is an complete example of what could be used:这是可以使用的完整示例:

name: Example

on:
  release:
    types: [published]

jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      tag_version: ${{ steps.get_tag_version.outputs.version }}
    steps:
     - name: Get the version 
       id: get_tag_version 
       run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\//}

   job2: # will be executed on for dev- tag
    runs-on: ubuntu-latest
    needs: [job1]
    if: contains( needs.job1.outputs.tag_version , 'dev-')
    steps:
     [...]

   job3: # will be executed on for prod- tag
    runs-on: ubuntu-latest
    needs: [job1]
    if: contains( needs.job1.outputs.tag_version , 'prod-')
    steps:
     [...]
   

I coded a workflow to test the implementation above and it worked as expected creating a prod-2 release tag:我编写了一个工作流来测试上面的实现,它按预期工作,创建了一个prod-2发布标签:

例子

EDIT: Note that you could also use a startWith instead of a contains function for the if expression.编辑:请注意,您还可以对 if 表达式使用startWith而不是contains函数。

The tags element is not valid for release events. tags元素对release事件无效。 In consequence, the workflow is triggered for every release event of type published no matter the tag.其结果是,工作流程触发每个release发布,无论标签类型的事件。 There is no direct filter for tags with the release event as there is for push and pull_request events.没有像pushpull_request事件那样直接过滤带有release事件的标签。

So you can leverage the if conditional on jobs in combination with the github.ref in the context which contains the tag of the release .因此,您可以在包含release 标签的上下文中结合github.ref结合作业的if条件。

name: Deploy

on:
  release:
    types: [published]

jobs:
  deploy-dev:
    name: Deploy to development
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/dev-')
    steps:
      # [...]

  deploy-prod:
    name: Deploy to production
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/prod-')
    steps:
      # [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM