From f93315f61ae1813f06ce3dd4e4c9287d9978ed50 Mon Sep 17 00:00:00 2001 From: Sarah Edwards Date: Tue, 1 Mar 2022 10:05:53 -0800 Subject: [PATCH] Add example of a conditional based on event type (#25626) Co-authored-by: Lucas Costi --- .../using-workflows/triggering-a-workflow.md | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/content/actions/using-workflows/triggering-a-workflow.md b/content/actions/using-workflows/triggering-a-workflow.md index fc7326ec1abe..95f9b22e27ce 100644 --- a/content/actions/using-workflows/triggering-a-workflow.md +++ b/content/actions/using-workflows/triggering-a-workflow.md @@ -195,7 +195,11 @@ If you want more granular control than events, event activity types, or event fi ### Using conditionals -You can use conditionals to further control whether jobs or steps in your workflow will run. For example, if you want the workflow to run when a specific label is added to an issue, you can trigger on the `issues labeled` event activity type and use a conditional to check what label triggered the workflow. The following workflow will run when any label is added to an issue in the workflow's repository, but the `run_if_label_matches` job will only execute if the label is named `bug`. +You can use conditionals to further control whether jobs or steps in your workflow will run. + +#### Example using a value in the event payload + +For example, if you want the workflow to run when a specific label is added to an issue, you can trigger on the `issues labeled` event activity type and use a conditional to check what label triggered the workflow. The following workflow will run when any label is added to an issue in the workflow's repository, but the `run_if_label_matches` job will only execute if the label is named `bug`. ```yaml on: @@ -211,7 +215,34 @@ jobs: - run: echo 'The label was bug' ``` -For more information, see "[Expressions](/actions/learn-github-actions/expressions)." +#### Example using event type + +For example, if you want to run different jobs or steps depending on what event triggered the workflow, you can use a conditional to check whether a specific event type exists in the event context. The following workflow will run whenever an issue or pull request is closed. If the workflow ran because an issue was closed, the `github.event` context will contain a value for `issue` but not for `pull_request`. Therefore, the `if_issue` step will run but the `if_pr` step will not run. Conversely, if the workflow ran because a pull request was closed, the `if_pr` step will run but the `if_issue` step will not run. + +```yaml +on: + issues: + types: + - closed + pull_request: + types: + - closed + +jobs: + state_event_type: + runs-on: ubuntu-latest + steps: + - name: if_issue + if: github.event.issue + run: | + echo An issue was closed + - name: if_pr + if: github.event.pull_request + run: | + echo A pull request was closed +``` + +For more information about what information is available in the event context, see "[Using event information](#using-event-information)." For more information about how to use conditionals, see "[Expressions](/actions/learn-github-actions/expressions)." {% ifversion fpt or ghae or ghes > 3.1 or ghec %}