Jenkins 多分支管道 Jenkinsfile 检测 Git 启动作业的回购

Jenkins Multibranch Pipeline Jenkinsfile detect Git repo that launched job

到目前为止,我的多分支管道配置工作运行良好....

但是每个存储库都有完全相同的 jenkinsfile,除了 git 存储库名称。

典型的 jenkinsfile 如下所示:

node('docker-slave') {
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'NEXUS', usernameVariable: 'NEXUS_USERNAME', passwordVariable: 'NEXUS_PASSWORD']]) {
        git url: 'git@bitbucket.org:myco/myprob.git', branch: env.branch_name, credentialsId: '08df8ab41de0', variable: 'CREDENTIALS'
        stage 'Test'
        sh 'env > env.txt'
        sh 'cat env.txt'
        sh 'make verify'
    }
}

我想做的是检测哪个 git 存储库触发了构建,这样我就不必在 jenkinsfile 中对其进行硬编码。

所以我想将 git 行更改为类似(注意 GIT_URL):

 git url: env.GIT_URL, branch: env.branch_name, credentialsId: '08df8ab41de0', variable: 'CREDENTIALS'

这让我更接近我的最终目标,即将我的 Jenkinsfile 存储在一个公共位置,而不必将它的 repo 复制到 repo 并修改它的 repo 到 repo。

有什么想法吗?

谢谢 菲尔

事实证明,在脚本中,以下代码完全符合我的需要:

checkout sum

不需要 git url 行....

所以最终代码如下所示:

node('docker-slave') {
     withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'NEXUS', usernameVariable: 'NEXUS_USERNAME', passwordVariable: 'NEXUS_PASSWORD']]) {
     checkout scm
     stage 'Test'
     sh 'make verify'
}