Denis Rouzaud
Denis Rouzaud

Reputation: 2600

How to check for a label in a github action condition

Let's say I have a github action like this:

name: My Action

on:
  pull_request:
    types:
      - closed

jobs:
  myjob:
    runs-on: ubuntu-latest
    name: Test
    if: github.event.pull_request.merged && XXX

I would like to have a condition here to test the presence of a label.

From the docs, using contains( github.event.pull_request.labels, 'my_label')doesn't seem appropriate since it's a dictionary and not an array.

Is there any way around this?

Upvotes: 32

Views: 25159

Answers (2)

Robert Nyström
Robert Nyström

Reputation: 105

I took inspiration from the answer by Denis Rouzaud above and applied it to Issues instead. It works great and helps me to manage project automation for our Issues that is created by external systems (Using the API):

jobs:
  issue_opened:
    name: issue_opened
    runs-on: ubuntu-latest
    if: |
      contains(github.event.issue.labels.*.name, '<My first label>') ||
      contains(github.event.issue.labels.*.name, '<My second label>')

Upvotes: 2

Denis Rouzaud
Denis Rouzaud

Reputation: 2600

Finally found it:

contains( github.event.pull_request.labels.*.name, 'My Label')

Upvotes: 77

Related Questions