Read_PropertiesFile_Using_Jenkinsfile

Devops Article#32: Read properties file from Jenkinsfile

What you will Learn:

  • Read properties file from Jenkins pipeline

Read properties file from Jenkins pipeline

Install ‘Pipeline​​ Utility Steps’ plugin in jenkins

Create a new file ‘demofile.properties’ in Github (having below key/value pair contents) and commit it

Copy the repository url as shown below

Create a new pipeline job and click ‘Pipeline syntax’.

Also Check:​​ Selenium Webdriver Training

Select ‘checkout: Check out from version control’ from ‘Sample Step’ dropdown and select ‘Git’ from SCM dropdown

Next, paste the repository url in the ‘Repository URL’ field as shown below​​ 

Next, since our branch is ‘main’

Change */master to */main

Click ‘Generate​​ Pipeline Script’

So this code snippet will help us checkout the scm repository in our local windows machine.

Copy the above snippet

Create a stage as shown below and paste the code snippet (line#7)

Below is the code snippet for ready reference:

checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/W2AGit/JenkinsPipelineAsACode.git']]])

Next, in the pipeline syntax, select ‘readProperties’ option as shown below

You can click the question mark help icon to read more as shown below

Click ‘Generate Pipeline Script’​​ 

Next, define a global variable ‘prop’ (see line#1 below).

Also create a new stage (lines 10-15). In line#12, we are reading the properties file ‘demofile.properties’ and storing its contents in the ‘prop’ variable.​​ 

In line#14, we are simply printing the value of one of the key viz app.name

Apply and Save

Build Now, wait for the successful job completion

Open console output to see logs.

Notice below that the value of app.name property is successfully printed as ‘demofile’

The value matches with file in Github

Similarly, we can read other keys as shown below

Apply and Save

Build Now, wait for the successful job completion

Below is the complete code:

def prop;

node

{

 ​​ ​​ ​​​​ stage('checkout from SCM')​​ 

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/W2AGit/JenkinsPipelineAsACode.git']]])

 ​​ ​​ ​​​​ }  ​​ ​​​​ 

 ​​ ​​ ​​​​ stage('Reading properties​​ file')​​ 

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ prop = readProperties file: 'demofile.properties'

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "The name of the webapp is: ${prop['app.name']}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "The build environment is: ${prop['environment.name']}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "The app version is: ${prop['app.version.number']}"

 ​​ ​​ ​​​​ }

}

This is how we can read the properties file using pipeline.

Thank you for reading!

Also Check:​​ Selenium Tutorials

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024