简体   繁体   English

在 Jenkins 中,如何正确设置 scm 对象并在构建概览页面上提供元信息?

[英]In Jenkins how do I properly set up a scm object and provide meta information on the build overview page?

In a Jenkins / git / Gerrit setup I currently maintain a bunch of " pipeline-scm " jobs: eg one of them gets triggered by Gerrit on an incoming change request in order to validate this change.在 Jenkins / git / Gerrit 设置中,我目前维护着一堆“ pipeline-scm ”作业:例如,其中一个由 Gerrit 在传入的更改请求中触发,以验证此更改。 There is a "Gerrit Trigger" plugin for Jenkins that provides that provides job with a git refspec and the branch that change relates to - among lots of other details. Jenkins 有一个“Gerrit Trigger”插件,它提供了一个 git refspec 和与更改相关的分支的工作 - 以及许多其他细节。 When you setup a "Pipeline script from SCM" a job instance (build) will be provided with an scm object which can be used to checkout the change and also gets used by Jenkins itself to show some meta information on the build's overview, eg the commits that define this change:当您设置“来自 SCM 的管道脚本”时,将为作业实例(构建)提供一个scm对象,该对象可用于checkout更改,并且 Jenkins 本身也使用它来显示有关构建概述的一些元信息,例如定义此更改的提交:

在此处输入图像描述

Now here comes my problem:现在我的问题来了:

For [..] reasons I need to turn this "Pipeline script from SCM" job into a "Pipeline script" job.出于 [..] 的原因,我需要将此“来自 SCM 的管道脚本”作业转换为“管道脚本”作业。 Ie instead of just using the auto-generated scm object (which is being used on the master node already to fetch the pipeline script (and most likely to utilize the meta information about this change)) to run checkout scm in my pipeline script I now have to create this object manually:即,而不是仅使用自动生成的scm对象(已在master节点上用于获取管道脚本(并且最有可能利用有关此更改的元信息))在我的管道脚本中运行checkout scm我现在必须手动创建此对象:

scm = [
    $class: "GitSCM",
    userRemoteConfigs: [[
        credentialsId: <SOME ID>,
        url: <REPO-URL>,
    ]],
    branches: [[
        name: <BRANCH-NAME>,
    ]],
]

checkout scm

This raises two questions:这提出了两个问题:

  1. I know the Gerrit plugin provides details about a change in environment variables like GERRIT_PATCHSET_REVISION , GERRIT_REFSPEC and GERRIT_BRANCH - but how do I use them?我知道 Gerrit 插件提供了有关环境变量更改的详细信息,例如GERRIT_PATCHSET_REVISIONGERRIT_REFSPECGERRIT_BRANCH - 但我该如何使用它们? I've seen examples which define branches using GERRIT_BRANCH , others use GERRIT_PATCHSET_REVISION others use a branch pattern like */master .我见过使用GERRIT_BRANCH定义branches示例,其他使用GERRIT_PATCHSET_REVISION其他使用*/master之类的分支模式的示例。

  2. how does the meta information which can be extracted from a valid scm instance get to the build's overview page?可以从有效scm实例中提取的元信息如何到达构建的概述页面? I guess the pipeline-scm mechanism makes the master node populate this page automatically, but now the master node has no scm object in advance anymore.我猜想pipeline-scm机制让master节点自动填充这个页面,但是现在master节点已经没有提前的scm对象了。 Is there a mechanism like "currentBuild.updateMetaInfo(scm)`?有没有像“currentBuild.updateMetaInfo(scm)`这样的机制?

Is there any documentation about how to properly set up and use a scm object (as Jenkins does) in a standalone pipeline job (ie without the scm mechanism)?是否有关于如何在独立管道作业(即没有 scm 机制)中正确设置和使用scm对象(如 Jenkins 所做的那样)的文档?

Update : This scm configuration works for me in terms of checkout :更新:这个scm配置在结账方面对我有用:

scm = [
    $class: "GitSCM",
    userRemoteConfigs: [[
        credentialsId: <MY_ID>,
        url: <MY_REPO_URL>,
        refspec: env["GERRIT_REFSPEC],
    ]],
    branches: [[name: "FETCH_HEAD"]],
];

Unfortunately even after checkout scm no "Changes" show up, instead the overview page only displays this:不幸的是,即使在checkout scm后也没有显示“更改”,而概览页面仅显示以下内容:

在此处输入图像描述

The build matrix in the job overview also states there are no changes:作业概述中的构建矩阵也表明没有变化: 在此处输入图像描述

Regarding your first question on how to use the GERRIT_ variables with the Jenkins Git SCM, check the docs :关于如何在 Jenkins Git SCM 中使用 GERRIT_ 变量的第一个问题,请查看文档

To get the Git Plugin to download your change;让 Git 插件下载您的更改; set Refspec to $GERRIT_REFSPEC and the Choosing strategy to Gerrit Trigger.将 Refspec 设置为 $GERRIT_REFSPEC 并将选择策略设置为 Gerrit Trigger。 This may be under ''Additional Behaviours/Strategy For Choosing What To Build' rather than directly visible as depicted in the screenshot.这可能在“用于选择构建内容的其他行为/策略”下,而不是在屏幕截图中直接可见。 You may also need to set 'Branches to build' to $GERRIT_BRANCH.您可能还需要将“要构建的分支”设置为 $GERRIT_BRANCH。 If this does not work for you set Refspec to refs/changes/:refs/changes/* and 'Branches to build' to *$GERRIT_REFSPEC.如果这对您不起作用,请将 Refspec 设置为 refs/changes/:refs/changes/* 并将“要构建的分支”设置为 *$GERRIT_REFSPEC。

Regarding the second one:关于第二个:

The checkout step should add the information to the build status page. checkout步骤应将信息添加到构建状态页面。

Regarding your closing question:关于你的结束问题:

The scm object is just a convenience object that represents the scm you would configure in Pipeline from SCM, so you dont need to provide the details specified there in the Pipeline script again. scm 对象只是一个方便的对象,代表您将从 SCM 在 Pipeline 中配置的 scm,因此您无需再次提供 Pipeline 脚本中指定的详细信息。

To generate a checkout step for a Pipeline without SCM, use the Snippet Generator from the Pipeline Syntax page.要为没有 SCM 的管道生成签出步骤,请使用管道语法页面中的片段生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jenkins 中,如何为主节点而不是构建节点设置 SCM 行为? - In Jenkins, how do I set SCM behavior for the master node rather the build nodes? 如果发生另一个scm触发的构建,我如何让Jenkins取消scm触发的构建? - How do I make Jenkins cancel a scm-triggered build if another scm-triggered build occurred? Jenkins SCM插件,如何获得自上次构建以来每个更新文件的列表? - Jenkins SCM plugin, how do I get a list of every updated file since last build? 当Jenkins不允许匿名用户时,如何通过Git推送到SCM-manager在Jenkins中触发构建? - How do I trigger a build in Jenkins, with Git push to SCM-manager, when Jenkins does not allow anonymous user? 如何正确设置詹金斯环境 - How to properly set up the jenkins environment 如何使用docker正确设置jenkins? - How to properly set up jenkins with docker? 如何在詹金斯同时设置“建立其他项目后建立”和“轮询SCM” - how to set “Build after other projects are built” and “Poll SCM” simultaneously in jenkins 如何在提交到 Github SCM 私有仓库时设置 Jenkins Pipeline 触发器? - How to set up Jenkins Pipeline trigger on commit to Github SCM private repo? 如何为jenkins构建设置chroot? - How to set up chroot for jenkins build? 如何在Jenkins管道中设置postgres数据库? - How do I set up postgres database in Jenkins pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM