• 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
                }
            }
        }    
    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐