簡體   English   中英

使用來自聲明性管道的 jenkins 憑據進行 Git 推送

[英]Git push using jenkins credentials from declarative pipeline

我正在使用 jenkins 管道(聲明性 synthax)並且我想將提交推送到我的遠程存儲庫。

有沒有辦法使用 git 插件來完成這個? 這是我目前正在嘗試的:

withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIAL_ID}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                        sh "git add ${BRANCH_RENAME}.bundle"
                        sh "echo ${GIT_USERNAME}|||||||${GIT_PASSWORD}"
                        sh "git tag -a backup -m 'Backup branch ${BRANCH} from vega-salesforce to vega-salesforce-backup' "
                        sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${GIT_URL_WITHOUT_HTTPS} --tags')
                    }

但它不起作用。 我收到以下錯誤:`

fatal: unable to access 'https://****:****@myrepositoryurl/mygitgroup/salesforce-backup/': Could not resolve host: ****:clear_password_here; Name or service not known

有人可以幫忙嗎? 我雖然問題來自我的密碼中存在的特殊字符,但我不確定。

我們終於想通了。 問題很簡單,我們的密碼中有特殊字符會破壞 url。

這是工作代碼:

withCredentials([usernamePassword(credentialsId: env.GIT_CREDENTIAL_ID, usernameVariable: 'USER', passwordVariable: 'PASS')]) {
                    script {
                        env.encodedPass=URLEncoder.encode(PASS, "UTF-8")
                    }
                    sh 'git clone https://${USER}:${encodedPass}@${GIT_URL} ${DIR} -b ${BRANCH}'
                    sh 'git add .'
                    sh 'git commit -m "foobar" '
                    sh 'git push'
                } 

您不能在腳本中使用 username:password 連接到 git repo。

您應該使用 ssh 密鑰。 請參閱此答案以獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM