Jenkinsfile 同时检出多个 Git 仓库

前置

通常,在 Jenkinsfile 中使用 Git 仓库是这样的:

stage('Checkout git repo') {
    steps {
        checkout([
            $class: 'GitSCM',
            branches: [[name: 'develop']],
            userRemoteConfigs: [[
                credentialsId:  'ssh-private-key-id',
                url: 'project-git-repo-url'
            ]]
        ])
    }
}

以上写法的结果是远程 Git 仓库中所有内容都被检出到当前工作目录下,因此我们才能像下面这样(不用切换目录而)直接运行一些命令:

stage('generate version number') {
    steps {
        VERSION_NUBMER = sh(script: "git describe --always", returnStdout: true).trim()
    }
}

将 Git 仓库检出到指定文件夹

如果要同时拉取多个 Git 仓库,面临的首要问题是如何将 Git 仓库检出到指定文件夹。对此,可以通过使用 $class: 'RelativeTargetDirectory' 解决,具体代码如下:

stage('Checkout git repo') {
    steps {
        checkout([
            $class: 'GitSCM',
            branches: [[name: 'develop']],
            userRemoteConfigs: [[
                credentialsId:  'ssh-private-key-id',
                url: 'project-git-repo-url'
            ]],
            extensions: [[
                $class: 'RelativeTargetDirectory',
                relativeTargetDir: 'repo-folder'
            ]]
        ])
    }
}

这样一来,远程 Git 仓库中所有内容将被检出到 ./repo-folder 文件夹下;后续我们若想在 Git 项目目录下操作,就需要先 cd ./repo-folder 了,例如:

stage('generate version number') {
    steps {
        VERSION_NUBMER = sh(script: "cd ./repo-folder && git describe --always", returnStdout: true).trim()
    }
}

检出多 Git 仓库

在解决了将 Git 仓库检出到指定文件夹这一问题后,检出多 Git 仓库这一需求就稍显容易,下面以检出repoA、repoB、repoC三个仓库为例:

stage('Checkout multiple git repos') {
    steps {
        checkout([
            $class: 'GitSCM',
            branches: [[name: 'develop']],
            userRemoteConfigs: [[
                credentialsId:  'ssh-private-key-id',
                url: 'project-git-repoA-url'
            ]],
            extensions: [[
                $class: 'RelativeTargetDirectory',
                relativeTargetDir: 'repoA-folder'
            ]]
        ])

        checkout([
            $class: 'GitSCM',
            branches: [[name: 'develop']],
            userRemoteConfigs: [[
                credentialsId:  'ssh-private-key-id',
                url: 'project-git-repoB-url'
            ]],
            extensions: [[
                $class: 'RelativeTargetDirectory',
                relativeTargetDir: 'repoB-folder'
            ]]
        ])

        checkout([
            $class: 'GitSCM',
            branches: [[name: 'develop']],
            userRemoteConfigs: [[
                credentialsId:  'ssh-private-key-id',
                url: 'project-git-repoC-url'
            ]],
            extensions: [[
                $class: 'RelativeTargetDirectory',
                relativeTargetDir: 'repoC-folder'
            ]]
        ])
    }
}

上面👆代码的结果就是工作目录下有了 repoA-folderrepoB-folderrepoC-folder 三个文件夹。

同时

截止现在,还没有达到我们的最终目的。本文标题叫做“Jenkinsfile 同时检出多个 Git 仓库”,但现在我们仅仅做到了“多个”还没有做到“同时”,为此我们需要使用Jenkins Pipeline语法中的 parallel 关键字。

stage('Checkout multiple git repos at the same time') {
    parallel {
        stage('repoA') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'develop']],
                    userRemoteConfigs: [[
                        credentialsId:  'ssh-private-key-id',
                        url: 'project-git-repoA-url'
                    ]],
                    extensions: [[
                        $class: 'RelativeTargetDirectory',
                        relativeTargetDir: 'repoA-folder'
                    ]]
                ])
            }
        }

        stage('repoB') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'develop']],
                    userRemoteConfigs: [[
                        credentialsId:  'ssh-private-key-id',
                        url: 'project-git-repoB-url'
                    ]],
                    extensions: [[
                        $class: 'RelativeTargetDirectory',
                        relativeTargetDir: 'repoB-folder'
                    ]]
                ])
            }
        }

        stage('repoC') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'develop']],
                    userRemoteConfigs: [[
                        credentialsId:  'ssh-private-key-id',
                        url: 'project-git-repoC-url'
                    ]],
                    extensions: [[
                        $class: 'RelativeTargetDirectory',
                        relativeTargetDir: 'repoC-folder'
                    ]]
                ])
            }
        }
    }
}

参考


image

posted @ 2022-11-16 21:48  暮重云  阅读(2002)  评论(0编辑  收藏  举报