Jenkins pipeline 中循环读取参数。
应用场景:部署的目标服务器是可选的,例如,将构建结果只部署到web-server-01服务器,或者同时部署web-server-01服务器和web-server-02服务器。

实现步骤:

第一步, 项目中选择“This project is parameterized”,参数类型选Choice Parameter。为用户提供下拉框,name为参数名,Choices为下拉框的内容。如图1。当Build with Parameters 时就会有选择框。如图2。
第二步, pipeline 中先接收参数,String0 = “$params.servers”,然后使用for循环取参数,使用groovy函数tokenize()处理逗号分隔的字符串,这个函数会返回一个字符串数组。还有一点需要注意对item的引用,不能加引号。pipeline详见正文。

图1
在这里插入图片描述
图2
在这里插入图片描述
Jenkins Pipeline:

environment {
	PowerShell = 'JenkinsFile\\web-site\\deploy.ps1'
	String0 =  "$params.servers"
 }
  stage ('Deploy to IIS') {
      steps {
			script {
				script {
					for (item in String0.tokenize(',')){
						echo "Deploying to " + item
						powershell(returnStatus: true, script: "$PowerShell " + item)
					}
				}
			}
      }
  }
Logo

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

更多推荐