[英]Jenkins pipeline cannot push to gerrit using credentials
我是 jenkins 管道腳本的新手,我正在嘗試在我的管道中使用 maven 發布插件。 但是我在發布的推送步驟中遇到了一些問題。
這是我的代碼:
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"
事實上,我認為密碼包含需要編碼的特殊字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.