Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read property from config file inside Jenkins pipeline using Config File Provider Plugin

I want to parametrize my Jenkins pipeline with a simple properties config file

skip_tests=true

that I've added to Jenkins Config File Managment:

config file management screenshot

In my pipeline I'm importing this file and try to read from it using the Jenkins Pipeline Config File Plugin.

node('my-swarm') {

 MY_CONFIG = '27206b95-d69b-4494-a430-0a23483a6408'

 try {

     stage('prepare') {
         configFileProvider([configFile(fileId: "$MY_CONFIG", variable: 'skip_tests')]) {
             echo $skip_tests
             assert $skip_tests == 'true'
         }
     }
 } catch (Exception e) {
     currentBuild.result = 'FAILURE'
     print e
 }
}

This results in an error:

provisioning config files...
copy managed file [my.properties] to file:/home/jenkins/build/workspace/my-workspace@tmp/config7043792000148664559tmp
[Pipeline] {
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // configFileProvider
[Pipeline] }
[Pipeline] // stage
[Pipeline] echo
groovy.lang.MissingPropertyException: No such property: $skip_tests for 
class: groovy.lang.Binding

Any ideas what I'm doing wrong here?

like image 869
Robert Avatar asked Apr 29 '19 13:04

Robert


People also ask

What configuration files can be used in Jenkins pipeline?

A common practice would be to define a global settings.xml to provide credentials and repository definitions and have local (default) settings.xml define the location of the local repo. The configuration files can be used in Jenkins pipeline.

What is the use of jenkinsfile?

The JenkinsFIle is used for Jenkins pipeline which is used to perform many steps such as, building, testing, deploying a job through Jenkins. The yaml/yml config file can be a PCF manifest file or any other config file and you need to read this config file in JenkinsFile for serving various purpose.

Can providerid be null when using configfiles in Jenkins?

JENKINS-43372 IllegalArgumentException: providerId can NOT be null when using configFiles JENKINS-42262 Provide a method where extension could implement own logic for the content of custom Config Version 2.15.7 (17. March 2017) JENKINS-42389 Modifying Folder configuration removes all config files Version 2.15.6 (13. Feb. 2017)

How to implement read configurations from the property file?

Let us learn how to implement the configurations from the property file. We will follow the below steps and implement Read Configurations: Firstly, right-click on the root Project and select New >> Folder . Additionally, name it as configs. Moreover, we will keep config files with in the same folder.


2 Answers

With the help of the other answers and How to read properties file from Jenkins 2.0 pipeline script I found the following code to work:

configFileProvider([configFile(fileId: "$PBD1_CONFIG", variable: 'configFile')]) {
     def props = readProperties file: "$configFile"
     def skip_tests = props['skip_tests']
     if (skip_tests == 'true') {
        print 'skipping tests'
     } else {
        print 'running tests'
     }
}

I had to use readProperties from Jenkins' Pipeline Utility Steps Plugin.

like image 141
Robert Avatar answered Oct 07 '22 14:10

Robert


Since the file is in property format you can use it in a shell step:

sh """
  source ${MY_CONFIG}
  .
  .
  .
"""

You would need to export the properties that need to be available on programs that the shell calls (e.g. Maven)

like image 40
Rich Duncan Avatar answered Oct 07 '22 15:10

Rich Duncan