fri*_*nux 20 git maven maven-release-plugin jenkins
我正在使用Jenkins Pipeline自动构建和部署我的Java应用程序.我还使用maven-release-plugin来执行Maven部署到Artifactory.
问题是我的Jenkinsfile(或Jenkins管道配置):
您了解最后一步创建无限循环,即使没有有用的提交.
这是我的Jenkinsfile的有趣部分:
sshagent([git_credential]) {
sh "${maven_bin} --settings ${maven_settings} -DreleaseVersion=${release_version} -DdevelopmentVersion=${development_version} release:prepare release:perform -B"
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能打破循环(当Maven在GIT上提交时,避免Jenkins触发新的构建)?
谢谢
Den*_*oer 16
恕我直言,随着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>
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过配置带有Jenkinsfile的Multibranch Pipeline在所有分支上构建并仅从master分支部署,从而非常轻松地在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}'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何配置Multibranch Pipeline的信息,请参见https://jenkins.io/blog/2017/02/07/declarative-maven-project/#set-up.
使用此技术,您只能在非主分支上进行开发.然后创建一个拉取请求,将您的更改合并回主分支.然后,应该将工件自动部署到工件存储库.
附录
使用上述方法发布到Maven存储库时,pom.xml将没有正确的版本.要让Maven发布正确的版本,请使用flatten-maven-plugin:http://www.mojohaus.org/flatten-maven-plugin/usage.html.
另外,请访问:https://maven.apache.org/maven-ci-friendly.html
小智 7
如果有人对循环有相同的问题或后续构建被触发,那么BUT有一个触发器,它会在每次推送到存储库时启动jenkins管道(而不是轮询).
这是我做的人:我检查了最后一次提交是否在评论中包含"[maven-release-plugin]".
jenkinsfile中的代码:
def lastCommit = sh returnStdout: true, script: 'git log -1 --pretty=%B'
if (lastCommit.contains("[maven-release-plugin]")){
sh "echo Maven release detected" //dont trigger build
} else {
sh "echo Last commit is not from maven release plugin" //do build steps
<..build Job...>
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16790 次 |
最近记录: |