gpt4 book ai didi

git - Maven Release Plugin 在 Jenkins Pipeline 中的使用

转载 作者:IT王子 更新时间:2023-10-29 01:24:58 27 4
gpt4 key购买 nike

我正在使用 Jenkins Pipeline 自动构建和部署我的 Java 应用程序。我还使用 maven-release-plugin 执行 Maven 部署到 Artifactory。

问题是我的 Jenkinsfile(或 Jenkins 管道配置):

  1. 我们在发布分支上提交 0.1.00-SNAPSHOT 版本
  2. Jenkins Pipeline获取代码,并进行maven发布
  3. Maven Release 将版本更改为 0.1.00
  4. Maven Release 标记 GIT 分支,提交并部署 Artifact
  5. Maven Release 修改版本为 0.2.00-SNAPSHOT 并提交
  6. Jenkins Pipeline 检测到 GIT 中的更改,因此触发新构建

您了解最后一步会创建一个无限循环,即使没有有用的提交也是如此。

这是我的 Jenkinsfile 中有趣的部分:

sshagent([git_credential]) {
sh "${maven_bin} --settings ${maven_settings} -DreleaseVersion=${release_version} -DdevelopmentVersion=${development_version} release:prepare release:perform -B"
}

如何打破循环(避免 Jenkins 在 Maven 在 GIT 上提交时触发新构建)?

谢谢

最佳答案

恕我直言,随着 git 和 pull 请求的出现,我认为将 maven-release-plugin 或 maven-version-plugin 与 Jenkins 管道一起使用不是一个好主意。

使用Multibranch Pipeline和这里提到的版本控制技术更符合持续交付: https://axelfontaine.com/blog/dead-burried.html

使用上面的版本控制技术,pom.xml 现在看起来像这样:

<project>
...
<version>${revision}</version>

<properties>
<!-- Sane default when no revision property is passed in from the commandline -->
<revision>0-SNAPSHOT</revision>
</properties>

<scm>
<connection>scm:git:your-git-repo-url</connection>
</scm>

<distributionManagement>
<repository>
<id>artifact-repository</id>
<url>your-artifact-repo-url</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.5</version>
<configuration>
<tag>${project.artifactId}-${project.version}</tag>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

现在,您可以通过使用 Jenkinsfile 配置多分支管道在所有分支上构建并仅从主分支部署,从而非常轻松地在 Jenkins 服务器上生成版本:

pipeline {
agent any
environment {
REVISION = "0.0.${env.BUILD_ID}"
}
triggers {
pollSCM('')
}
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '30'))
}
tools {
maven '3.5.2'
jdk 'jdk8'
}
stages {
stage ('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
'''
}
}
stage ('Build') {
steps {
sh 'mvn clean package'
}
}
stage ('Deploy') {
when {
branch 'master'
}
steps {
script {
currentBuild.displayName = "${REVISION}"
}
sh 'mvn deploy scm:tag -Drevision=${REVISION}'
}
}
}
}

参见 https://jenkins.io/blog/2017/02/07/declarative-maven-project/#set-up关于如何配置多分支管道。

使用这种技术,您只能在非主分支上进行开发。然后创建一个 pull 请求以将您的更改 merge 回 master 分支。这应该会自动将您的 Artifact 部署到您的 Artifact 存储库。


附录

当使用上述方法发布到 Maven 存储库时,pom.xml 将没有正确的版本。要让 Maven 发布正确的版本,请使用 flatten-maven-plugin:http://www.mojohaus.org/flatten-maven-plugin/usage.html .

另外,查看:https://maven.apache.org/maven-ci-friendly.html

关于git - Maven Release Plugin 在 Jenkins Pipeline 中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161285/

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