Set local variables from YAML files in a Jenkins Pipeline parallel block

I have Jenkins Groovy running declarative Pipeline with a parallel block. The parallel stages run on different agents with different build processes. I want my Groovy to load variables from YAML files stored in the code bases, for use in the shell scripts that run the builds. And the variables need to be set differently for the different agents.

I’ve tried to distill the issue down to show what I’m trying to do, hopefully it’s not still too lengthy to ask for help here. In my actual projects, the .groovy files in vars are in a shared library repository, while the Jenkinsfile and the YAML files are in each code repository.

I’ve set sleeps to force a race condition. With the sleeps, I’m having it first do the reconfiguration on Patience, then the reconfiguration on Fortitude, then show the variables on Patience, and then finally show the variables on Fortitude.

It seems that when an environment variable on one agent is set using env.*, the environment variable is also set on the other agent, even though it’s an entirely different Jenkins build node. Like spooky action at a distance, there.

At the end of the post below, I have the expected output, and what I’m actually getting instead.

I have tried adding env. in front of SYSTEM_OS in the reconfiguration stages, but that seems to set a global variable that overrides the other agent. Having no env. seems to set a local variable that’s scoped the the stage only, not to the parallel stage for the agent.

In localPipelineConfig.groovy I tried using "$local_setting_item.key" =, but that gives the error "$local_setting_item.key" is a GString expression, but it should be a variable expression. So I tried using def "$local_setting_item.key" =, but that gives the error unexpected token: def.

Jenkinsfile

library identifier: 'shared-library@version', retriever: legacySCM(scm)
PatienceAndFortitudeParallelPipeline()

vars/PatienceAndFortitudeParallelPipeline.groovy

def call() {
    pipeline {
        agent { label 'ubuntu-16.04' }
        stages {
            stage('Configuration') {
                steps {
                    script {
                        globalPipelineConfig("globalSettings.yml")
                        env.SERVER = "builder.example.com"
                    }
                }
            }
            stage('Patience and Fortitude in parallel') {
                parallel {
                    stage('Patience') {
                        agent { label 'ubuntu-16.04' }
                        stages {
                            stage('Patience - Reconfiguration') {
                                steps {
                                    script {
                                        localPipelineConfig('patienceSettings.yml')
                                        SYSTEM_OS = 'ubuntu'
                                    }
                                }
                            }
                            stage('Patience - Do some work') {
                                steps {
                                    sleep 2
                                    sh '''
                                    echo "Project: $PROJECT"
                                    echo "Server: $SERVER"
                                    echo "Python version: $PYTHON_VERSION"
                                    echo "OS: $SYSTEM_OS"
                                    '''
                                }
                            }
                            stage('Patience - Do more work') {
                                steps {
                                    sh '''
                                    $WORKSPACE/project_builder_patience.sh
                                    '''
                                }
                            }
                        }
                    }
                    stage('Fortitude') {
                        agent { label 'amazon_linux2' }
                        stages {
                            stage('Fortitude - Reconfiguration') {
                                steps {
                                    sleep 1
                                    script {
                                        localPipelineConfig('fortitudeSettings.yml')
                                        SYSTEM_OS = 'al2'
                                    }
                                }
                            }
                            stage('Fortitude - Do some work') {
                                steps {
                                    sleep 2
                                    sh '''
                                    echo "Project: $PROJECT"
                                    echo "Server: $SERVER"
                                    echo "Python version: $PYTHON_VERSION"
                                    echo "OS: $SYSTEM_OS"
                                    '''
                                }
                            }
                            stage('Fortitude - Do more work') {
                                steps {
                                    sh '''
                                    $WORKSPACE/project_builder_fortitude.sh
                                    '''
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

vars/globalPipelineConfig.groovy

def call(configFileName) {
    Map pipelineCfg = readYaml(file: "${WORKSPACE}/${configFileName}")
    for (global_setting_item in pipelineCfg.global_settings) {
        env."$global_setting_item.key" = global_setting_item.value
    }
    return
}

globalSettings.yml

global_settings:
  PROJECT: "services"

vars/localPipelineConfig.groovy

def call(configFileName) {
    Map pipelineCfg = readYaml(file: "${WORKSPACE}/${configFileName}")
    for (local_setting_item in pipelineCfg.local_settings) {
        env."$local_setting_item.key" = local_setting_item.value
    }
    return
}

patienceSettings.yml

local_settings:
  PYTHON_VERSION: python3.10

fortitudeSettings.yml

local_settings:
  PYTHON_VERSION: python3.11

project_builder_patience.sh

#!/bin/bash
echo "I would build Patience."

project_builder_fortitude.sh

#!/bin/bash
echo "I would build Fortitude."

Desired output on patience node:

Project: services
Server: builder.example.com
Python version: python3.10
OS: ubuntu
I would build Patience.

Actual output on patience node:

Project: services
Server: builder.example.com
Python version: python3.11
OS: 
I would build Patience.

Desired output on fortitude node:

Project: services
Server: builder.example.com
Python version: python3.11
OS: al2
I would build Fortitude.

Actual output on fortitude node:

Project: services
Server: builder.example.com
Python version: python3.11
OS: 
I would build Fortitude.

New contributor

Paul Nickerson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật