25

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?

Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45
  • 1
    Are you sure it doesn't work? The `labels` property from the [`pull_request`](https://developer.github.com/v3/activity/events/types/#pullrequestevent) webhook is an array – smac89 Jan 04 '20 at 07:13
  • 1
    indeed, but an array of dictionnaries. I could finally use the `*`operator, and it works – Denis Rouzaud Jan 04 '20 at 07:16
  • 1
    I just want to add that a brand new workflow will most likely not work for already existing PRs. So when setting up Actions based on PRs non-standard types ('labeled' etc), use a new PR to test it. – Alex Skrypnyk Mar 26 '20 at 12:01

2 Answers2

63

Finally found it:

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

Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45
  • It doesn't looks to be working anymore. Does anyone got an alternative? – Deimosfr Oct 30 '20 at 18:14
  • 7
    This still works but beware that if your action has `on: [pull_request]`, the label(s) need to be there at PR creation time and not added afterward otherwise your github action will not rerun. Default triggers are `opened`, `synchronize` and `reopened` : https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request – jebeaudet Feb 22 '21 at 19:52
  • 6
    To add on @jebeaudet 's comment: The labels don't need to be there, if you simply add the type `labeled` to the list of types instead of using the default triggers – J. S. Jul 12 '21 at 09:18
0

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>')