0

in jenkins declarative pipeline is there a way to execute pre condition whereby it should load build parameters from a file. In Jenkins there is an option whereby we can restart individual stage. Therefore, i wish for each stage to load the parameters from groovy file.

Currently is

pipeline {
    agent any

        stage("Grep the values") {
            steps {
                load "${WORKSPACE}/file-parameter.groovy"
            }
        }


        stage("Perform Deploynment) {
            when {
                expression { "${Perform_Deployment}" == "true" }
            }

            steps {
                withCredentials([
                    usernamePassword(credentialsId: "LoginID", passwordVariable: "LoginPassword", usernameVariable: "LoginUser")
                ]) {
                    ansiblePlaybook (
                        playbook: "${WORKSPACE}/ansible-playbook.yml",
                        forks: 5,
                        extraVars: [
                            loginUser: "${LoginUser}",
                            loginPassword: "${LoginPassword}"
                        ]
                    )
                }
            }
        }
    }
}

How can i load "${WORKSPACE}/file-parameter.groovy" in teh stage before when condition. My expectation should be somethign as below

pipeline {
    agent any

        stage("Grep the values") {
            steps {
                load "${WORKSPACE}/file-parameter.groovy"
            }
        }


        stage("Perform Deploynment) {
            load "${WORKSPACE}/file-parameter.groovy"
            when {

                expression { "${Perform_Deployment}" == "true" }
            }

            steps {
                withCredentials([
                    usernamePassword(credentialsId: "LoginID", passwordVariable: "LoginPassword", usernameVariable: "LoginUser")
                ]) {
                    ansiblePlaybook (
                        playbook: "${WORKSPACE}/ansible-playbook.yml",
                        forks: 5,
                        extraVars: [
                            loginUser: "${LoginUser}",
                            loginPassword: "${LoginPassword}"
                        ]
                    )
                }
            }
        }
    }
}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
valene
  • 5
  • 4

1 Answers1

0

The load step returns whatever the groovy script returned when it was executed, so you need to store it in a variable

file-parameter.groovy could either look like this:

return [
    performDeployment: true,
    // other variables
]

or like this

performDeployment = true

// other variables and methods

return this

In both cases you could use it in your pipeline like so:

stage("Grep the values") {
    steps {
        script {
            fileParams = load("${WORKSPACE}/file-parameter.groovy")
        }
    }
}

stage("Perform Deploynment) {
    when {
        expression { fileParams.performDeployment }
    }

I am pretty sure there is no need for the string comparison you are doing and you could use just the boolean value instead.

smelm
  • 1,253
  • 7
  • 14
  • i tried both, but when i restart stage i hitting error org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String performDeployment. If just click build no issue. Issue happen when i restart stage – valene Sep 28 '20 at 02:20
  • Somehow fileParams is a string in your case and not a map as it should be. Did you try printing it to the console? But you said it worked when you started it normally? – smelm Sep 28 '20 at 09:24
  • yea correct. the issue happens only when restart stage. In declarative pipeline we can restart individual stage. For instance, if we restart Perform Deploynment in jenkins, then issue happens – valene Sep 30 '20 at 08:26
  • Did you try to add a print statement? fileParams seems to be of the wrong type and I wonder what the exact content is. Could you print out the variable and reproduce the error? – smelm Sep 30 '20 at 09:07