Jenkins流水线SCM Step检出git和svn代码

[TOC]

本文主要介绍使用Pipeline: SCM Step插件,展示如何对git或svn仓库,进行分支/标签切换。需要注意的是Pipeline: SCM Step只是给具体的scm做接口转发,视你使用的具体scm而定,还要安装对应的依赖插件。

关于Pipeline: SCM Step能支持的哪些SCM见compatibility list

git分支/标签

首先,要了解git内部管理分支/标签的数据结构,可以参考这里两篇文章Git 内部原理 - Git 引用Git高级操作:refs和reflog,有以下结论

  • 分支的完整语法:refs/remotes/<remoteRepoName>/<branchName>
    • remoteRepoName:git clone -o或者git remote add时,定义的本地仓库别名,常定义为origin
    • branchName:分支名称
  • 标签的完整语法:refs/tags/<tagName>
    • branchName:标签名称

然后,从git插件的文档了解到,下载分支/标签需要通过填写branches.name来指定

注:根据实际项目情况,会用到多个git检出参数,比如子模块递归、子目录检出、账号凭据等,但限于篇幅且不想在本节暴露过多的参数,以免造成读者理解上的混乱。常见的参数都会逐渐在此后的章节中介绍。下例均以https://github.com/lizijie/jenkins_git_test_proj.git作为测试仓库。

检出分支

node {
    def branch_name = "refs/remotes/origin/main"

    stage("git branch/tag") {
        def result = checkout([
            $class: 'GitSCM', 
            branches: [[name: branch_name]], 
            userRemoteConfigs: [
                [url: 'https://github.com/lizijie/jenkins_git_test_proj.git']
            ]
        ])
        println("$result")
    }
}

检出标签

node {
    def tag_name = "refs/tags/v0.1.0-alpha"

    stage("git branch/tag") {
        def result = checkout([
            $class: 'GitSCM', 
            branches: [[name: tag_name]], 
            userRemoteConfigs: [
                [url: 'https://github.com/lizijie/jenkins_git_test_proj.git']
            ]
        ])
        println("$result")
    }
}

svn分支/标签 待续



原文:
https://lizijie.github.io/2023/04/18/Jenkins%E6%B5%81%E6%B0%B4%E7%BA%BFSCM-Step%E6%A3%80%E5%87%BAgit%E5%92%8Csvn%E4%BB%A3%E7%A0%81.html
作者github:
https://github.com/lizijie

PREVIOUS包装skynet.call&send远程调用接口
NEXTskynet中debug_console性能参数说明