Skip action when label already on PR vs. on label create event?

The good: We’re successfully triggering actions on labels by filtering on label created events:

on:
  pull_request:
    types: [labeled]
...
if: github.event.label.name == 'do-thingy'

The unclear: When a PR has a specific pre-existing label… we want to opt-out and NOT do the thingy. The confusing part is how to tell what labels already exist on the PR, vs. on the trigger action, so we can check for a preexisting opt-out label.

Ideally something like

### Psuedo-code (does not work, but would like it to!)
on:
  pull_request: #any action on a PR

...
if: not_in(github.pr.labels, 'skip_ci')

… but we can’t figure it out.

Any way to check the active labels on a PR as part of an if check? Or some other way to do this?

@lmeyerov,

You can get the array of the labels from the github context, when a workflow is running on the events for pull request or issue.

  • Expression to get the array of the labels on pull request.
github.event.pull_request.labels
  • Expression to get the array of the labels on issue.
github.event.issue.labels

In your case, you can use the expression “github.event.pull_request.labels.*.name” to just get an array of the label names on the pull request, then use the “contains” function to check if the specified label (skip_ci) is an element in the array.

### Psuedo-code (does not work, but would like it to!)
on:
  pull_request: #any action on a PR

...
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip_ci') }}

The following are the related docs:

1 Like

Worked like a charm, thank you!

@lmeyerov,
You’re welcome. And glad that my suggestion can help you.