JenkinsFile
In this tutorial I am going to show you how to read YAML/YML config file in 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. For example, you want to avoid conditional statements in JenkinsFile for deploying your applications into PCF environment, so you want to read the config file according to profile. Therefore profile based config file is read into JenkinsFile for deploying app into different environments.
Manifest File
You might have seen how to create PCF app manifest file, here I am going to use the similar manifest.yml file which can be read into JenkinsFile.
Let’s assume the following content in the manifest.yml file:
---
applications:
- name: service-api
target: https://api.dev.roytuts.com/
space: app-dev
organization: roytuts-com
disk_quota: 1024M
memory: 1024M
instances: 2
buildpacks:
- java-buildpack-openjdk-v4-49-1
stack: cflinuxfs3
routes:
- route: dev.roytuts.com/serviceapi-dev
services:
- svcmysql
env:
SPRING_PROFILE: dev
JAVA_OPTS: -Duser.timezone=America/New_York -Dappdynamics.http.proxyHost=proxy.roytuts.com -Dappdynamics.http.proxyPort=8080 -Dappdynamics.agent.applicationName=ServiceAPI -Dappdynamics.agent.tierName=service-api-dev
PROXY_HOST: proxy.roytuts.com
ssl_enabled: true
If you want to know about various tags used in the above manifest file then read the tutorial about PCF manifest file.
Read YAML/YML File
Now I will show you how to read yaml/yml config file in JenkinsFile. There are various ways you can read yaml file in JenkinsFile. You can use any one of the following blocks to read your yml config file:
stage("Read Manifest Config") {
node {
...
}
}
Or
node {
stage("Read Manifest Config") {
...
}
}
Or
stage("Read Manifest Config") {
node {
script {
...
}
}
}
Or
node {
stage("Read Manifest Config") {
script {
...
}
}
}
To read yaml or yml file inside any one of the above blocks, use the following line of code:
def configVal = readYaml file: "full path of the yml file"
For example, the following code reads a yml file:
stage("Read Manifest Config") {
node {
def configVal = readYaml file: "manifest.yml"
}
}
Now the configVal variable will have the values from the manifest.yml file. The configVal variable holds the value from the manifest.yml file in the following manner:
[applications:[[name:service-api, target:https://api.dev.roytuts.com/, space:app-dev, organization:roytuts-com, disk_quota:1024M, memory:1024M, instances:2, buildpacks:[java-buildpack-openjdk-v4-49-1], stack:cflinuxfs3, routes:[route:dev.roytuts.com/serviceapi-dev], services:[svcmysql], env:[SPRING_PROFILE:dev, JAVA_OPTS:-Duser.timezone=America/New_York -Dappdynamics.http.proxyHost=proxy.roytuts.com -Dappdynamics.http.proxyPort=8080 -Dappdynamics.agent.applicationName=ServiceAPI -Dappdynamics.agent.tierName=service-api-dev, PROXY_HOST: proxy.roytuts.com, ssl_enabled: true]]]]
If you want to see the value of the configVal then you can echo its value in the JenkinsFile.
stage("Read Manifest Config") {
node {
def configVal = readYaml file: "manifest.yml"
echo "configVal: " + configVal
}
}
Nest you can access individual field’s value, for example, you can access name under applications tag using the following manner configVal['applications']['name'][0]
:
stage("Read Manifest Config") {
node {
def configVal = readYaml file: "manifest.yml"
echo "configVal: " + configVal
echo configVal['applications']['name'][0]
}
}
Next you may want to use the config value in the sh section of the JenkinsFile. sh """ ... """
section is used to execute the shell commands.
You cannot use the configVal directly inside the sh section. Therefore you need to use environment variable to set the config value and later use it.
...
env.APP_NAME = configVal['applications']['name'][0]
...
Now you can use the variable APP_NAME inside the sh section. To get the value of APP_NAME use like ${APP_NAME}.
For example, if you want to use cf push
command then you need to use app name, buildpack, artifact name, stack.
First read the required values from config and set them into env:
stage("Read Manifest Config") {
node {
def configVal = readYaml file: "manifest.yml"
echo "configVal: " + configVal
env.APP_NAME = configVal['applications']['name'][0]
env.STACK = configVal['applications']['stack'][0]
env.BUILD_PACK = configVal['applications']['buildpacks'][0][0]
}
}
Use the cf push command:
stage("Deploy") {
node {
sh '''
...
cf push ${APP_NAME} -b ${BUILD_PACK} -p build/libs/artifact_name.jar -s ${STACK} --no-start
...
'''
}
}
Hope you got an idea how to read yaml/yml config values in JenkinsFile.