有没有一种方法可以在Jenkins管道脚本中从GIT repo调用脚本?
我有一份Jenkins管道工作,我在其中提到了GHE repo1,我的管道脚本是在那里编写的。现在,在GHE repo1中的管道脚本中,我想调用我在GHE repo2中保留的一个nodejs脚本。我怎么能从这个远程repo2调用我的Jenkins工作中没有提到的东西?我尝试在管道脚本中克隆repo2,但出现了错误有没有一种方法可以在Jenkins管道脚本中从GIT repo调用脚本?,git,jenkins,github,jenkins-pipeline,Git,Jenkins,Github,Jenkins Pipeline,我有一份Jenkins管道工作,我在其中提到了GHE repo1,我的管道脚本是在那里编写的。现在,在GHE repo1中的管道脚本中,我想调用我在GHE repo2中保留的一个nodejs脚本。我怎么能从这个远程repo2调用我的Jenkins工作中没有提到的东西?我尝试在管道脚本中克隆repo2,但出现了错误 git clone ssh://git@github.com/Myorg/repo2.git Cloning into 'repo2'... Permission denied (pu
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:,keyFileVariable:'SSHKEYFILE')){
sh(“ssh代理bash-c'ssh add${env.SSHKEYFILE};git clone')//如果使用windows,则将'sh'替换为'bat'
}
这需要在构建代理的path变量中包含git可执行文件。我将它与git for windows一起使用
编辑:这是一个潜在的安全问题,因为SSH密钥保存在一个文件中,可以被其他作业读取 错误是告诉问题是什么,您需要在jenkins机器上拥有公钥才能克隆git repo。目前您没有克隆人的访问权限。您可以在我的本地计算机上克隆此回购。如何克隆管道中的脚本。如何处理SSH密钥?我在Jenkins凭证中有SSH密钥。这可以使用签出,然后使用凭证部分来完成。检查pipelineI中的签出步骤我可以使用以下步骤执行:stages{stage('Clone gre admin cli'){steps{dir('gre-admin-cli')){git url:'git@github.ibm.com:Bluemix Admin/gre Admin cli.git',branch:'master',credentialsId:'c37cf275-afc4-4131-b9c1-f7d61d2f9b8b'}}
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
}