Jenkins github pr status check pipeline

I want to make a pipeline that performs check when I open PR for “main” branch.


(This picture is a check made through github-action, but there is something I want to make through jenkins pipeline.)

I tried to implement it through a plugin Github Pull Request Builder, but it was not used because it identified a statement that there was a security issue at the moment.

So the next thing I came up with was to make a check that only works with pr through multibranch-pipeline.

pipeline {
  agent any

  stages {
    stage("PR") {
      when {
        branch pattern: "^PR[-][0-9]*", comparator: "REGEXP"
      }
      stages {
        stage("SomeTest") {
          steps {
            sh "test"
          }
        }
      }
    }
  }
}

It was implemented using the when keyword to operate only when it is PR Check.

But I don’t like this way, because I thought it didn’t fit the purpose of multibranch-pipeline, my pipeline only targets the “main” branch.

So I would like to make a test that execute when I create PR through regular pipeline, not multibranch-pipeline.

Does anyone know how to do it like this? Please help me :pray:

Jenkins setup:
Jenkins: 2.471
OS: Linux - 6.10.0-linuxkit
Java: 17.0.12 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

ant:511.v0a_a_1a_334f41b_
antisamy-markup-formatter:162.v0e6ec0fcfcf6
apache-httpcomponents-client-4-api:4.5.14-208.v438351942757
asm-api:9.7-33.v4d23ef79fcc8
aws-credentials:231.v08a_59f17d742
aws-java-sdk-ec2:1.12.767-467.vb_e93f0c614b_6
aws-java-sdk-minimal:1.12.767-467.vb_e93f0c614b_6
bootstrap5-api:5.3.3-1
bouncycastle-api:2.30.1.78.1-248.ve27176eb_46cb_
branch-api:2.1178.v969d9eb_c728e
build-timeout:1.33
caffeine-api:3.1.8-133.v17b_1ff2e0599
checks-api:2.2.0
cloudbees-folder:6.942.vb_43318a_156b_2
command-launcher:115.vd8b_301cc15d0
commons-lang3-api:3.16.0-82.ve2b_07d659d95
commons-text-api:1.12.0-129.v99a_50df237f7
config-file-provider:973.vb_a_80ecb_9a_4d0
credentials:1371.vfee6b_095f0a_3
credentials-binding:681.vf91669a_32e45
dark-theme:479.v661b_1b_911c01
display-url-api:2.204.vf6fddd8a_8b_e9
durable-task:568.v8fb_5c57e8417
echarts-api:5.5.0-1
eddsa-api:0.3.0-4.v84c6f0f4969e
email-ext:1814.v404722f34263
font-awesome-api:6.5.2-1
generic-webhook-trigger:2.2.2
git:5.3.0
git-client:5.0.0
github:1.40.0
github-api:1.321-468.v6a_9f5f2d5a_7e
github-branch-source:1793.v1831e9c68d77
gradle:2.12
gson-api:2.11.0-41.v019fcf6125dc
instance-identity:185.v303dc7c645f9
ionicons-api:74.v93d5eb_813d5f
jackson2-api:2.17.0-379.v02de8ec9f64c
jakarta-activation-api:2.1.3-1
jakarta-mail-api:2.1.3-1
javax-activation-api:1.2.0-7
javax-mail-api:1.6.2-10
jaxb:2.3.9-1
jdk-tool:80.v8a_dee33ed6f0
jjwt-api:0.11.5-112.ve82dfb_224b_a_d
joda-time-api:2.12.7-29.v5a_b_e3a_82269a_
jquery3-api:3.7.1-2
json-api:20240303-41.v94e11e6de726
json-path-api:2.9.0-58.v62e3e85b_a_655
junit:1284.vf75d778f98c5
ldap:725.v3cb_b_711b_1a_ef
mailer:472.vf7c289a_4b_420
matrix-auth:3.2.2
matrix-project:832.va_66e270d2946
metrics:4.2.21-451.vd51df8df52ec
mina-sshd-api-common:2.13.2-125.v200281b_61d59
mina-sshd-api-core:2.13.2-125.v200281b_61d59
multibranch-scan-webhook-trigger:1.0.11
okhttp-api:4.11.0-172.vda_da_1feeb_c6e
pam-auth:1.11
periodicbackup:2.0
pipeline-build-step:540.vb_e8849e1a_b_d8
pipeline-github-lib:61.v629f2cc41d83
pipeline-graph-analysis:216.vfd8b_ece330ca_
pipeline-graph-view:313.v1322ce83d680
pipeline-groovy-lib:730.ve57b_34648c63
pipeline-input-step:495.ve9c153f6067b_
pipeline-milestone-step:119.vdfdc43fc3b_9a_
pipeline-model-api:2.2205.vc9522a_9d5711
pipeline-model-definition:2.2205.vc9522a_9d5711
pipeline-model-extensions:2.2205.vc9522a_9d5711
pipeline-multibranch-defaults:2.1
pipeline-rest-api:2.34
pipeline-stage-step:312.v8cd10304c27a_
pipeline-stage-tags-metadata:2.2205.vc9522a_9d5711
pipeline-stage-view:2.34
plain-credentials:183.va_de8f1dd5a_2b_
plugin-util-api:4.1.0
resource-disposer:0.23
scm-api:696.v778d637b_a_762
script-security:1354.va_70a_fe478c7f
snakeyaml-api:2.2-121.v5a_68b_9300b_d4
ssh-credentials:343.v884f71d78167
ssh-slaves:2.973.v0fa_8c0dea_f9f
sshd:3.330.vc866a_8389b_58
structs:338.v848422169819
theme-manager:262.vc57ee4a_eda_5d
timestamper:1.27
token-macro:400.v35420b_922dcb_
trilead-api:2.147.vb_73cc728a_32e
variant:60.v7290fc0eb_b_cd
workflow-aggregator:600.vb_57cdd26fdd7
workflow-api:1336.vee415d95c521
workflow-basic-steps:1058.vcb_fc1e3a_21a_9
workflow-cps:3922.va_f73b_7c4246b_
workflow-durable-task-step:1364.v2fd76fb_6fd41
workflow-job:1436.vfa_244484591f
workflow-multibranch:795.ve0cb_1f45ca_9a_
workflow-scm-step:427.v4ca_6512e7df1
workflow-step-api:678.v3ee58b_469476
workflow-support:920.v59f71ce16f04
ws-cleanup:0.46

Hello and welcome to this community, @Antoliny0919. :wave:

To create a Jenkins pipeline that performs checks when a pull request (PR) is opened for the main branch, I think you could use the pipeline syntax with the when directive to conditionally execute stages based on the branch name. :thinking:

Here is an untested Jenkinsfile:

pipeline {
  agent any

  stages {
    stage('Check PR for main branch') {
      when {
        allOf {
          expression { return env.CHANGE_TARGET == 'main' }
          expression { return env.CHANGE_ID != null }
        }
      }
      steps {
        echo 'Running checks for PR targeting main branch...'
        // Add your test steps here
        sh 'test'
      }
    }
  }
}

The when directive is used to conditionally execute the stage.
allOf ensures that both conditions must be true for the stage to run.
expression { return env.CHANGE_TARGET == 'main' }: checks if the target branch of the PR is main.
expression { return env.CHANGE_ID != null }: checks if the build is triggered by a PR (i.e., CHANGE_ID is not null).

Ensure that your Jenkins instance is configured to use a GitHub webhook or a similar mechanism to trigger builds on PR events.
The env.CHANGE_TARGET and env.CHANGE_ID environment variables are provided by Jenkins when using the GitHub Branch Source Plugin or similar plugins that support PR builds.

Let us know if this mechanism works for you, or if this wild guess was a little bit too… wild. :wink: