【问题标题】:Jenkins Declarative Piplines- Script is unable to pickup parameter as varibleJenkins 声明式管道 - 脚本无法将参数作为变量获取
【发布时间】:2022-01-06 15:57:12
【问题描述】:

我是詹金斯的新手。尝试创建一个使用基于选择的参数的基本管道。以下是我的脚本。

代码----

  pipeline{
     agent {
        label 'agent'
           } 
     parameters {
        choice choices: ['John', 'Stacy'], description: 'Choose one', name: 'Person'
         }
     stages {
        stage('Print') {
            steps {
               echo "Hello ${params.Person}"
               sh """if (${params.Person} = "John")
                     then
                       echo "Person is male."
                     else
                        echo "Person is female."
                     fi"""
                }
                    }
     } 
  }

现在无论我选择什么选项,我的构建都会成功完成。它总是显示结果“人是女性。

以下是我的一个构建的结果。

Started by user ****
[Pipeline] Start of Pipeline
[Pipeline] node
Running on agent in 
 /home/temp/jenkins_agent/workspace/ChoiceBased PL
 [Pipeline] {
 [Pipeline] stage
 [Pipeline] { (Print)
 [Pipeline] echo
 Hello John
 [Pipeline] sh
 + John = John
  /home/temp/jenkins_agent/workspace/ChoiceBased PL@tmp/durable- 
 b7e98c46/script.sh: 1: John: not found
 + echo Person is female.
 Person is female.
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] }
 [Pipeline] // node
 [Pipeline] End of Pipeline

完成:成功

请建议我缺少什么?

【问题讨论】:

    标签: jenkins jenkins-pipeline parameter-passing


    【解决方案1】:

    我会改变它只是为了在 Groovy 中而不是在 sh 中进行比较

        stage('Print') {
            steps {
               echo "Hello ${params.Person}"
               script {
                   if (params.Person == "John") {
                       echo "Person is male."
                   } else {
                       echo "Person is female."
                   }
                }
            }
        }
    

    那么当你选择 Stacey 你会得到

    [Pipeline] echo
    Hello Stacy
    [Pipeline] script
    [Pipeline] {
    [Pipeline] echo
    Person is female.
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    

    【讨论】:

    • 谢谢。它有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2018-11-10
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多