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

不羁的心 提交于 2020-07-19 04:20:25

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/57631440/how-do-i-run-all-jenkins-pipeline-steps-except-one-in-a-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!