如何从Pull Request动态加载Jenkins共享库?
动态加载Jenkins共享库(PR分支)的可行方案
你遇到的问题是Jenkins无法解析PR相关的Git引用作为共享库版本,以下是几种可行的解决方法:
方法1:直接使用PR分支的Commit哈希值
Git的commit哈希是全局唯一且Jenkins能直接识别的引用,可跳过PR引用的解析问题。
- 操作:打开PR页面,找到head分支的最新commit哈希(一串40位字符)
- 使用示例:
library identifier: 'custom-lib@a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0', retriever: modernSCM( [$class: 'GitSCMSource', remote: 'git@git.mycorp.com:my-jenkins-utils.git', credentialsId: 'my-private-key'] )
方法2:配置GitSCMSource的Refspec抓取PR引用
默认Jenkins的GitSCMSource不会自动抓取PR相关引用,需添加refspec参数让Jenkins拉取PR的head/merge分支:
library identifier: 'custom-lib@origin/pull/111/head', retriever: modernSCM( [$class: 'GitSCMSource', remote: 'git@git.mycorp.com:my-jenkins-utils.git', credentialsId: 'my-private-key', refspec: '+refs/pull/*:refs/remotes/origin/pull/*'] )
添加该refspec后,Jenkins会抓取所有PR引用,你之前尝试的origin/pull/111/head或origin/pull/111/merge即可正常解析。
方法3:手动checkout PR代码后加载本地库
先通过checkout步骤拉取PR代码到Jenkins工作目录,再指定本地路径加载共享库:
// 先checkout PR的head分支 checkout([ $class: 'GitSCM', branches: [[name: 'refs/pull/111/head']], userRemoteConfigs: [[ url: 'git@git.mycorp.com:my-jenkins-utils.git', credentialsId: 'my-private-key', refspec: '+refs/pull/*:refs/remotes/origin/pull/*' ]] ]) // 加载本地路径下的共享库,假设checkout到了jenkins-utils目录 library identifier: 'custom-lib', libraryPath: './jenkins-utils'
内容的提问来源于stack exchange,提问作者Umesh Kumar
相关产品推荐
相关产品推荐

