Github actions - on pull request merged with speciphic labels

Viewed 877

I want to trigger an action when the following constraints fulfilled.

pr merged to main branch AND pr contains label == 'site'.

This is what I came up with, the problem that this one is running when the pr is created.
Nothing happens when I merge it to main.
What am I doing wrong?

name: Build and push site
on:
  pull_request:
    branches: [main]
    types: [labeled, closed]

jobs:
  build-push-site:
    if: github.event.pull_request.merged == true && contains( github.event.pull_request.labels.*.name, 'site')
    uses: avifatal/nx-tokenct/.github/workflows/build-push.yml@main
    with:
      ...

(inspired by this Run Github Actions when pull requests have a specific label)
Thanks

1 Answers

You should only work on "closed" branches, not "labeled" ones. You check labels later anyway.

This will do the job:

name: Build and push site
on:
  pull_request:
    types:
      - closed

jobs:
  build-push-site:
    if: ${{ (github.event.pull_request.merged == true) && (contains(github.event.pull_request.labels.*.name, 'site')) }}
       ...
Related