0

I have Jenkins CICD pipeline and I need to read a config file which has key value pairs of Project that needs to be build with the tag number.

I need to read though this file and execute Jenkins pipeline steps in Loop.

Currently my Jenkins CICD pipeline works for single build. I have issues when trying to read a config file and loop though the steps.

Below is a sample of the code i am trying to achieve:

pipeline{
    agent any    
    environment {
        buildApp = "$ApplicationToBuild"
        cloudEnvironment = "$ENV"
        TIMESTAMP = new java.text.SimpleDateFormat('yyyyMMddHHmmss').format(new Date())
        WORKSPACE="${env.WORKSPACE}"
    }
    stages {
        stage ('Validation step for deployment') {      
            steps {
        script {
                    sh 'line_count=$( wc -l applicationSettings.config )'
                    echo 'line count is $line_count'
                    for (int lines = 0; lines < ${line_count}; lines++) {
                        gitAppRepo=""
                        gitAppTag=""
                        gitAppRepo=$(echo $lines |  sed 's/=.*//')
                        echo "gitAppRepo is $gitAppRepo"
                        gitAppTag=$(grep "^$gitAppRepo=" ./applicationSettings.config |cut -d= -f2)
                        echo "gitAppTag is $gitAppTag"                      
                    }
                    }
                }
            }
    }
    post {
        always {
            echo 'One way or another, I have finished'
            }
    }
}

I using the line count and looping though the config file to get the app to deploy and the tag. The actual deployment would be called in another jenkins file which contains all the steps.

Below error is encountered in the above loop. Is there any eligent method to loop though in groovy?

java.lang.NoSuchMethodError: No such DSL method '$' found among steps

And how do we call another JenkinsFile in the same project. Below is my file structure. I need to call the Jenkins_files_main in Jenkins_files.

Jenkins_files
README.md
applicationSettings.config
Jenkins_files_main
hakamairi
  • 4,464
  • 4
  • 30
  • 53
VN-
  • 27
  • 6
  • You are wrong to think that if you set a variable in sh step it would be available in your pipeline. Those are separate contexts. Also there's a difference between single quote and double quote in groovy. https://stackoverflow.com/questions/6761498/whats-the-difference-of-strings-within-single-or-double-quotes-in-groovy Depending on you file type, this question could already be answered here https://stackoverflow.com/questions/39619093/how-to-read-properties-file-from-jenkins-2-0-pipeline-script – hakamairi Feb 18 '19 at 09:43
  • Thanks for commenting. The readJSON works fine in this case. – VN- Feb 18 '19 at 10:59
  • Interesting, you never mentioned you are reading a JSON file. – hakamairi Feb 18 '19 at 11:16
  • It was not initially, but its nothing but key value so json will work fine in my case. – VN- Feb 18 '19 at 11:21

1 Answers1

0

First of all, you have used ${line_count} which only works if you are running a shell script (since it looks for environment variable). Other than that you use it as "${line_count}" or simply line_count. Then for this you should have read the output of the command in a variable like :

def line_count= sh(script: "wc -l applicationSettings.config",
                   returnStdout: true).trim()

So your for loop would become:

for (int lines = 0; lines < line_count; lines++)

Then, there is also a better way to read a json file, like this:

    def object = readJSON file: "your.json"


            for (key in object.element.keySet()) {
               echo "key=${key}"
               echo "value= ${object.element.get(key)}"
}

Hope this helps.

Siddhant Mishra
  • 488
  • 2
  • 12
  • Thanks, I am new to groovy and this helps. The json works just fine. Btw any suggestion how to call another jenkinsfile from current file? – VN- Feb 18 '19 at 10:48
  • Yes, the best way is to `load('fileName.groovy')` or assign it in a variable like `common=load('commons.groovy')` and use the functions defined in it like : `common.func()` – Siddhant Mishra Feb 18 '19 at 11:58