簡體   English   中英

有什么方法可以在Jenkins管道腳本中從GIT存儲庫調用腳本?

[英]Is there a way from which I can call script from GIT repo in Jenkins pipeline script?

我有一個Jenkins管道作業,在其中提到了我寫管道腳本的GHE repo1。 現在,在我的GHE repo1中的管道腳本中,我想調用一個保存在GHE repo2中的nodejs腳本。 我如何從Jenkins Job中未提及的遠程repo2打電話? 我嘗試在管道腳本中克隆repo2,但它給出了錯誤

git clone ssh://git@github.com/Myorg/repo2.git
Cloning into 'repo2'...
Permission denied (publickey).
fatal: Could not read from remote repository.

我能夠通過使用以下代碼來做到這一點:

stages
  {
       stage('Clone repo1')
       { 
            steps
            {
              dir('repo1') 
              {
                 git url: 'XXXXXXX',
                 branch: 'master',
                 credentialsId: 'XXXXXXX'
              }
           }
       }
     }

您還可以將其他SSH密鑰保存在Jenkins憑據中,然后使用本機git命令(可在腳本管道中使用,我不知道聲明性管道):

withCredentials([sshUserPrivateKey(credentialsId: <credentials_name>, keyFileVariable: 'SSHKEYFILE')]) {
    sh("ssh-agent bash -c 'ssh-add ${env.SSHKEYFILE}; git clone <url>'") // replace 'sh' with 'bat' if using windows
}

這需要在構建代理的path變量中使用git可執行文件。 我將它與git一起用於Windows。

編輯:這是一個潛在的安全問題,因為SSH密鑰已保存在文件中,並且可由其他作業讀取!

暫無
暫無

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

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