How do I get PR information in workflow triggered by Github App PR events

I’m trying to use the new GitHub PR trigger feature described here:

(Why I can’t post links here?)

  • CircleCI Discuss topic #52739
  • CircleCI Discuss topic #52796
  • circleci dot com slash docs slash github-trigger-event-options

What I ultimately want to do is trigger CircleCI via GitHub PR events (open, reopen, synchronize) and run SonarQube on the PR branch, when the PR is targeting (base.ref) is our main branch. Worth noting is main branch is not the default branch set on github. We got some weird branching scheme going on here.

What I’ve done is onboard the project to use Github App installation (our organization is on Github OAuth), add following events as trigger:

  • All pushes
  • PR marked ready for review
  • Pushes to open non-draft PRs
  • PR opened

All triggers are pointing to .circleci/config.yml pipeline. I’m using [workflow level when expression](circleci dot com slash docs slash config-reference hash using-when-in-workflows) to decide if we should run pr workflow or deployment workflows.

The information I need here is the base branch ref name in pipeline value for workflow run condition and passing into SonarQube. I’ve tried the following expressions:

  • pipeline.trigger_parameters.webhook.body.pull_request.base.ref == "main" → Compile error, variable doesn’t exist
  • pipeline.trigger_parameters.webhook.body regex matches \"base\"\s*:\s*\{.*\"ref\"\s*:\s*\"main\" → Doesn’t trigger pipeline
  • echo pipeline.trigger_parameters.webhook.body in bash: {} (I thought this should contain webhook body sent from github?)
  • echo pipeline.trigger_parameters.github_app → Compile error, guess there’s no dot drilling logic in compiler
  • echo pipeline.trigger_parameters.github_app.ref in bash: returns head ref, not what I want
  • echo pipeline.trigger_parameters.github_app.checkout_sha in bash: compile error, but this value is documented in [pipeline values table](circleci dot com slash docs slash variables hash pipeline-values)?
  • echo pipeline.git.base_revision in bash: Empty string

I could identify pipeline triggers from pull request via pipeline.event.name == "pull_request" [documented here](circleci dot com slash docs slash github-trigger-event-options hash supported-trigger-options), even though it’s not in pipeline values table.

Ok turns out you can list all pipeline values via apiv2 /api/v2/pipeline/:pipeline-id/values route. There is no information about the PR other than draft and merged status in it, which are both undocumented.

I’m exactly in the same situation. Did you manage to find out how to get the name of the target branches?

We didn’t. In the end we move back to GitHub OIDC trigger from GitHub Apps trigger, enable build only on PR, and override branch trigger with [Product Launch] Override Only Build PRs Setting to match our needs.

We set GitHub token in env, grab associated PR with CIRCLE_PULL_REQUESTS env and fetch PR information from GitHub API. This solution does introduce race condition when PR information change and rerun older runs will grab wrong information on PR.

The trigger isn’t really reliable when there’s multiple PR associated to a single ref.

We just tell users about these caveats and hope they won’t step on any bug related to CircleCI setup here.