pipeline { agent { label 'Linux' } parameters { extendedChoice description: '請選擇構建環境', multiSelectDelimiter: ',', name: 'envs', propertyFile: '/data/jksconf/jkslist', propertyKey: 'envs', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 5 gitParameter branch: '', branchFilter: '.*', defaultValue: 'origin/master', description: '代碼分支', name: 'project', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH_TAG' booleanParam defaultValue: false, description: '是否開啓代碼掃描', name: 'SONAR_SCAN' } environment { sonar_scan = "$params.SONAR_SCAN" } stages { stage('Checkout Code') { steps { checkout([$class: 'GitSCM', branches: [[name: '${project}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'e152b36f-b12a-4bef-a9d8-22222222', url: 'ssh://git@git.abs.com/abc/demo']]]) } } stage('代碼掃描'){ steps { script { def scannerHome = tool 'sonar-scanner-4.5.0.2216'; if ( sonar_scan == 'true' ) { withSonarQubeEnv('SonarQube') { sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.language=PHP -Dsonar.sources=. -Dsonar.sourceEncoding=utf8" } } } } } stage('同步站點'){ steps { sh label: '', script: ''' sh deploy.sh ${envs} ${project} ''' script { FUNT = sh(returnStdout: true, script: "echo ${envs}") PROJ = sh(returnStdout: true, script: "echo ${project}}") buildDescription "構建分支:[${FUNT}]-[${PROJ}]" } } } } post { always { echo 'One way or another, I have finished' deleteDir() /* clean up our workspace */ } success { echo 'I succeeeded!' } unstable { echo 'I am unstable :/' } failure { echo 'I failed :(' } changed { echo 'Things were different before...' } } }
經過sonar_scan這個參數判斷,是否執行代碼掃描git