无法将参数从一个管道作业传递给另一管道作业

时间:2020-03-11 15:14:10

标签: jenkins jenkins-pipeline parameter-passing

编辑问题:我正在尝试运行一个简单的管道作业,该作业会触发另一个管道作业并发送参数值。 我在下面的示例中尝试了一个简化的用例

Piepeline-父母

pipeline{
agent any
options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
}
stages {
    stage('Invoke sample_pipleline') {
        steps {
                CommitID = 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'
                Branch = "master"
            input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'depServer')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'
            build job: 'simple_child', 
        parameters: [string(name: 'CommitID', value: 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'),string(name: 'Environment', value: depServer), string(name: 'Branch', value: 'master')], 
                quietPeriod: 1
        }
    }
}

}

管道-儿童

pipeline{
agent any
parameters {
    string defaultValue: '', description: 'K', name: 'depServer'
}
options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
}
stages {
    stage('CodePull') {
        steps {
            echo "Testing"
            echo "${depServer}"
        }
    }
}

}

当我运行父管道时,它没有触发子管道,但给出了错误。

    Started by user ARAV
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Windows_aubale in                 C:\Users\arav\Documents\Proj\Automation\Jenkins\Jenkins_slave_root_directory\workspace\sample_parent2
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Invoke sample_pipleline)
    [Pipeline] input
    Input requested
    Approved by ARAV
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    groovy.lang.MissingPropertyException: No such property: depServer for class: groovy.lang.Binding

在实施建议的更改并进行了一些调整之后,父作业会触发子作业,但子日志显示它未接收到传递的参数。

    Started by upstream project "sample_parent" build number 46
    originally caused by:
    Started by user ARAV
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/simple_child
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (CodePull)
    [Pipeline] echo
    Testing
    [Pipeline] echo

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

请帮助我了解我在这里做错了什么。 感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

groovy.lang.MissingPropertyException:无此类属性:depServer用于 类别:groovy.lang.Binding

这意味着您尚未定义变量depServer

通过将input步骤的结果分配给变量depServer来解决此问题:

steps {
    script {
        def input_env = input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'depServer')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'

        build job: 'simple_child', 
            parameters: [string(name: 'CommitID', value: 'aa21a592d1039cbce043e5cefea421efeb5446a5'),string(name: 'Environment', value: input_env.depServer), string(name: 'Branch', value: "master")], 
            quietPeriod: 1
    }
}

我添加了一个script块,以便能够创建和分配变量。

input实际上返回一个看起来像这样的HashMap

[depServer:QA, user:someUser]

这就是为什么我们必须写input_env.depServer作为构建作业的参数。

答案 1 :(得分:0)

如果您的子管道具有名为“ depServer”的参数:

parameters {
    string name: 'depServer', defaultValue: '', description: ''
}

您应该为此提供一个值:

build job: 'simple_child', 
    parameters: [string(name: 'depServer', value: 'SOMETHING'] 

最后,您应该解决它:

        steps {
            echo "Testing"
            echo "${params.depServer}"
        }

答案 2 :(得分:0)

非常感谢zett42和MaratC!因此,最后有效的代码如下(结合了两个答案):

父脚本:

pipeline{
    agent any
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
    }
    stages {
        stage('Invoke sample_pipleline') {
            steps {
                script{
                    def CommitID
                    def depServer
                    def Branch
                    CommitID = 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'
                    Branch = "master"
                userip = input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'input_env')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'
                depServer = userip.input_env
                echo "${depServer}"
                build job: 'simple_child', 
            parameters: [string(name: 'CommitID', value: "${CommitID}"),
                         string(name: 'Environment', value: "${depServer}"), 
                         string(name: 'Branch', value: "${Branch}")], 
                    quietPeriod: 1
                }
            }
        }
    }
}

子脚本:

pipeline{
    agent any
    parameters {
        string defaultValue: '', description: 'K', name: 'Environment'
    }
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
    }
    stages {
        stage('CodePull') {
            steps {
                echo "Testing"
                echo "${params.Environment}"
            }
        }
    }
}