如何在GitHub上重命名Jenkins的请求请求构建器的“状态检查”显示名称

时间:2019-01-28 09:46:57

标签: jenkins github

我们在 GitHub 上有一个项目,其中有两个 Jenkins Multibranch Pipeline 作业-一个构建该项目,另一个运行测试。这两个管道之间的唯一区别是它们具有不同的 JenkinsFiles

我怀疑有两个相互关联的问题:

  1. 在GitHub状态检查部分中,我仅看到带有以下标题的一项检查: continuous-integration/jenkins/pr-merge — This commit looks good, 引导我进入 test Jenkins渠道。这意味着GitHub不会选择我们的 build 管道,即使它在Jenkins上可见。我怀疑这是因为两个支票具有相同的名称(即continuous-integration/jenkins/pr-merge)。
  2. 我无法弄清楚如何重命名每个Jenkins作业的状态检查消息(即 test build )。我曾经遇到过this个类似的问题,但是它的解决方案不适用于我们,因为 Build Triggers Multibranch Pipelines
  3. 中不可用

如果有人知道如何针对 Jenkins Multibranch Pipelines 在每个职位上更改此消息,那将非常有帮助。谢谢!

编辑(仅提供更多信息):

我们已经在存储库中设置了GitHub / Jenkins webhooks,并且我们的 build test 作业都已开始构建,只是状态检查/消息没有两者都不会在GitHub上显示(似乎仅用于 test )。 这是我们用于 build 作业的JenkinsFile:

#!/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:['/*']);
            }
        };
    }
}

3 个答案:

答案 0 :(得分:1)

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

#!/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]] ]
  ]);
}

请检查thisthis链接以了解更多详细信息。

答案 1 :(得分:0)

这个答案很像@ biruk1230的答案。但是,如果您不想降级github插件来解决该错误,则可以直接调用API。

continuous-integration/jenkins/pr-merge

两种方法的问题在于,无论如何都将显示this.helper.workingExperienceList[0]["start_date"]

答案 2 :(得分:0)

您可以使用Github Custom Notification Context SCM Behaviour插件https://plugins.jenkins.io/github-scm-trait-notification-context/

安装后,转到作业配置。在“分支源”->“ GitHub”->“行为”下,单击“添加”,然后从下拉菜单中选择“自定义Github通知上下文”。然后,您可以在“标签”字段中输入自定义上下文名称。