簡體   English   中英

帶有 docker 代理的 Jenkinsfile 無法加載 ssh 密鑰

[英]Jenkinsfile with docker agent not able to load ssh key

我有一個簡單的管道:

pipeline {
    agent {
        docker {
            image 'python:3.8-alpine3.15'
        }
    }
...
steps {
       withCredentials([sshUserPrivateKey(credentialsId: "repo", keyFileVariable: 'keyfile')]){
             sh '''
             set +x
             eval `ssh-agent -s`
             ssh-add ${keyfile}
             git clone git@gitlab.com/blabla
             '''

      }
   }
}

錯誤的output為:

Masking supported pattern matches of $keyfile
Agent pid 53
+ ssh-add **** (blabla@blabla.com)
...
Host key verification failed.
fatal: Could not read from remote repository.

我在同一台機器上用相同的密鑰逐步嘗試了相同的步驟並且它有效,問題在於withCredentials綁定。 改成ssh-agent插件是不可行的。

有誰知道出了什么問題以及為什么我無法成功加載憑據?

經過大量調試,對我有用的是:

  • SSH Agent實現(需要插件下載,不好)
pipeline {
    agent {
        docker {
            image 'python:3.8-alpine3.15'
        }
    }
...
steps {
       sshagent(credentials: ['repo']) {
             sh '''
             set +x
             mkdir ~/.ssh
             ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
             git clone git@gitlab.com/blabla
             pip install -r requirements.txt
             '''
      }
   }
}

最后我最終使用了sshagent插件,否則如果你需要使用withCredentials插件,你應該考慮:

  • 香草實施(沒有額外的插件下載,很好)
pipeline {
    agent {
        docker {
            image 'python:3.8-alpine3.15'
        }
    }
...
steps {
       withCredentials([sshUserPrivateKey(credentialsId: "repo", keyFileVariable: 'keyfile')]) {
             sh '''
             set +x
             mkdir ~/.ssh
             ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
             eval `ssh-agent -s`
             ssh-add ${keyfile}
             git clone git@gitlab.com/blabla
             pip install -r requirements.txt
             '''
      }
   }
}

我個人認為withCredentials的實現更容易實現,因為你不依賴於外部插件。

外部參考:

通過這兩個實現,將 ssh 密鑰傳遞到在 docker 容器內運行的管道時應該沒有任何問題。

免費軟件。

暫無
暫無

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

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