4

I am trying to start a workflow only if the merged pull_request has a specific label.

The merged key is referenced here within an action. The Pull object itself is documented here. But I don't see merged documented by itself or with other keys.

Is pull_request.label available to a Github Action? Is there a comprehensive doc that shows all the keys available to a pull_request?

Matt Norris
  • 8,596
  • 14
  • 59
  • 90
  • According to the link you shared, you could get the first label name in your workflow triggered by a pull_request event using `github.event.pull_request.labels.[0].name` (and use a script to get all the other values in a variable if you need to). I believe [this thread](https://stackoverflow.com/questions/62325286/run-github-actions-when-pull-requests-have-a-specific-label) could be useful as well, for example using `if: contains(github.event.pull_request.labels.*.name, '')` – GuiFalourd Mar 15 '22 at 17:31

1 Answers1

6

To list all labels you can use something like this

       x=${{ toJson(github.event.pull_request.labels.*.name) }}
       echo $x

Also to use a single label you can try

steps:
      - name: deploy        
        if: contains(github.event.pull_request.labels.*.name, 'deploy')      
        run: |
          echo "deploy"
pavan
  • 1,825
  • 1
  • 11
  • 10
  • Thank you, @pavan! I had some issues with marshaling using your first statement, though I think it had more to do with local tests using https://github.com/nektos/act. Your second statement above did the trick! – Matt Norris Mar 21 '22 at 16:29
  • The second statement didn't work because `github.event.pull_request.labels.*.name` returns an array and `contains()` is comparing strings. This will work `contains(toJson(github.event.pull_request.labels.*.name), 'deploy')`. – Chilcano Sep 29 '22 at 15:03
  • 1
    Actually the second should work. [According to the docs example](https://docs.github.com/en/actions/learn-github-actions/expressions#example-using-an-object-filter) this is exactly how you can check the labels. – Janusz 'Ivellios' Kamieński Nov 17 '22 at 16:20