首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GitHub上重命名Jenkins的拉请求生成器的“状态检查”显示名称

如何在GitHub上重命名Jenkins的拉请求生成器的“状态检查”显示名称
EN

Stack Overflow用户
提问于 2019-01-28 09:46:57
回答 4查看 3.7K关注 0票数 4

我们有一个关于GitHub的项目,它有两个Jenkins多分支管道作业-一个构建项目,另一个运行测试。这两个管道之间唯一的区别是它们有不同的JenkinsFiles

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

  1. 在GitHub状态检查部分中,我只看到一个标题为continuous-integration/jenkins/pr-merge — This commit looks good的检查:continuous-integration/jenkins/pr-merge — This commit looks good,它将我引向测试 Jenkins管道。这意味着我们的build管道不会被GitHub获取,尽管它在Jenkins上是可见的。我怀疑这是因为这两个检查都有相同的名称(即continuous-integration/jenkins/pr-merge)。
  2. 我还没有弄清楚如何为每个Jenkins作业(即、test、build)重命名状态检查消息。我曾经历过类似的问题,但它的解决方案不适用于我们,因为构建触发器多分支管道中不可用

如果有人知道如何在Jenkins多分支管道的每个作业的基础上更改此消息,那将是非常有帮助的。谢谢!

编辑(只是更多的信息):

我们已经在存储库上设置了GitHub/Jenkins We钩子,并为我们的build测试作业启动了构建,只是状态检查/消息没有显示在GitHub上(似乎只显示在测试上)。下面是我们的JenkinsFile for build作业:

代码语言:javascript
复制
#!/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:['/*']);
            }
        };
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-01-29 09:41:25

尝试使用GitHubCommitStatusSetter (关于声明性管道语法,请参见答案)。您使用的是脚本化的管道语法,所以在您的示例中它将是这样的(注意:这只是一个原型,它肯定必须修改以匹配特定的项目):

代码语言:javascript
复制
#!/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]] ]
  ]);
}

有关详细信息,请查看链接。

票数 3
EN

Stack Overflow用户

发布于 2020-02-11 10:16:25

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

安装之后,转到作业配置。在“分支源”-> " Github“-> "Branch”下单击"Add“,然后从下拉菜单中选择”“。然后,您可以在"Label“字段中键入自定义上下文名称。

票数 2
EN

Stack Overflow用户

发布于 2019-06-04 03:18:02

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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54399220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档