简体   繁体   中英

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

I have a Jenkins pipeline Job in which I have mentioned GHE repo1 where my pipeline script is written. Now in my pipeline script in GHE repo1 I want to call one nodejs script which I have kept in GHE repo2. How I can call from this remote repo2 which is not mentioned in my Jenkins Job? I tried cloning repo2 in my pipeline script but it gives error

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

I was able to do by using below code:

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

You can also save the other SSH key in the Jenkins Credentials and then use native git commands (works in scripted pipeline, I don't know about declarative pipeline):

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
}

This requires the git executable in the path variable of the build agent. I am using it with git for windows.

EDIT: This is a potential security issue because the SSH key is saved in a file and could be read by other jobs!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM