How can I keep github.event.pull_request.labels up-to-date when re-trying a workflow?

I have my workflow jobs set to skip if a particular label isn’t set on a PR:

if: "github.event_name != 'pull_request' || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'run extra jobs'))"

Which works fine if the label is already there, however If I first run the workflow with the label absent, and then add the label & re-try the workflow, github.event.pull_request.labels hasn’t registered that the label has now been added to the PR, and so the job is still skipped.

Is there a way to rectify this problem?

Hi @johnlbergqvist,

This is by designed. For pull_request event, the default activity type is opened , synchronize , or reopened. Add the label and then re-run the workflow will not change the workflow GITHUB_SHA, cannot get the new labels.

As an alternative, you can add ‘labeled’ activity type for pull_request event, if you add the label back, new workflow will be triggered and have the lastest labels.
Code sample as below:

on: 
  pull_request:
    types: 
      - opened
      - edited
      - synchronize
      - labeled
    
jobs:
  job1:
    if: "github.event_name != 'pull_request' || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'run extra jobs'))"
    runs-on: [ubuntu-latest]
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

Thanks

@weide-zhou That sounds great. Is it possible for it to only run if a particular label has been added? (or skip running the workflow if the label that’s triggered the event isn’t the label i’m checking for in this scenario)?

Hi @johnlbergqvist,

Thanks for your reply! If the workflow is triggered by pull_request event with labeled activity, the label name can be checked via ${{ github.event.label.name }}. Hence you can check the value in the if expression.

if: "github.event_name != 'pull_request' || (github.event_name == 'pull_request' && github.event.label.name == 'run extra jobs')"

Thanks