Jenkins Pipeline: 도커 컨테이너 내에 도커 빌드

Jenkins Pipeline: 도커 컨테이너 내에 도커 빌드

나는 다음을 수행하려고합니다

  1. 코드 확인
  2. 다른 도커 이미지를 사용하여 사전 확인을 수행합니다(Jenkins 노드에 설치하고 싶지 않음).
  3. Docker 이미지를 사용하여 jar 빌드maven:3.6-jdk-8
  4. 그런 다음 실행 Dockerfile하여 앱 이미지를 빌드합니다.
  5. 이미지를 저장소에 푸시

이제 Jenkins 노드에 Docker 외에는 아무것도 설치하고 싶지 않습니다. 이를 달성하기 위해 Docker 컨테이너에서 전체 파이프라인을 실행하고 싶습니다. 제가 고민하고 있는 것은 컨테이너 내에서 4번째 단계를 구축하는 방법입니다.

Jenkinsfile을 아래와 같이 작성했습니다.

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

하지만 컨테이너 내에 도커 이미지를 만드는 방법을 잘 모르겠습니다. 검색은 그다지 도움이 되지 않았습니다. 도커 이미지 구축을 위해 Jenkins 노드를 사용해 보았지만 혼합하여 일치시킬 수 없는 것 같습니다. 나는 이것이 상당히 개방적인 질문이라는 것을 충분히 이해하지만, 간단한 대답을 아는 것이 도움이 될 것이라고 생각합니다.

답변1

나는 다음과 같은 것을 시도할 것이다:

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

reuseNode옵션을 사용하면 체크아웃 및 Docker 빌드가 노드 자체에서 실행되더라도 컨테이너에서 maven 빌드를 실행할 수 있습니다. 문서 보기여기.

관련 정보