2016-11-15 55 views
7

我在jenkins中有Github Organisation項目。在我的版本庫的根,我的Jenkinsfile看起來是這樣的:如何結帳github組織中的ssh remote Jenkins工作流程並在Jenkinsfile中使用ssh憑證

node { 

    def jenkinsCredsId = 'xxxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbbbbb' 

    stage 'Checkout' 
    checkout scm 
    // I also tried the following: 
    // checkout scm: [$class: 'GitSCM', source: 'ssh://[email protected]:MY_ORGANISATION/jenkins-testing-temp.git', clean: true, credentialsId: jenkinsCredsId] 

    stage 'Build' 
    // generate some artefact (dist.zip) 

    stage 'Release' 

    sshagent([jenkinsCredsId]) { 
    sh ''' 
    git remote -v // show remotes 
    ssh-add -l // show currently loaded ssh keys fingerprints 

    git fetch --all --tags // IT FAILS HERE 

    CURRENT_BUILD_TAG="some_build/${BUILD_NUMBER}" 
    git tag ${CURRENT_BUILD_TAG} 

    git push --tags 

    github-release release \ 
    --security-token ${GITHUB_RELEASE_TOKEN} \ 
    --user MY_ORGANIZATION \ 
    --repo MY_REPO \ 
    --tag ${CURRENT_BUILD_TAG} \ 
    --name ${CURRENT_BUILD_TAG} 

    github-release upload \ 
    --security-token ${GITHUB_RELEASE_TOKEN} \ 
    --user MY_ORGANIZATION \ 
    --repo MY_REPO \ 
    --tag ${CURRENT_BUILD_TAG} \ 
    --name ${CURRENT_BUILD_TAG} \ 
    --file dist.zip 
    ''' 
    } 

有一個在這裏的幾行測試存儲庫訪問和它目前沒有在git fetch部分,出現以下錯誤:

fatal: could not read Username for ' https://github.com ': No such device or address

上述Jenkinsfilegit remote -v命令輸出類似origin https://github.com/MY_ORGANIZATION/MY_REPO的東西。

Github Organization git的配置看起來是這樣的: repository-sources-github-organization-jenkins-config

我發現了幾個相關問題:

回答

8

事實證明,在Github Organization項只使用https憑證f或Scan credentials(如上圖所示)。

解決方法是按Advanced按鈕,然後在Checkout credentials下拉列表中選擇ssh憑證,而不是默認的- Same as scan credentials -

advanced-view-checkout-credentials

注意星星憑據是用戶名/密碼的條目,這就是我發現了這個問題。

這樣,checkout scm將使用ssh,而sshagent([jenkinsCredsId]) {塊將按預期工作,讓您創建標籤,根據您的權限獲取並推送。 :)