Do something if a particular label is set

Hi!

I just wrote my first GitHub Action, which gets triggered when an issue is opened. I now want to change this so that the runner starts only when a particular label is assigned. I know I can use the trigger/type “labeled” for issues, but I want to look for a specific label. Is that possible?

Thanks!

2 Likes

Currently, GitHub Actions does NOT support to set labels filter for issues event.

However, as a workaround, you can use if conditional to skip all jobs in the workflow when the label does not match the specific label. And you need to add the if conditional on every job.

jobs:
 job01:
    if: github.event.label.name == '<label_name>'

More details, you can reference here: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idif

4 Likes

Ah, I see, thanks a lot. I played a little with the if conditional and this works pretty well. It looks like that the job produces a “fail” if the condition is not met, right? This would give me an error message/email… I could add the conditional to each step and get rid of that, but this seems less obvious. Is the described behavior expected and intended?

Btw. I found another way to test for any given label (since I now knew where to look thanks to your reply):

jobs:
  job01:
     if: contains( toJson(github), '<label_name>' )

Don’t know if that is a good idea (since it could find <label_name> somewhere else in the issue data), but so far it works.

Yeah, in the past, if all jobs in a workflow were skipped with the if conditional , the workflow displayed as “skipped/no status” status with a grey icon. But now, it displays as failed and will send the notification email for the failed workflow run.

Or you can use:

if: contains(github.event.pull_request.labels.*.name, 'some-defined-label')

It works in the case you put a label in a PR.

10 Likes

This is a great solution ! We are using this but now if shows a failed workflow instead of just not running it. Do you have a workaround ?

add a empty step that just echo some dummy text.

Edit: Sorry I haven’t seen the date.

Hello!

I have been trying to get labels in Github Actions with all the methods above and unable to do so. Did something change in the way those can be extracted from the PR?

Currently, my actions trigger on push and labels are missing in the Github context toJson(github). The only label property is labels_url.

Any help would be appreciated!

Cheers

2 Likes