3

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

SexyMF
  • 10,657
  • 33
  • 102
  • 206

1 Answers1

9

Do not work on "labeled" branches just because you want to check the label. This check is done later. Work on "labeled" branches only if you want to trigger the action each time the PR is labelled.

To trigger the action at merge, work on "closed" branches and check the merge as below.


This code will execute when PR is closed, and check that

  1. PR is merged
  2. PR has label site
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')) }}
       ...
XGeffrier
  • 325
  • 3
  • 7