【问题标题】:How can I rename Jenkins' pull request builder's "status check" display name on GitHub如何在 GitHub 上重命名 Jenkins 的拉取请求构建器的“状态检查”显示名称
【发布时间】:2019-01-28 09:46:57
【问题描述】:

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

我有两个我怀疑彼此相关的问题:

  1. 在 GitHub 状态检查部分,我只看到一个标题如下的检查: continuous-integration/jenkins/pr-merge — This commit looks good, 这将我引导到 test Jenkins 管道。这意味着我们的 build 管道没有被 GitHub 获取,即使它在 Jenkins 上可见。我怀疑这是因为这两个检查具有相同的名称(即continuous-integration/jenkins/pr-merge)。
  2. 我无法弄清楚如何为每个 Jenkins 作业(即 testbuild)重命名状态检查消息。我遇到过this 类似的问题,但它的解决方案不适用于我们,因为 Build TriggersMultibranch Pipelines 中不可用

如果有人知道如何为 Jenkins 多分支流水线按作业更改此消息,那将非常有帮助。谢谢!

编辑(只是更多信息):

我们已经在存储库上设置了 GitHub/Jenkins webhook,并且我们的 buildtest 作业都开始构建,只是状态检查/消息没有' 两者都不会显示在 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:['/*']);
            }
        };
    }
}

【问题讨论】:

  • 你检查this的答案了吗?
  • 我看过它,但它似乎不适合我们的 JenkinsFile 结构。我们正在运行一个节点项目,所以我不知道文件是否会有所不同。我会将我们的 JenkinsFile 的副本添加到问题中,以便您查看它 - 我对 JenkinsFiles 的经验很少,因此我们将不胜感激。
  • 已回复。

标签: jenkins github


【解决方案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 链接了解更多详情。

【讨论】:

  • 我会试试这个并报告。感谢您迄今为止的所有帮助!
  • 我刚试过这个,它有效!我什至可以为管道中的每个阶段设置检查,这非常有用!感谢@biruk1230 的所有帮助。已解决
  • 很高兴它有帮助:)
  • 遗憾的是,这个解决方案今天停止为我工作。每次运行setBuildStatus 方法时,我都会收到“错误:[GitHub 提交状态设置器] - 无法检索构建的 Git 元数据,将构建结果设置为不稳定”错误消息。我已经尝试过this 解决方案,但它对我不起作用。有什么想法吗?
  • 如果您已经降级了 GitHub 插件,请尝试同时降级 GitHub Branch Source 插件,因为似乎是插件更新的问题。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    这个答案很像@biruk1230 的答案。但是如果你不想降级你的 github 插件来解决这个 bug,那么你可以直接调用 API。

    void setBuildStatus(String message, String state) 
    {
        env.COMMIT_JOB_NAME = "continuous-integration/jenkins/pr-merge/sanity-test"
        withCredentials([string(credentialsId: 'github-token', variable: 'TOKEN')]) 
        {
            // 'set -x' for debugging. Don't worry the access token won't be actually logged
            // Also, the sh command actually executed is not properly logged, it will be further escaped when written to the log
            sh """
                set -x
                curl \"https://api.github.com/repos/thanhlelgg/brain-and-brawn/statuses/$GIT_COMMIT?access_token=$TOKEN\" \
                    -H \"Content-Type: application/json\" \
                    -X POST \
                    -d \"{\\\"description\\\": \\\"$message\\\", \\\"state\\\": \\\"$state\\\", \
                    \\\"context\\\": \\\"${env.COMMIT_JOB_NAME}\\\", \\\"target_url\\\": \\\"$BUILD_URL\\\"}\"
            """
        } 
    }
    

    这两种方法的问题是continuous-integration/jenkins/pr-merge无论如何都会显示出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      相关资源
      最近更新 更多