Jenkins Pipeline:使用Git Parameter Plugin 实现参数化构建时选择分支和Tag

  • Plugin:Git parameter

  • 目的:构建时实现参数选择分支或Tag。

Important:

If you need use other type (other then branch) parameter, you must use git within checkout

Important settings:
  • It should be set a default value because initial build must get this information
  • Using git should be set a branchFilter as ‘origin/(.*)’ (origin is a remote server name)
先看下效果图

这里的分支名和tag名比较简单,如果你的分支名是根据feature并且关联了jira类型的管理软件,那你的分支名可能很长,输入即麻烦又容易出错。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200315100215277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxOTc3MTI1,size_16,color_FFFFFF,t_70

Jenkins Pipeline:
/**
*Use Git parameter plugin to Adds ability to choose branches, 
*tags or revisions from git repository configured in project.
*/

pipeline {
    agent any
    parameters {
        gitParameter name: 'BRANCH_TAG', 
                     type: 'PT_BRANCH_TAG',
                     branchFilter: 'origin/(.*)',
                     defaultValue: 'master',
                     selectedValue: 'DEFAULT',
                     sortMode: 'DESCENDING_SMART',
					 description: 'Select your branch or tag.'
		choice(name: 'SonarQube', choices: ['False','True'],description: '')				 
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM', 
                          branches: [[name: "${params.BRANCH_TAG}"]], 
                          doGenerateSubmoduleConfigurations: false, 
                          extensions: [], 
                          gitTool: 'Default', 
                          submoduleCfg: [], 
                          userRemoteConfigs: [[url: 'https:/192.168.1.2/scm/test.git',credentialsId: 'for_gitlab',]]
                        ])
            }
        }
    
         stage('QA Check') {
		    when {
	           expression { return env.sonarqube == "True" }
	        }
            steps {
                withSonarQubeEnv('SonarQubeServer') {
                    sh ' $SonarScannerHome/bin/sonar-scanner ' +
                    '-Dsonar.sources=src/main ' +
                    '-Dsonar.projectKey="test" ' +
                    '-Dsonar.projectName="test" '
                }
                timeout(time: 30, unit: 'MINUTES') {
                    waitForQualityGate abortPipeline: true
                }
            }
        }    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值