【问题标题】:If condition in Github Actions for another Job如果 Github Actions 中的条件用于另一个作业
【发布时间】:2021-06-18 03:06:48
【问题描述】:

我的用例是,当 Pull Request Comments 中有触发词时触发文档构建。 我正在使用pull-request-comment-trigger 来了解代码中是否存在触发词

知道触发了 Action 后,我想在 repo 中运行一些命令。所以,我必须使用actions/checkout

我的疑问是,在run 命令中,只有Shell 命令有效,对吧? 如果满足上述条件,我想再做一份工作。

我当前的 Yaml 文件

name: Testing GH Actions

on:
  pull_request:
    types: [opened]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: khan/pull-request-comment-trigger@master
        id: check
        with:
          trigger: "AppajiC"
          reaction: rocket
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
      - run:
        # Generally Anything here runs if below condition satisfies.
        # I want to run another job here, which uses actions/checkout@v2 action
        if: steps.check.outputs.triggered == 'true'

我怎样才能做到这一点?

【问题讨论】:

  • 您可以在结帐步骤中使用相同的 if 条件。或者,您可以为整个作业创建一个具有 if 条件的新作业,因此您无需为每个步骤重复 if。
  • 你能举个例子吗?

标签: github continuous-integration github-actions


【解决方案1】:

您可以在 checkout 步骤和以下步骤中使用条件:

- name: Checkout
  uses: actions/checkout@v2
  if: steps.check.outputs.triggered == 'true'

- name: Following step1
  if: steps.check.outputs.triggered == 'true'

...

或者,您可以创建一个新作业并使用该 if 条件一次:

jobs:
  deploy:
    runs-on: ubuntu-latest
    outputs:
      deploy-status: ${{ steps.check.outputs.triggered }}
    steps:
      - uses: khan/pull-request-comment-trigger@master
        id: check
        with:
          trigger: 'AppajiC'
          reaction: rocket
        env:
          GITHUB_TOKEN: ${{ github.token }}

  # this job will only run if steps.check.outputs.triggered == 'true'
  # you just need to write the if once
  after-deploy:
    runs-on: ubuntu-latest
    needs: [deploy]
    if: needs.deploy.outputs.deploy-status == 'true'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        
      ...

【讨论】:

  • 感谢您的帮助
猜你喜欢
  • 2022-07-10
  • 2021-04-03
  • 2021-05-13
  • 2020-10-16
  • 2020-01-08
  • 2021-08-28
  • 2020-05-02
  • 2020-10-26
  • 2021-09-19
相关资源
最近更新 更多