Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github actions - on pull request merged with specific labels

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

like image 588
SexyMF Avatar asked Oct 24 '25 03:10

SexyMF


1 Answers

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')) }}
       ...
like image 197
XGeffrier Avatar answered Oct 26 '25 23:10

XGeffrier