[英]Why does Jenkins allow specifying multiple git branches?
我无法解释用例是什么,但是从阅读源代码来看,git 插件中似乎有一个“高级用例”,它选择一个分支*(参考)并为其他分支安排一个新的构建.
必须注意的是,调度新构建仅限于其实现继承自AbstractProject
的作业,例如 Free-Style Jobs。 请注意,管道作业不是AbstractProjects
!
* 有可用的“构建选择器”来排序或限制匹配的分支/引用。
选择代码摘录:
/**
* If the configuration is such that we are tracking just one branch of one repository
* return that branch specifier (in the form of something like "origin/master" or a SHA1-hash
*
* Otherwise return [@code null}.
*/
@CheckForNull
private String getSingleBranch(EnvVars env) {
// if we have multiple branches skip to advanced usecase
if (getBranches().size() != 1) {
return null;
}
// [...]
}
// [...]
/**
* Determines the commit to be built in this round [...]
*/
private @NonNull Build determineRevisionToBuild(/* ... */) {
// [...]
if (candidates.isEmpty() ) {
final String singleBranch = environment.expand( getSingleBranch(environment) );
candidates = getBuildChooser().getCandidateRevisions(
false, singleBranch, git, listener, buildData, context);
}
if (candidates.isEmpty()) {
// getBuildCandidates should make the last item the last build, so a re-build
// will build the last built thing.
throw new AbortException("Couldn't find any revision to build. Verify the repository and branch configuration for this job.");
}
Revision marked = candidates.iterator().next();
( 来源)
安排新的构建:
if (candidates.size() > 1) {
log.println("Multiple candidate revisions");
if (checkForMultipleRevisions) {
Job<?, ?> job = build.getParent();
if (job instanceof AbstractProject) {
AbstractProject project = (AbstractProject) job;
if (!project.isDisabled()) {
log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
if (!project.scheduleBuild(0, new SCMTrigger.SCMTriggerCause("This build was triggered by build "
+ build.getNumber() + " because more than one build candidate was found."))) {
log.println("WARNING: multiple candidate revisions, but unable to schedule build of " + project.getFullDisplayName());
}
}
}
}
}
( 来源)
您在最新版本的插件中有更具描述性的解释。
让我回答你的问题。 这真的归结为一个问题,为什么要在 Git 中创建多个分支? 适用于 CICD 管道的典型示例是。 假设您有一些将部署到不同环境的代码。 因此,为了表示不同的环境,您可以创建分支。 例如develop
、 test
、 production
等。开发完成后,开发人员将在开发分支上工作,它将传播到生产。 开发分支将被合并到测试,然后到生产。 这就是詹金斯出现的地方。 在每次提交到相关分支时,您需要将此代码部署到相关环境,为此,您可以创建一个管道来处理部署过程。
例如看下面的管道。
stage('Deliver for development') {
when {
branch 'development'
}
steps {
sh './jenkins/scripts/deliver-for-development.sh'
input message: 'Finished using the web site? (Click "Proceed" to continue)'
sh './jenkins/scripts/kill.sh'
}
}
stage('Deploy for production') {
when {
branch 'production'
}
steps {
sh './jenkins/scripts/deploy-for-production.sh'
input message: 'Finished using the web site? (Click "Proceed" to continue)'
sh './jenkins/scripts/kill.sh'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.