jokarl
jokarl

Reputation: 2225

Method in Jenkinsfile exits before completing

I have a Jenkinsfile which builds the same source for several different architectures and targets, and to facilitate this I have a method that builds for me. It is called like this:

stage('Build AMD64 image') {
    steps {
        dir("${env.WORKING_DIRECTORY}") {
            script {
                buildModule('storemanager', 'Dockerfile.amd64-slim', 'storemanager-0.0.1.jar', 'amd64')
            }
        }
    }
}

And the method itself looks like this:

@NonCPS
def buildModule(module, dockerfile, jarfile, arch) {

    try {
        echo "Building ${arch} image"
        def dockerImage = docker.build("${env.IMAGE_NAME}", "--build-arg MODULE=${module} --build-arg JAR_FILE=${jarfile} --no-cache -f ${dockerfile} .")
        echo "${env.DOCKER_REGISTRY_URL} ${env.STOREMANAGER_REPOSITORY_CREDENTIALS}"
        docker.withRegistry("${env.DOCKER_REGISTRY_URL}", "${env.STOREMANAGER_REPOSITORY_CREDENTIALS}") {
            def tag
            echo "RELEASE: ${params.Release}"
            if (params.Release) {
                tag = "${BUILD_NUMBER}-${params.Version}-${arch}"
            } else {
                tag = "${BUILD_NUMBER}-${arch}"
            }
            echo "Pushing tag ${tag}"
            dockerImage.push(tag)
        }
    } catch (err) {
        currentBuild.result = 'FAILURE'
        throw err
    }
}


pipeline {
   // ....
}

This method stops running as soon as it has executed docker.build. Nothing else is printed to the console, no image is pushed to our repository. The build succeeds, but the resulting image is nowhere to be seen.

What am I doing wrong? Am I using Jenkins pipeline wrong, or is it a Groovy issue?

Upvotes: 1

Views: 119

Answers (1)

jokarl
jokarl

Reputation: 2225

The issue is the annotation @NonCPS:

When you put @NonCPS on a method, Jenkins will execute the entire method in one go without the ability to pause. Also, you're not allowed to reference any pipeline steps or CPS transformed methods from within an @NonCPS annotated method.

Found here.

Upvotes: 1

Related Questions