2016-08-30 50 views
6

在我的jenkins管道項目中,我可以檢查代碼從git罰款......但我們需要做一些git checkins,憑據顯然不會被緩存。jenkins管道不能檢查代碼到git

stage 'Checkout' 
    git url: '[email protected]:myproj.git', branch: 'master', credentialsId: '012ce21d-e920-44ee-b6f7-08df8ab41de0', variable: 'CREDENTIALS' 
    sh('git push') <---- fails with Permission denied (public key). 

這裏是輸出示例:

Entering stage Checkout 
Proceeding 
[Pipeline] git 
> git rev-parse --is-inside-work-tree # timeout=10 
Fetching changes from the remote Git repository 
> git config remote.origin.url [email protected]:myproj # timeout=10 
Fetching upstream changes from [email protected]:myproj.git 
> git --version # timeout=10 
using GIT_SSH to set credentials 
> git -c core.askpass=true fetch --tags --progress [email protected]:myproj.git +refs/heads/*:refs/remotes/origin/* 
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10 
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 
Checking out Revision cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 (refs/remotes/origin/master) 
> git config core.sparsecheckout # timeout=10 
> git checkout -f cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 # timeout=10 
> git branch -a -v --no-abbrev # timeout=10 
> git branch -D master # timeout=10 
> git checkout -b master cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 
> git rev-list cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 # timeout=10 
[Pipeline] sh 
[myproj] Running shell script 
+ git push --set-upstream origin master 
Warning: Permanently added the RSA host key for IP address '192.192.143.2' to the list of known hosts. 
Permission denied (publickey). 
fatal: Could not read from remote repository. 

人有一個很好的解決這個?

感謝

+0

您使用的詹金斯從碼頭工人了呢? –

+0

奴隸在碼頭上運行,不要認爲這很重要,儘管 –

+0

我最近有一個類似的問題在Windows奴隸...只是可以肯定,你能告訴我們你的管道的結果,如果你從主人運行它?另外,您是否配置了正確的存儲庫權限?從你的git @ gitbucket網址中可以看出,一個git用戶應該有權訪問你的repo – Pom12

回答

2

答案是使用sshagent詹金斯插件:

http://getmesh.io/Blog/Jenkins+2+Pipeline+101

這個插件注入的混帳訪問

+0

我不認爲這是「答案」。這是一個解決方法,但你不應該需要這麼做,我personnaly在我的管道中沒有'sshagent'命令,'git push'工作得很好。但是我的私鑰在Jenkins的home('/ var/lib/jenkins/.ssh/id_rsa')和相關的憑證'jenkins_ssh_key'指向這個私鑰...... – Pom12

+0

是從機上的目錄嗎? –

+0

是的,我在奴隸和主人都有,而且都工作得很好! – Pom12

4

從詹金斯管道樣本庫中提取的SSH_AUTH_SOCK環境變量,我們可以這樣做,避免使用sshagent:https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.Groovy

然後您爲例,溶液應使用憑據綁定插件(https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin),並使用這個片段:

stage ('Checkout') { 
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '012ce21d-e920-44ee-b6f7-08df8ab41de0', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) { 
    sh("git tag -a some_tag -m 'Jenkins'") 
    sh('git push git://${GIT_USERNAME}:${GIT_PASSWORD}@bitbucket.org:myproj.git') 
    } 
}