0

I'm trying to do the following

  1. Checkout the code
  2. Do some prechecks using some other docker images (don't want to install these on Jenkins node)
  3. Build jar using docker image maven:3.6-jdk-8
  4. Then run Dockerfile to build app image
  5. Push the image to repository

Now, I don't want to install anything apart from Docker on Jenkins node. I want to run the full pipeline in Docker container to achieve this. What I'm struggling is how to build the 4th step from within the container.

I wrote the Jenkinsfile as below

pipeline {

    agent none
    
    stages {
        stage('Maven build') {
            agent {
                docker {
                    image 'maven:3.6-jdk-8'
                    args '-u root:root'
                }
            }
            steps {
                checkout(
                    [
                        $class: 'GitSCM',
                        branches: [
                            [name: '*/master']
                        ],
                        doGenerateSubmoduleConfigurations: false, 
                        extensions: [], 
                        submoduleCfg: [], 
                        userRemoteConfigs: [
                            [
                                credentialsId: '<cred-id>',
                                url: '<github-url>']
                            ]
                        ])
                        
                sh '''
                    set -eux pipefail

                    mvn -e clean install
                '''
            }
        }
        stage('Build docker image') {
             // Which docker image to use?
        }
    }
}

But I'm not sure how to build a docker image within container. The search didn't help that much. I tried using the Jenkins node for the docker image building but it seems I cannot mix and match. I totally understand this is quite an open question but I think it would be helpful to know the straightforward answer(s).

Ganesh Satpute
  • 399
  • 1
  • 3
  • 6

1 Answers1

0

I would try something like:

pipeline {

    /*
     * Run everything on an existing agent configured with a label 'docker'.
     * This agent will need docker, git and a jdk installed at a minimum.
     */
    agent {
        node {
            label 'docker'
        }
    }

    // using the Timestamper plugin we can add timestamps to the console log
    options {
        timestamps()
    }

    environment {
        //Use Pipeline Utility Steps plugin to read information from pom.xml into env variables
        IMAGE = readMavenPom().getArtifactId()
        VERSION = readMavenPom().getVersion()
    }
    
    stages {

        stage('Clone repository') {
            /* 
             * Let's make sure we have the repository cloned to our workspace 
             */
            checkout(
                    [
                        $class: 'GitSCM',
                        branches: [
                            [name: '*/master']
                        ],
                        doGenerateSubmoduleConfigurations: false, 
                        extensions: [], 
                        submoduleCfg: [], 
                        userRemoteConfigs: [
                            [
                                credentialsId: '<cred-id>',
                                url: '<github-url>']
                            ]
                        ])
        }

        stage('Maven build') {
            agent {
                docker {
                    /*
                     * Reuse the workspace on the agent defined at top-level of
                     * Pipeline but run inside a container.
                     */
                    image 'maven:3.6-jdk-8'
                    reuseNode true
                }
            }
            steps {        
                sh '''
                    set -eux pipefail

                    mvn -e clean install
                '''
            }
            post {
                success {
                /* 
                 * Only worry about archiving the jar file 
                 * if the build steps are successful (this part may be not necessary)
                 */
                archiveArtifacts(artifacts: '**/target/*.jar', allowEmptyArchive: true)
                }
           }
        }
        stage('Build docker image') {
             steps {
                sh '''
                    docker build -t ${IMAGE} .
                    docker tag ${IMAGE} ${IMAGE}:${VERSION}
                    docker push ${IMAGE}:${VERSION}
                '''
            }
        }
    }
}

The reuseNode option should allow you to run maven build in the container even though the checkout and Docker build are run on the node itself. See documentation here.

Konrad Botor
  • 141
  • 4