场景:根据job名称定义不同的代码仓库地址和分支

方案:使用script把整个代码下载过程包含起来

常规写法是这样的:

pipeline
{
    agent { label 'test' } 

    stages
    {
        stage('DownloadCode')
        {
            steps
            {
                
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: 'master']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [
                        [$class: 'CleanBeforeCheckout'], 
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'code'], 
                        [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', timeout: 3, trackingSubmodules: false]], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: 'xxx', 
                    url: 'git@xxx.git']]])
            }
        }
        
        
    }
}

这种写法的弊端就是固定写好代码仓库地址和分支,如果有多个类似的job,只是代码仓库不同,就需要多个Jenkinsfile

 

设想对于这种多个pipeline,步骤都是相同或相似的,只是代码仓库不同的情况,统一使用一个Jenkinsfile,在Jenkinsfile里面通过对pipeline的名字判断来分别定义git url地址和branch等变量

在Jenkinsfile里面使用switch等语句时需要在script{}里面,因此所定义的变量在script外面就获取不到了

尝试多种方法总会报错或者在下面下载代码时获取不到前面定义的变量 

最终解决方案:

pipeline
{
    agent { label 'test' } 

    stages
    {
        stage('DownloadCode')
        {
            steps
            {
                script
                {
                    //according jenkins job name to set git url and branch to download
                    switch(env.JOB_NAME)
                    {
                        case "pipeline1":
                            url = 'git@url1.git'
                            branch = 'release'
                            break
                        case "pipeline2":
                            url = 'git@url2.git'
                            branch = 'master'
                            break
                        case "pipeline3":
                            url = 'git@code.url3.git'
                            branch = 'develop'
                            break
                        default:
                            echo "############ wrong pipeline name ############"
                            break
                    }
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: "$branch"]], //这里必须用"$branch"
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [
                        [$class: 'CleanBeforeCheckout'], 
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'code'], 
                        [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', timeout: 3, trackingSubmodules: false]], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: 'xxx', 
                    url: "$url"]]]) //这里必须用"$url"
                }
            }
        }
        
        
    }
}

 

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐