从属性文件加载属性并使它们在整个作业/管道中可用 - Jenkins 声明性语法 [英] Load properties from properties file and make them available throughout the job/pipeline - Jenkins declarative syntax

查看:23
本文介绍了从属性文件加载属性并使它们在整个作业/管道中可用 - Jenkins 声明性语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求很简单,我只想外化一些值"以使我的 Jenkinsfile 更可重用,为此我需要从 Jenkinsfile 旁边的文件中加载属性,并确保这些属性在管道中的任何地方都可用.我对 groovy 和 Jenkins 代码仍然很陌生,但从未想过这么简单的事情会如此困难.我在脚本安全插件中启用了一些方法,但以下代码(以及我尝试过的几种变体)总是会出现错误或打印 null 或给我 NPE.我尝试了多种组合,下面的代码只是其中之一.

My requirement is simple, i just want to externalize some 'values' to make my Jenkinsfile more re usable and for this i need to load the properties from a file which is going to be right next to Jenkinsfile, and make sure that these properties are available anywhere in the pipeline. I am still new to groovy and Jenkins code but never thought such a simple thing would be so difficult. I enabled some methods in script security plugin but the following code (and several variations i tried around it) always pose errors or print null or give me NPE. I have tried multiple combinations and the below code is just one of them.

properties = null

@NonCPS
def loadProperties() {
    checkout scm
    File propertiesFile = new File('${workspace}/pipeline.properties')
    propertiesFile.withInputStream {
            properties.load(propertiesFile)
    }
}

pipeline {
    agent none

    stages {

        stage ('prepare') {
            agent any

            steps {
                script {
                loadProperties()
                echo "${properties['repo']}"
                }
            }
        }
        stage('Build') {

            agent any

            steps {
                sh 'echo ${properties.repo}'
            }

        }
    }
}

推荐答案

我想出了几种方法来外部化 Jenkins 管道中的属性.您可以根据主要区别进行选择.

I figured out a couple of ways to externalize properties in Jenkins pipelines. You can choose your pick based on the main difference.

1) 完全使用 groovy 代码.此代码段将要求您在脚本安全插件附带的进程内脚本批准"中启用多个方法签名,因此只有在适当考虑后才能这样做.

1) Using groovy code entirely. This code snippet would require you to enable several method signatures in 'In-process script approval' which comes in with script security plugin, and hence this should be done only after due consideration.

properties = null     

def loadProperties() {
    node {
        checkout scm
        properties = new Properties()
        File propertiesFile = new File("${workspace}/pipeline.properties")
        properties.load(propertiesFile.newDataInputStream())
        echo "Immediate one ${properties.repo}"
    }
}

pipeline {
    agent none

    stages {
        stage ('prepare') {
            agent any

            steps {
                script {
                    loadProperties()
                    echo "Later one ${properties.branch}"
                }
            }
        }
        stage('Build') {
            agent { label 'master'  }

            steps {
                // works fine. properties is available everywhere
                echo properties.branch
            }           
        }
    }
}

2) 使用管道实用程序步骤插件 - 管道插件套件默认包含此功能,它允许以更好的方式加载属性而无需启用安全异常.我会推荐这种方法.

2) Using pipeline utility steps plugin - Pipeline suite of plugins include this by default and it allows a better way to load properties without requiring to enable security exceptions. I would recommend this method.

properties = null

def loadProperties() {
    node {
        checkout scm
        properties = readProperties file: 'pipeline.properties'
        echo "Immediate one ${properties.repo}"
    }
}

pipeline {
    agent none

    stages {           
        stage ('prepare') {
            agent any

            steps {
                script {
                    loadProperties()
                    echo "Later one ${properties.ansible}"
                }
            }
        }
        stage('Build') {

            agent any

            steps {
                echo properties.branch
            }

        }
    }
}

这篇关于从属性文件加载属性并使它们在整个作业/管道中可用 - Jenkins 声明性语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆