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 Answer
Reset to default 0This 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}.
environment
block can only be strings, whilereadYaml
returns a map. Instead just define it as a global variable outside thepipeline
directive. – Noam Helmer Commented Mar 4 at 10:52