Jenkinsfile 一种类型的参数而不是两种类型,将选择转换为字符串或将字符串转换为选择
Jenkinsfile one type of parameter instead of two type, convert choice into string or string into choice
我使用了两个参数,一个是选项 (ID),另一个是字符串 (NID),但值是相同的。要求是仅使用参数选择或字符串。是否可以将选择参数转换为字符串或将字符串转换为选择参数?
这样我就可以使用一个参数和一个部署函数。
def deploy1(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.ID}" ===> string paramater
]])
def deploy2(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.NID}" ===> Needs choice paramater
]])
parameters {
choice(
name: 'ID',
choices: [ '8922', '9292', '3220' ]
)
string(
name: 'NID',
defaultvalue: '8922,9292,3220'
)
stage (DEV') {
steps {
script {
if (params.ENVIRONMENT == "dev"){
deploy1('devl') ===> this will call my deploy function
}
}
}
}
是的,您只需使用 split
:
即可将字符串参数转换为数组
下面是一个例子:
// Define list which would contain all servers in an array
def ID= []
pipeline {
agent none
parameters
{
// Adding below as example string which is passed from paramters . this can be changed based on your need
// Example: Pass NID list as , separated string in your project. This can be changed
string(name: 'NID', defaultValue:'8922,9292,3220', description: 'Enter , separated NID values in your project e.g. 8922,9292,3220')
}
stages {
stage('DEV') {
agent any
steps {
script
{
// Update ID list
ID= params.NID.split(",")
// You can loop thorugh the ID list
for (myid in ID)
{
println ("ID is : ${myid}")
}
}
}
}
}
}
我使用了两个参数,一个是选项 (ID),另一个是字符串 (NID),但值是相同的。要求是仅使用参数选择或字符串。是否可以将选择参数转换为字符串或将字符串转换为选择参数? 这样我就可以使用一个参数和一个部署函数。
def deploy1(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.ID}" ===> string paramater
]])
def deploy2(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.NID}" ===> Needs choice paramater
]])
parameters {
choice(
name: 'ID',
choices: [ '8922', '9292', '3220' ]
)
string(
name: 'NID',
defaultvalue: '8922,9292,3220'
)
stage (DEV') {
steps {
script {
if (params.ENVIRONMENT == "dev"){
deploy1('devl') ===> this will call my deploy function
}
}
}
}
是的,您只需使用 split
:
即可将字符串参数转换为数组
下面是一个例子:
// Define list which would contain all servers in an array
def ID= []
pipeline {
agent none
parameters
{
// Adding below as example string which is passed from paramters . this can be changed based on your need
// Example: Pass NID list as , separated string in your project. This can be changed
string(name: 'NID', defaultValue:'8922,9292,3220', description: 'Enter , separated NID values in your project e.g. 8922,9292,3220')
}
stages {
stage('DEV') {
agent any
steps {
script
{
// Update ID list
ID= params.NID.split(",")
// You can loop thorugh the ID list
for (myid in ID)
{
println ("ID is : ${myid}")
}
}
}
}
}
}