我是jenkins管道脚本的新手,我正在尝试在我的管道中使用maven release插件。但是在发布的推送步骤中我遇到了一些问题。
这是我的代码:
node('linux') {
stage('Checkout') {
checkout scm
}
withMaven(maven: 'Maven 3') {
stage('Build') {
sh "mvn clean verify"
junit '**/target/*-reports/TEST-*.xml'
}
stage('SonarQube analysis') {
sh 'mvn sonar:sonar'
}
stage('Release') {
withCredentials([usernamePassword(credentialsId: "jenkins-gerrit", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh 'git config user.email "************************"'
sh 'git config user.name $USERNAME'
sh 'mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$PASSWORD'
}
}
}
我收到以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.4.2:prepare (default-cli) on project aqt-router: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] fatal: unable to access 'http://****:****@mygerritserver/myproject/': Could not resolve host: ****; Name or service not known
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
我试图直接将我的个人凭据放在代码中,而不是使用withCredentials:
sh 'mvn -B release:clean release:prepare release:perform -Dusername=johnDoe -Dpassword=password'
这次它似乎找到了gerrit url,但显然,用户不允许推送,因为我们有一个gerrit架构:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.4.2:prepare (default-cli) on project aqt-router: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] remote: Branch refs/heads/master:
[ERROR] remote: You are not allowed to perform this operation.
[ERROR] remote: To push into this reference you need 'Push' rights.
[ERROR] remote: User: johnDoe
[ERROR] remote: Please read the documentation and contact an administrator
[ERROR] remote: if you feel the configuration is incorrect
[ERROR] remote:
[ERROR] remote: Processing changes: refs: 1
[ERROR] remote: Processing changes: refs: 1, done
[ERROR] To http://johnDoe:password@mygerritserver/myproject
[ERROR] ! [remote rejected] master -> master (prohibited by Gerrit)
问题是我需要使用我们的jenkins凭证中指定的gerrit用户,因为他有权推送和填充。但似乎我不能通过withCredentials函数使用它。
请注意,我不能尝试直接将gerrit用户放在命令行中,因为我不知道用户名和密码。
谢谢您的帮助
编辑:
感谢@ellak评论,我找到了解决方案:
def encodedPassword = URLEncoder.encode("$PASSWORD",'UTF-8')
sh "mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$encodedPassword"
实际上我认为密码包含需要编码的特殊字符。
您需要使用"
而不是'
进行字符串插值以在groovy中工作。
sh "git config user.name $USERNAME"
sh "mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$PASSWORD"
感谢@ellak评论,我找到了解决方案:
def encodedPassword = URLEncoder.encode("$PASSWORD",'UTF-8')
sh "mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$encodedPassword"
实际上我认为密码包含需要编码的特殊字符。