jenkins pipeline中动态定义变量(流程控制语句)

本文介绍了如何在Jenkins Pipeline中根据job名称动态定义代码仓库地址和分支。通过使用script块结合switch语句,实现了一个通用的Jenkinsfile,以应对多个具有相似步骤但代码仓库不同的job。虽然在script中定义的变量无法直接在外部访问,但经过尝试和解决,找到了一种能够成功下载代码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:根据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"
                }
            }
        }
        
        
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值