jenkinsjenkins-pipeline

How to build docker images using a Declarative Jenkinsfile


I'm new to using Jenkins....

I'm trying to automate the production of an image (to be stashed in a repo) using a declarative Jenkinsfile. I find the documentation to be confusing (at best). Simply put, how can I convert the following scripted example (from the docs)

node {
    checkout scm
    def customImage = docker.build("my-image:${env.BUILD_ID}")
    customImage.push()
}

to a declarative Jenkinsfile....


Solution

  • You can use scripted pipeline blocks in a declarative pipeline as a workaround

    pipeline {
        agent any
        stages {
            stage('Build image') {
                steps {
                    echo 'Starting to build docker image'
    
                    script {
                        def customImage = docker.build("my-image:${env.BUILD_ID}")
                        customImage.push()
                    }
                }
            }
        }
    }