How to restrict the jenkins pipeline docker agent in specific slave?

旧街凉风 提交于 2019-12-18 19:44:19

问题


I have several jenkins slaves configured and only label with dockerserver has docker env, then how can I restrict the jenkins pipeline docker agent in this slave?

Below Jenkinsfile doesn't work, the agent inside stage will overwrite the defined slave dockerserver

pipeline {
    agent { label 'dockerserver' }
    stages {
        stage('Back-end') {
            agent {
                docker { image 'maven:3-alpine' }
            }
            steps {
                sh 'mvn --version'
            }
        }
        stage('Front-end') {
            agent {
                docker { image 'node:7-alpine' }
            }
            steps {
                sh 'node --version'
            }
        }
    }
}

It may pick other slave which doesn't have docker supported

Any suggestion?


回答1:


Just had the same problem, seems to work for me like this:

pipeline {
    agent { label 'dockerserver' } // if you don't have other steps, 'any' agent works
    stages {
        stage('Back-end') {
            agent {
                docker {
                  label 'dockerserver'  // both label and image
                  image 'maven:3-alpine'
                }
            }
            steps {
                sh 'mvn --version'
            }
        }
        stage('Front-end') {
            agent {
              docker {
                label 'dockerserver'  // both label and image
                image 'node:7-alpine' 
              }
            }
            steps {
                sh 'node --version'
            }
        }
    }
}



回答2:


After read the guideline more, noticed it was stated https://jenkins.io/doc/book/pipeline/docker/#specifying-a-docker-label.

It shall be configured in the jenkins global(system) configuration



来源:https://stackoverflow.com/questions/49981735/how-to-restrict-the-jenkins-pipeline-docker-agent-in-specific-slave

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