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

查看:77
本文介绍了从属性文件加载属性并使它们在整个作业/管道中可用-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)完全使用常规代码.此代码段将要求您在脚本安全性插件随附的进程内脚本批准"中启用多个方法签名,因此,只有在经过适当考虑后,才能进行此操作.

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天全站免登陆