0

I have a GitHub Action workflow that runs to deploy a preview of a react-native expo app always when a Pull Request is opened. However, I do not want it to run when the dependabot opens a Pull Request.

How can I filter the dependabot Pull Requests? I saw there is a label dependencies attached, but I could not make the label to be filtered.

A few attempts I tried:

name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: ${{ !contains(github.event.pull_request.labels.*.name, '0 diff dependencies') }}
name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: "!contains(github.event.pull_request.labels.*.name, '0 diff dependencies')"
name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: "!contains(github.event.pull_request.labels.*.name, '0 dependencies')"
name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: github.event.label.name != 'dependencies'
name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: ${{ github.event.label.name != 'dependencies' }}

If you want, you can find here the repository.

Lavínia Beghini
  • 165
  • 1
  • 11
  • Maybe a work-around is to use the fact that dependabot always commits with a message starting with `build(deps):`. This could work: `if: ${{ !contains(github.event.head_commit.message, 'build(deps)') }}` – joanis Dec 16 '22 at 04:25
  • Or this line might be more reliable: `Signed-off-by: dependabot[bot] ` you would never add that to a commit not created by dependabot. – joanis Dec 16 '22 at 04:29
  • By the way, if you add `echo "${{ toJson(github) }}"` in the `run:` section of one of your job's steps, it will display the whole structure with all the variables you can possibly query, so that would allow you to inspect and find where and how that label is stored exactly. – joanis Dec 16 '22 at 04:33
  • I liked the idea to use `Signed-off-by: dependabot[bot]`. Is this a parameter I put in front of the `if`? – Lavínia Beghini Dec 16 '22 at 16:23
  • 1
    I wanted to edit my answer to add this option, but it is bizarrely difficult to do correctly. Having a `: ` sequence in the string literal actually causes a syntax error in YAML. Also, the head commit message matches on a push action but not on a pull request action since that runs on a temporary merge. I give up trying to figure this variant out, sorry. – joanis Dec 16 '22 at 18:50
  • That is ok! I am glad to know that use the label is actually the best approach. Thank you for the effort to give the best solution! – Lavínia Beghini Dec 16 '22 at 21:51

2 Answers2

2

github.event.pull_request.labels is an array, so you index its first element. Assuming dependabot will only assign one label, this should be OK:

name: Preview
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  preview:
    if: github.event.pull_request.labels[0].name != 'dependencies'

I just tested it on a dummy repo and it skipped my action when the PR label matched.

joanis
  • 10,635
  • 14
  • 30
  • 40
1

Well, actually, there was a problem. Sometimes, the dependabot adds the label AFTER creating the Pull Request.

However, to solve it, it is possible to filter by user:

name: Verification
on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  verification:
    if: contains(github.actor, 'dependabot') == false
Lavínia Beghini
  • 165
  • 1
  • 11