最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jenkins pipeline - In Jenkinsfile, how do I set environment variables from parsed YAML file? - Stack Overflow

programmeradmin0浏览0评论

My Jenkinsfile looks like this:

pipeline {
    agent any

    parameters {
        choice( name: 'project_short_code', description: 'The short code for this project', choices: ['foo','bar'])
    }

    environment {
        image = 'base_machine:0.2.11'
        projects = readYaml file: "build_scripts/9999-build_properties_for_project_code.yaml"
        base_domain_deploy = projects.project_code."${project_short_code}".base_domain_deploy
    }

    etc., . . . 
}

But I am getting errors which look like this:

WorkflowScript: 29: Environment variable values must either be single quoted, double quoted, or function calls. @ line 29, column 30.
           base_domain_deploy = projects.project_code."${project_short_code}".base_domain_deploy
                                ^

I will need access to this variable throughout the scope of the Jenkinsfile. How can I get this done?

UPDATE

I was unable to make Noam Helmer's suggestion work for me. Anything defined outside the scope of the pipeline block threw errors. Anything inside that scope required being wrapped in a stage, step or script. Currently my Jenkinsfile looks like this:

pipeline {
    agent any

    parameters {
        choice( name: 'project_short_code', description: 'The short code for this project', choices: ['foo','bar'])
    }

    stages {
    }
        stage('Clone site code to build envornment') {
            steps {
                script {
                    def projects = readYaml file: "build_scripts/9999-build_properties_for_project_code.yaml"
                    def our_project_repo = projects.project_code."${project_short_code}".our_project_repo
                    def site_deploy_target = projects.project_code."${project_short_code}".site_deploy_target

                }
                dir("./${site_deploy_target}") {
                    git branch: "${site_tag}", credentialsId: "${ssh_credentials}", url: "${our_project_repo}" 
                }
            }
        }
    }
}

And that is throwing errors looking like this:

Also:   .jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 5357bb99-cc95-4599-b332-b531d2f66c35
groovy.lang.MissingPropertyException: No such property: our_project_repo for class: groovy.lang.Binding

$our_project_repo seems to fall out of scope inside the same stage{} / steps{} block. Why is that, please? And what can I do about it?

Thanks, -- Hugh

My Jenkinsfile looks like this:

pipeline {
    agent any

    parameters {
        choice( name: 'project_short_code', description: 'The short code for this project', choices: ['foo','bar'])
    }

    environment {
        image = 'base_machine:0.2.11'
        projects = readYaml file: "build_scripts/9999-build_properties_for_project_code.yaml"
        base_domain_deploy = projects.project_code."${project_short_code}".base_domain_deploy
    }

    etc., . . . 
}

But I am getting errors which look like this:

WorkflowScript: 29: Environment variable values must either be single quoted, double quoted, or function calls. @ line 29, column 30.
           base_domain_deploy = projects.project_code."${project_short_code}".base_domain_deploy
                                ^

I will need access to this variable throughout the scope of the Jenkinsfile. How can I get this done?

UPDATE

I was unable to make Noam Helmer's suggestion work for me. Anything defined outside the scope of the pipeline block threw errors. Anything inside that scope required being wrapped in a stage, step or script. Currently my Jenkinsfile looks like this:

pipeline {
    agent any

    parameters {
        choice( name: 'project_short_code', description: 'The short code for this project', choices: ['foo','bar'])
    }

    stages {
    }
        stage('Clone site code to build envornment') {
            steps {
                script {
                    def projects = readYaml file: "build_scripts/9999-build_properties_for_project_code.yaml"
                    def our_project_repo = projects.project_code."${project_short_code}".our_project_repo
                    def site_deploy_target = projects.project_code."${project_short_code}".site_deploy_target

                }
                dir("./${site_deploy_target}") {
                    git branch: "${site_tag}", credentialsId: "${ssh_credentials}", url: "${our_project_repo}" 
                }
            }
        }
    }
}

And that is throwing errors looking like this:

Also:   .jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 5357bb99-cc95-4599-b332-b531d2f66c35
groovy.lang.MissingPropertyException: No such property: our_project_repo for class: groovy.lang.Binding

$our_project_repo seems to fall out of scope inside the same stage{} / steps{} block. Why is that, please? And what can I do about it?

Thanks, -- Hugh

Share Improve this question edited Mar 9 at 15:24 Hugh Esco asked Mar 3 at 19:40 Hugh EscoHugh Esco 671 silver badge6 bronze badges 1
  • 1 The variables defined in the environment block can only be strings, while readYaml returns a map. Instead just define it as a global variable outside the pipeline directive. – Noam Helmer Commented Mar 4 at 10:52
Add a comment  | 

1 Answer 1

Reset to default 0

This video provided the guidance I was looking for:
https://www.youtube/watch?v=KwQDxwZRZiE

And my solution looks like this:

pipeline {
    agent any

    parameters {
        choice( name: 'project_short_code', description: 'The short code for this project', choices: ['foo','bar'])
    }

    stages {
    }
        stage('Clone site code to build envornment') {
            steps {
                script {
                    def projects = readYaml file: "build_scripts/9999-build_properties_for_project_code.yaml"
                    env.our_project_repo = projects.project_code."${project_short_code}".our_project_repo
                    env.site_deploy_target = projects.project_code."${project_short_code}".site_deploy_target
                }
                dir("./${env.site_deploy_target}") {
                    git branch: "${site_tag}", credentialsId: "${ssh_credentials}", url: "${env.our_project_repo}" 
                }
            }
        }
    }
}

Apparently, an assignment to an env.FOO requires no def keyword, and, at least in the same context, can be accessed as ${env.FOO}.

发布评论

评论列表(0)

  1. 暂无评论