pipeline参数化

parameters是参数的意思,parameters指令提供用户在触发Pipeline时应提供的参数列表。这些用户指定的参数的值通过该params对象可用于Pipeline步骤。

生成流水线
pipeline参数化_第1张图片

  1. 字符串参数
    就是定义一个字符串参数,用户可以在Jenkins UI上输入字符串,常见使用这个参数的场景有,用户名,收件人邮箱,文件网络路径,主机名称的或者url等
parameters {
  string defaultValue: 'fea/v1.0.1', description: 'please give a name', name: 'branch', trim: false
}

2.选择参数
选择(choice)的参数就是支持用户从多个选择项中,选择一个值用来表示这个变量的值。工作中常用的场景,有选择服务器类型,选择版本号等。

parameters {
  choice choices: ['1.1', '1.2', '1.3'], description: 'select the version to test', name: 'version'
}

pipeline脚本

pipeline {
   agent any
options{  timestamps () }
environment {
parameters {
  string defaultValue: 'feature/v1.0.1', description: '请输入需要发布的分支', name: 'branch', trim: false
  choice choices: ['1.1', '1.2', '1.3'], description: 'select the version to test', name: 'version'
}
   stages {
      stage('get code ') {
         steps {
          sh("ls -al ${env.WORKSPACE}")
          deleteDir()  
          sh("ls -al ${env.WORKSPACE}")
          checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:front/dfc.git']]])   
	 
         }
      }

stage ('get branch-test'){
 steps {
      sh '''
echo $version
git checkout $branch
git pull origin $branch
echo "-------------"
/usr/local/bin/cnpm install
npm run build:test
'''
            }
          }

        }

    }
     

最后运行按键也变了
pipeline参数化_第2张图片

日志

16:35:56  + echo 1.3
16:35:56  1.3
16:35:56  + git checkout feature/v1.0.1
16:35:56  Previous HEAD position was 4570b96... Merge branch 'feature/import-pdf' into 'master'
16:35:56  Switched to a new branch 'feature/v1.0.1'
16:35:56  Branch feature/v1.0.1 set up to track remote branch feature/v1.0.1 from origin.
16:35:56  + git pull origin feature/v1.0.1
16:35:56  From gitlab.yunpiaoer.com:front/yunpiaoer_qwapp
16:35:56   * branch            feature/v1.0.1 -> FETCH_HEAD

你可能感兴趣的:(Jenkins)