如何在 jenkinsfile 中创建循环
how to create loop inside jenkinsfile
我已经创建了一个 jenkinsfile
在里面我创建了一个包含 10 个服务器名称的文本参数。
我想在一个阶段中创建一个循环来回显每个服务器名称。
// ######################################################################### PROPERTIES ###########################################################################
properties([
parameters([
text(defaultValue: '', description: 'EXAMPLE: lsch3-123', name: 'SERVERS')])])
// ######################################################################### START PIPELINE ###########################################################################
pipeline {
agent none
options {
skipDefaultCheckout true
}
stages {
stage('GIT & PREP') {
agent {label "${params.DC}-ansible01"}
steps {
cleanWs()
run loop here
}
}
}
}
请参阅下面的 code/reference 示例,该示例将在舞台中循环播放。
// Define list which would contain all servers in an array. Initialising it as empty
def SERVERS = []
pipeline {
agent none
options {
skipDefaultCheckout true
}
parameters
{
// Adding below as example string which is passed from paramters . this can be changed based on your need
// Example: Pass server list as ; separated string in your project. This can be changed
string(name: 'SERVERS', defaultValue:'server1;server2', description: 'Enter ; separated SERVERS in your project e.g. server1;server2')
}
stages {
stage('GIT & PREP') {
agent {label "${params.DC}-ansible01"}
steps {
cleanWs()
// run loop here under script
script
{
// Update SERVERS list.Fill SERVERS list with server names by splitting the string.
SERVERS = params.SERVERS.split(";")
// Run loop below : execute below part for each server listed in SERVERS list
for (server in SERVERS)
{
println ("server is : ${server}")
}
}
}
}
}
}
此致。
我已经创建了一个 jenkinsfile 在里面我创建了一个包含 10 个服务器名称的文本参数。 我想在一个阶段中创建一个循环来回显每个服务器名称。
// ######################################################################### PROPERTIES ###########################################################################
properties([
parameters([
text(defaultValue: '', description: 'EXAMPLE: lsch3-123', name: 'SERVERS')])])
// ######################################################################### START PIPELINE ###########################################################################
pipeline {
agent none
options {
skipDefaultCheckout true
}
stages {
stage('GIT & PREP') {
agent {label "${params.DC}-ansible01"}
steps {
cleanWs()
run loop here
}
}
}
}
请参阅下面的 code/reference 示例,该示例将在舞台中循环播放。
// Define list which would contain all servers in an array. Initialising it as empty
def SERVERS = []
pipeline {
agent none
options {
skipDefaultCheckout true
}
parameters
{
// Adding below as example string which is passed from paramters . this can be changed based on your need
// Example: Pass server list as ; separated string in your project. This can be changed
string(name: 'SERVERS', defaultValue:'server1;server2', description: 'Enter ; separated SERVERS in your project e.g. server1;server2')
}
stages {
stage('GIT & PREP') {
agent {label "${params.DC}-ansible01"}
steps {
cleanWs()
// run loop here under script
script
{
// Update SERVERS list.Fill SERVERS list with server names by splitting the string.
SERVERS = params.SERVERS.split(";")
// Run loop below : execute below part for each server listed in SERVERS list
for (server in SERVERS)
{
println ("server is : ${server}")
}
}
}
}
}
}
此致。