从属性文件加载属性并使它们在整个作业/管道中可用-Jenkins声明性语法

伊山

我的要求很简单,我只想外部化一些“值”以使我的Jenkinsfile更可重用,为此,我需要从一个文件中加载属性,该文件将紧挨Jenkinsfile,并确保这些属性是在管道中的任何地方都可用。我对groovy和Jenkins代码仍然是陌生的,但是从未想到过如此简单的事情会如此困难。我在脚本安全性插件中启用了一些方法,但是以下代码(以及我尝试过的几种变体)始终会引起错误或显示null或给我NPE。我尝试了多种组合,下面的代码只是其中之一。

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管道中的属性。您可以根据主要差异选择选择。

1)完全使用常规代码。此代码段将要求您在脚本安全性插件随附的“进程内脚本批准”中启用多个方法签名,因此,只有在经过适当考虑后,才能进行此操作。

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)使用管道实用程序步骤插件-管道插件套件默认情况下包括此插件,它允许在无需启用安全异常的情况下更好地加载属性。我会推荐这种方法。

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
            }

        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用声明性语法在Jenkins文件中声明许多代理?

在Jenkins管道中向当前时间添加分钟(声明性语法)

Jenkins声明性管道:如何注入属性

Jenkins脚本化管道或声明性管道

Jenkins:在声明性管道中使用XmlSlurper

Jenkins:声明性管道中的未知阶段部分“矩阵”

多个节点上声明性 Jenkins 管道中的并行结帐

Jenkins声明性管道:如何从输入步骤中读取选择?

如何在声明性Jenkins管道中添加sidecar MySQL?

使用声明性管道中的jenkins凭证进行Git推送

在声明性Jenkins管道中捕获sh命令的输出

Jenkins-Textarea参数插件从属性文件设置默认值?

如何从SQLAlchemy结果中获取列名(声明性语法)

声明性 Jenkins 管道:如何从项目中删除分支而不从 svn 中删除它们?

Typescript-一些省略的实现从属性中删除了可选性

jenkins(声明性)管道在“始终”之前将操作“成功”发布?

声明性Jenkins管道,每夜部署主分支

Jenkins声明性管道的不可序列化错误

通过 jenkins 声明性管道增加 Maven 版本

Jenkins声明性管道,从Artifactory下载最新上传(构建)并获取属性

在GWT中从属性文件动态加载值

如何从 Jenkins 声明性管道中的 URL 填充 Jenkins 构建参数值

在声明性 Jenkins 管道中引用文件夹级范围凭据

从脚本返回的值未分配给在 jenkins 声明性管道阶段声明的变量

如何在roslyn的语法树中删除标记。例如从属性中删除虚拟关键字令牌?

声明性管道作业中的 Dockerfile 失败

检查是否在Jenkins管道中声明了ENV属性

如何在Jenkins声明性管道文件中使用选择参数整数值作为数组

在jenkins声明性管道中的多行sh脚本中包含参数