maven - Jenkins pipeline cannot push to gerrit using credentials
问题描述
I'm new to jenkins pipeline script, and I'm trying to use the maven release plugin in my pipeline. But I'm facing some problem during the push step of the release.
Here is my code :
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'
}
}
}
And I get the following error :
[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
I tried to directly put my personnal credentials in the code, instead of using the withCredentials :
sh 'mvn -B release:clean release:prepare release:perform -Dusername=johnDoe -Dpassword=password'
This time it seems to find the gerrit url, but obviously, the user is not allowed to push, as we have a gerrit architecture :
[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)
The thing is I need to use the gerrit user specified in our jenkins credentials, because he has the right to push and stuff. But it seems i cannot use it through the withCredentials function.
Please note that I cannot try to directly put the gerrit user in the command line because i don't know the username and the password.
Thank you for your help
EDIT :
Thanks to @ellak comment I found the solution :
def encodedPassword = URLEncoder.encode("$PASSWORD",'UTF-8')
sh "mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$encodedPassword"
Indeed I think the password was containing special char that needed to be encoded.
解决方案
You need to use "
instead of '
for String interpolation to work in groovy.
sh "git config user.name $USERNAME"
sh "mvn -B release:clean release:prepare release:perform -Dusername=$USERNAME -Dpassword=$PASSWORD"
推荐阅读
- python - 如何通过 python LDAP 或 Django LDAP 获取用户的经理?
- snowflake-cloud-data-platform - 从 SAP HANA 到 SNOWFLAKE 的过程对象迁移
- c# - 如何退出 hostbuilder 循环并使用 http 侦听器而不会出现 Cors 错误?
- python - 无论屏幕大小如何,如何将小部件放在窗口的某个位置。特金特
- r - 从一个向量生成所有可能的 k-mers 并保留在 r
- firebase-cloud-messaging - 单击 FCM 通知消息时将 PWA/Webapp 置于前台(与打开的选项卡 URL 无关)
- mysql - mysql:大表上的唯一约束
- python - Conda 发现与不相关的软件包有冲突
- angular - TypeScript 控制台日志打印来自同一对象的不同值
- android - “com.android.build.gradle.AppPlugin@27e0e84c”的“未指定”的错误“插件版本”不是有效的版本号