首页 > 解决方案 > How can I rename Jenkins' pull request builder's "status check" display name on GitHub

问题描述

We have a project on GitHub which has two Jenkins Multibranch Pipeline jobs - one builds the project and the other runs tests. The only difference between these two pipelines is that they have different JenkinsFiles.

I have two problems that I suspect are related to one another:

  1. In the GitHub status check section I only see one check with the following title: continuous-integration/jenkins/pr-merge — This commit looks good, which directs me to the test Jenkins pipeline. This means that our build pipeline is not being picked up by GitHub even though it is visible on Jenkins. I suspect this is because both the checks have the same name (i.e. continuous-integration/jenkins/pr-merge).
  2. I have not been able to figure out how to rename the status check message for each Jenkins job (i.e. test and build). I've been through this similar question, but its solution wasn't applicable to us as Build Triggers aren't available in Multibranch Pipelines

If anyone knows how to change this message on a per-job basis for Jenkins Multibranch Pipelines that'd be super helpful. Thanks!

Edit (just some more info):

We've setup GitHub/Jenkins webhooks on the repository and builds do get started for both our build and test jobs, it's just that the status check/message doesn't get displayed on GitHub for both (only for test it seems). Here is our JenkinsFile for for the build job:

#!/usr/bin/env groovy 
properties([[$class: 'BuildConfigProjectProperty', name: '', namespace: '', resourceVersion: '', uid: ''], buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')), [$class: 'ScannerJobProperty', doNotScan: false]])
node {
    stage('Initialize') {
        echo 'Initializing...'
        def node = tool name: 'node-lts', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
        env.PATH = "${node}/bin:${env.PATH}"
    }

    stage('Checkout') {
        echo 'Getting out source code...'
        checkout scm
    }

    stage('Install Dependencies') {
        echo 'Retrieving tooling versions...'
        sh 'node --version'
        sh 'npm --version'
        sh 'yarn --version'
        echo 'Installing node dependencies...'
        sh 'yarn install'
    }

    stage('Build') {
        echo 'Running build...'
        sh 'npm run build'
    }

    stage('Build Image and Deploy') {
        echo 'Building and deploying image across pods...'
        echo "This is the build number: ${env.BUILD_NUMBER}"
        // sh './build-openshift.sh'
    }

    stage('Upload to s3') {
        if(env.BRANCH_NAME == "master"){
            withAWS(region:'eu-west-1',credentials:'****') {
                def identity=awsIdentity();
                s3Upload(bucket:"****", workingDir:'build', includePathPattern:'**/*');
                cfInvalidate(distribution:'EBAX8TMG6XHCK', paths:['/*']);
            }
        };
        if(env.BRANCH_NAME == "PRODUCTION"){
            withAWS(region:'eu-west-1',credentials:'****') {
                def identity=awsIdentity();
                s3Upload(bucket:"****", workingDir:'build', includePathPattern:'**/*');
                cfInvalidate(distribution:'E6JRLLPORMHNH', paths:['/*']);
            }
        };
    }
}

标签: jenkinsgithub

解决方案


尝试使用GitHubCommitStatusSetter(有关声明性管道语法,请参阅答案)。您正在使用脚本化的管道语法,因此在您的情况下,它将是这样的(注意:这只是原型,绝对必须更改以匹配您的项目特定):

#!/usr/bin/env groovy 
properties([[$class: 'BuildConfigProjectProperty', name: '', namespace: '', resourceVersion: '', uid: ''], buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')), [$class: 'ScannerJobProperty', doNotScan: false]])
node {

    // ...

    stage('Upload to s3') {
        try {
            setBuildStatus(context, "In progress...", "PENDING");

            if(env.BRANCH_NAME == "master"){
                withAWS(region:'eu-west-1',credentials:'****') {
                    def identity=awsIdentity();
                    s3Upload(bucket:"****", workingDir:'build', includePathPattern:'**/*');
                    cfInvalidate(distribution:'EBAX8TMG6XHCK', paths:['/*']);
                }
            };

            // ...

        } catch (Exception e) {
            setBuildStatus(context, "Failure", "FAILURE");
        }
        setBuildStatus(context, "Success", "SUCCESS");
    }
}

void setBuildStatus(context, message, state) {
  step([
      $class: "GitHubCommitStatusSetter",
      contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context],
      reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/my-org/my-repo"],
      errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
      statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
  ]);
}

请检查链接和链接以获取更多详细信息。


推荐阅读