gpt4 book ai didi

maven - 限制在 SNAPSHOT 依赖项上触发哪些下游构建

转载 作者:行者123 更新时间:2023-12-05 08:10:05 25 4
gpt4 key购买 nike

我们有一个 Jenkins 服务器,每当(php 或 java)项目具有有效的 pom.xml 时,使用 jenkins-build-per-branch 从 git 同步。我们使用 Maven 版本控制策略来管理我们的 Artifact ,并使用 git-flow 作为分支策略/工具。我们还尽可能使用 jenkins 选项“每当构建 SNAPSHOT 依赖项时构建”。

我们遇到的问题是在构建 -SNAPSHOT 人工制品时 - 一切都乱七八糟,一切都想立即构建。 (构建“开发”-SNAPSHOT 会导致所有下游“功能”和“开发”分支开始)

理想情况下,我们希望找到一些方法,当 jenkins 启动下游作业时,我们不会在功能构建和开发构建之间交叉授粉。

有人试过吗?像 Conditional+BuildStep+Plugin 这样的东西会有帮助吗? https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin

最佳答案

这是一个老问题,但在 6 年后仍然适用。 Build whenever a SNAPSHOT dependency is built 设置上有一个“threshold”字段,可以控制触发哪些构建

来自pipeline-maven-plugin README :

Threshold based on the Maven lifecycle phase reached in the Mavenbuild of the upstream job (package, install, deploy). By default, onlythe maven builds who reach the deploy phase will trigger downstreambuilds.

Downstream lifecyclephase

例如,在脚本管道的 withMaven() 中,您可以使用 lifecycleThreshold: 'deploy' 设置一个 pipelineGraphPublisher,例如:

    withMaven(
maven: MAVEN_VERSION,
jdk: JAVA_VERSION,
mavenOpts: MAVEN_OPTS,
globalMavenSettingsConfig: globals.MAVEN_SETTINGS_ID,
options: [
pipelineGraphPublisher(
lifecycleThreshold: 'deploy',
includeSnapshotVersions: true
)
]) {

sh("mvn ${PHASE}")
}

然后任何执行生命周期阶段下方 deploy(例如packageinstall)的 SNAPSHOT 构建将 < em>不 触发下游作业。请注意,deploy 已经是默认设置,因此该示例不是特别有用,但它展示了如何使用该设置,您可能希望将其设置到另一个阶段。

第一部分已经完成,但现在您需要一种方法来有条件地为您确实想要触发下游构建的构建执行不同的 Maven 生命周期阶段您不想触发下游构建。我们根据分支名称执行此操作,以便 Pull Request 和 Release 分支不会触发上游包:

/**
* Return the correct Maven goal for the current branch
*
* Because the pipelineGraphPublisher's lifecycleThreshold in the withMaven() call above is set to 'deploy', pipelines
* that run the 'install' goal will not trigger downstream jobs; this helps us minimize superfluous Jenkins builds:
*
* https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/README.adoc#trigger-downstream-pipeline-when-a-snapshot-is-built
*/
String getGoalForCurrentBranch() {
if ( env.BRANCH_NAME ==~ /(^PR-(\d+)$)|(^releases\/v.*)/ ) {
echo("Pull Request or release branch detected! Executing Maven 'install' goal rather than 'deploy' goal to avoid triggering downstream Jenkins jobs")
return 'install'
}
return 'deploy'
}

然后您可以在执行 mvn 的任何地方调用此 getGoalForCurrentBranch() 方法以确定执行哪个生命周期阶段:

withMaven(
...
sh("mvn ${getGoalForCurrentBranch()}")
)

大多数分支将执行 mvn deploy 并且触发下游 Jenkins 作业,但 Pull Request 分支将执行 mvn install 并将 触发下游作业。

需要注意的是,您可能还有其他依赖于某些生命周期阶段的东西。在上面的示例中,Pull Request 分支 Artifact 不会部署到您的 Artifact 存储库(例如 Nexus)。在我们的例子中,这实际上是所需的行为,但您需要确定什么是您可以接受的,并相应地调整您的阈值。

关于maven - 限制在 SNAPSHOT 依赖项上触发哪些下游构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27298359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com