Skip to content

On trigger two conditions #25763

On trigger two conditions #25763
Dec 29, 2021 · 7 answers

Hello,

Is it possible to use two on trigger conditions, such as:

on:
pull_request:
branches:
- master
paths:
- ‘/.src’
- '!.github/

push:
branches:
- master
paths:
- '
/.src’
- '!.github/

It looks like it passes the parsing, but does it behave as OR or AND condition?

Thanks!

Neither. 😉

Either of the conditions will trigger the workflow separately, so kind of similar to OR, but you’ll get multiple runs if multiple events occur.

Replies

7 suggested answers

Neither. 😉

Either of the conditions will trigger the workflow separately, so kind of similar to OR, but you’ll get multiple runs if multiple events occur.

0 replies
Answer selected

Those are very unrelated events. All events effectively happen in parallel.

My action has extra code that effectively ends a push event if it detects there’s an open pull request.

Workflow:

  <a href="https://github.com/check-spelling/spell-check-this/blob/ad8754db6e49d737690f5005c8f332f069e3fe8d/.github/workflows/spelling.yml" target="_blank" rel="noopener nofollow ugc">github.com</a>

check-spelling/spell-check-this/blob/ad8754db6e49d737690f5005c8f332f069e3fe8d/.github/workflows/spelling.yml

<pre><code class="lang-yml">name: Spell checking

on:
push:
branches: [""]
tags-ignore: ["
"]
pull_request_target:
issue_comment:
types: [created]

jobs:
spelling:
name: Spell checking
permissions:
contents: read
pull-requests: read
outputs:
internal_state_directory: ${{ steps.spelling.outputs.internal_state_directory }}
runs-on: ubuntu-latest
if: "contains(github.event_name, 'pull_request') || github.event_name == 'push'"
concurrency:

This file has been truncated. show original

Action definition:

  <a href="https://github.com/check-spelling/check-spelling/blob/8919d434aab1251e80d394a2a83a00668f4e106b/action.yml#L122-L123" target="_blank" rel="noopener nofollow ugc">github.com</a>

check-spelling/check-spelling/blob/8919d434aab1251e80d394a2a83a00668f4e106b/action.yml#L122-L123

    
      
  1. suppress_push_for_open_pull_request:
  2. description: "If set, do not do work in response to `push` if there is an open `pull` request to the repository for the branch (this assumes there's a `pull_request_target` event configured)."

Code:

  <a href="https://github.com/check-spelling/check-spelling/blob/8919d434aab1251e80d394a2a83a00668f4e106b/check-pull-requests.sh" target="_blank" rel="noopener nofollow ugc">github.com</a>

check-spelling/check-spelling/blob/8919d434aab1251e80d394a2a83a00668f4e106b/check-pull-requests.sh

<pre><code class="lang-sh">#!/bin/bash

. "$spellchecker/common.sh"
if [ $(uname) = "Linux" ]; then
date_to_epoch() {
date -u -d "$1" +'%s'
}
else
date_to_epoch() {
date -u -j -f '%Y-%m-%dT%H:%M:%SZ' "$1" +'%s'
}
fi
timeframe=${timeframe:-60}
time_limit=$(( $timeframe * 60 ))
strip_quotes() {
tr '"' ' '
}

begin_group 'Retrieving open pull requests'
pulls=$temp/pulls.json

This file has been truncated. show original

0 replies

Those can be related events. For example I want to trigger pull_request or merge only if given path conditions are met. If pull_request and files changed in src/.c or merge and files change in src/.c.

Sorry, the original message have the code block messed up:

on:
  pull_request:
    branches:
      - master
    paths:
      - 'src/*.c'
      - '!.github/**'
  push:
    branches:
      - master
    paths:
      - 'src/*.c'
      - '!.github/**'
0 replies

The code is clearer now, but unfortunately your question isn’t. The events and their filters would still be handled completely separately.

0 replies

Sorry for being unclear. The question was about how two conditions per event behave in GitHub Actions, and you basically answered it.

0 replies

So, your question isn’t unreasonable, and afaict the documentation doesn’t spell this out:

  <a href="https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#example-including-branches-and-tags" target="_blank" rel="noopener nofollow ugc">GitHub Docs</a>

Workflow syntax for GitHub Actions - GitHub Docs

A workflow is a configurable automated process made up of one or more jobs. You must create a YAML file to define your workflow configuration.

So, if I were you, I’d file a bug asking them to clarify.

But note, there are two unrelated things you’re asking about, and that isn’t helpful.

  • If a workflow defines two events, both events are treated as reasons to run workflows, and those reasons are independent, if there’s a reason to run that workflow for that event, it will run the jobs with the event to the best of its ability. Independent of any other events that may also be applicable to the workflow. (This is effectively an “OR”.) From my perspective, (and I think airtower-luna’s,) this part is clear.

  • If a workflow defines, more than one oftypes, paths, and branches, I believe that’s an and, however none of the text makes this clear and none of the examples cover it:
    Workflow syntax for GitHub Actions - GitHub Docs
    There’s a hint that it’s an and in that the text describes using them to narrow things down, and it’d only make sense if they actually narrow things down.
    Note that it’s probably and at the category level (types, tags, branches, …) but it’s an or at the level beneath that (so any matching type, any matching tag, any matching branch). – It wouldn’t be unreasonable to file a bug about this side, just don’t mention multiple on: ... cases as that will lead back to the confusion above.

  <a href="https://github.com/github/docs/issues/new?assignees=&amp;labels=content&amp;template=improve-existing-docs.yaml" target="_blank" rel="noopener nofollow ugc">GitHub</a>

Build software better, together

GitHub is where people build software. More than 73 million people use GitHub to discover, fork, and contribute to over 200 million projects.

0 replies

Yes, I meant the part of which event(s) will trigger workflows. Path and branch filters within each event are AND, as far as I know. 🙂

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants