Jenkins Pipeline: cree la ventana acoplable dentro del contenedor de la ventana acoplable

Jenkins Pipeline: cree la ventana acoplable dentro del contenedor de la ventana acoplable

Estoy intentando hacer lo siguiente

  1. Consulta el código
  2. Realice algunas comprobaciones previas utilizando otras imágenes de la ventana acoplable (no quiero instalarlas en el nodo de Jenkins)
  3. Construir jar usando la imagen de la ventana acoplablemaven:3.6-jdk-8
  4. Luego ejecute Dockerfilepara crear la imagen de la aplicación.
  5. Enviar la imagen al repositorio

Ahora, no quiero instalar nada más que Docker en el nodo Jenkins. Quiero ejecutar el proceso completo en el contenedor Docker para lograr esto. Lo que estoy luchando es cómo construir el cuarto paso desde dentro del contenedor.

Escribí el archivo Jenkins como se muestra a continuación

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?
        }
    }
}

Pero no estoy seguro de cómo crear una imagen acoplable dentro del contenedor. La búsqueda no ayudó mucho. Intenté usar el nodo Jenkins para la creación de imágenes de la ventana acoplable, pero parece que no puedo mezclar ni combinar. Entiendo totalmente que esta es una pregunta bastante abierta, pero creo que sería útil conocer las respuestas sencillas.

Respuesta1

Probaría algo como:

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}
                '''
            }
        }
    }
}

La reuseNodeopción debería permitirle ejecutar la compilación de maven en el contenedor aunque la compilación de checkout y Docker se ejecuten en el propio nodo. Ver documentaciónaquí.

información relacionada