Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run all jenkins pipeline steps except one in a docker container

I'm trying to set up a jenkins multibranch pipeline to run all my code validation steps in a docker container, then build the docker image and push it outside of said docker container.

Currently, my Jenkinsfile looks sort of like this (slimmed down for readability):

pipeline {
    agent {
        label 'AWS'
    }
    stages {
        stage('stuff in docker') {
            agent {
                dockerfile {
                    filename 'Dockerfile.jenkins'
                    reuseNode true
                }
            }
            steps {
                stuff
            }
        }
        stage('more stuff in docker') {
            agent {
                dockerfile {
                    filename 'Dockerfile.jenkins'
                    reuseNode true
                }
            }
            steps {
                stuff
            }
        }
        stage('stuff not in docker') {
            steps {
                stuff
            }
        }
        stage('more stuff not in docker') {
            steps {
                stuff
            }
        }
    }
    post {
        always {
            cleanWs()
        }
    }
}

The problem here is that every stage that I use a dockerfile agent, jenkins will attempt to rebuild the docker image. The stages are all cached, but sending build context and actually processing everything still takes more time than I'd like. If I use the dockerfile as the root agent, I get to run everything inside the same docker container, but I lose the ability to do git stuff and build the docker image (at least without connecting to the external docker process sock, which seems like more hassle than should be necessary).

I'd like to know if there's some way I can use the same docker image for multiple steps, but then pull out of that docker image for some other steps.

like image 645
ChickenWing Avatar asked Aug 23 '19 18:08

ChickenWing


2 Answers

Figured it out!

pipeline {
    agent {
        label 'AWS'
    }
    stages {
        stage('do everything in docker') {
            agent {
                dockerfile {
                    filename 'Dockerfile.jenkins'
                    reuseNode true
                }
            }
            stages {
                stage('stuff in docker') {
                    steps {
                        stuff
                    }
                stage('more stuff in docker') {
                    steps {
                         stuff
                    }
                }
            }
        }
        stage('stuff not in docker') {
            steps {
                stuff
            }
        }
        stage('more stuff not in docker') {
            steps {
                stuff
            }
        }
    }
    post {
        always {
            cleanWs()
        }
    }
}

tl;dr you can nest stages

like image 146
ChickenWing Avatar answered Sep 28 '22 08:09

ChickenWing


Using dockerfile as build agent will always result in a container rebuild. If you just want to run some steps inside a (pre-build) container use docker as agent. Of course, this can also be a container build locally inside the same pipeline. You can also utilize bind mounts to share data between containers.

If unsure, check out the documentation 1 and 2. Maybe it's also advisable to set the global agent to none in your case.

like image 44
Joe Avatar answered Sep 28 '22 08:09

Joe