MartinP
MartinP

Reputation: 93

Jenkins pipeline script variables being overwritten?

I have a jenkins pipeline script that runs various test suites in parallel over multiple nodes. I'm not a jenkins expert - most of this is copy and paste from other jobs we have.

I am occasionally getting failures where the archiveArtefacts command has somehow gotten the wrong 'tar_file' variable.

Normally, the suite name is built into the tar file of logs, then the tar file is archived to Jenkins, but for some runs, the tar file gets created with one suite name, then the archive step has a different name (I've seen runs where 2 or 3 of the parallel steps fail with this sort of error, and some where only 1 fails).

So somehow, between sh("tar -czf ${tar_file} ${host_logs}/*") and archiveArtifacts artifacts: tar_file the value of tar_file has changed to that of a different suite.

Any thoughts on how I can change this so that the tar_file stays constant in each step?

try {
    stage('cuke_regression') {
        def stepsForCuke = [:]
        stepsForCuke['cuke_api'] = sectionCukeRegressionTests("api")
        stepsForCuke['cuke_admin'] = sectionCukeRegressionTests("admin")
        stepsForCuke['cuke_notification'] = sectionCukeRegressionTests("notification")
        stepsForCuke['cuke_public'] = sectionCukeRegressionTests("public")
        stepsForCuke['cuke_project'] = sectionCukeRegressionTests("project")
        stepsForCuke['cuke_group'] = sectionCukeRegressionTests("group")
        stepsForCuke['cuke_review'] = sectionCukeRegressionTests("review")
        stepsForCuke['cuke_workflow'] = sectionCukeRegressionTests("workflow")
        stepsForCuke['cuke_review_comment'] = sectionCukeRegressionTests("review_comment")
        parallel stepsForCuke
    }
} 

def sectionCukeRegressionTests(suite) {
    section = 'cukes'
    return {
        section: {
            node('docker-node') {
                tar_file = "cukes-"+suite+"-logs.tgz "
                try {
                        sh("docker-compose exec ... run the tests \"")
                        sh("tar -czf ${tar_file} ${host_logs}/*")
                } finally {
                    sh("docker-compose down")
                    archiveArtifacts artifacts: tar_file
                }
            }
        }
    }
}

Upvotes: 0

Views: 1247

Answers (1)

MartinP
MartinP

Reputation: 93

Dibakar's suggestion of making the tar_file variable a 'def' variable has fixed the problem for me.

Upvotes: 2

Related Questions