Declarative pipeline - override pipeline level agent

256 views
Skip to first unread message

nirav

unread,
Sep 27, 2018, 4:01:25 PM9/27/18
to Jenkins Users
I am having trouble defining agents for my build pipeline. I need to use 2 docker images as agents for entire pipe. first 4 steps uses one image and last two steps uses another one. I don't want to repeat agent statement in every stage.

It was easy with scripted pipe. I defined all my images on top level and then for each stage I just need to say container("${name of the image}")
     containers: [
        containerTemplate(name: 'maven', image: 'maven:3.5.4-jdk-10', ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'docker', image: 'docker:18.03.1-ce', ttyEnabled: true, command: 'cat'),
    ]


I see that declarative pipeline you can not define multiple agents at pipeline level. 

Is there a way I can define agent in one stage and in all other stages i refer them by label/name etc? 

ps - I don't want to do any setting under 'Manage Jenkins'.

Thanks

nirav

unread,
Sep 27, 2018, 4:29:41 PM9/27/18
to Jenkins Users
SO far I tried following but with all I keep hitting ‘Jenkins’ doesn’t have label ‘docker’

Try 1 : Trying to override top-level agent. I get  ‘Jenkins’ doesn’t have label ‘docker’ and build hangs there

pipeline {

    agent { 

      docker {

            image 'maven:3-alpine' 

            args '-v /Users/npatel/.m2:/root/.m2' 

        }

    }

    stages {

        stage("Initialize") {

             agent { 

      docker {

            image 'maven:3.5.4-jdk-10' 

            args '-v /Users/npatel/.m2:/root/.m2' 

        }

    }

             steps{

                echo "buildId: ${env.BUILD_ID}"

             }

        }

    }


}


Try 2 : Trying to override any agent. I still get  ‘Jenkins’ doesn’t have label ‘docker’ and build hangs there

pipeline {

    agent any

    stages {

        stage("Initialize") {

             agent { 

      docker {

            image 'maven:3.5.4-jdk-10' 

            args '-v /Users/npatel/.m2:/root/.m2' 

        }

    }

             steps{

                echo "buildId: ${env.BUILD_ID}"

             }

        }

    }


}

Andrew Bayer

unread,
Sep 27, 2018, 4:36:13 PM9/27/18
to jenkins...@googlegroups.com
Top level docker agents can't be overridden - but you can use the kubernetes plugin in Declarative (https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarative.groovy e.g.) which may be what you're looking for.

A.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5338401e-eb41-4c6a-818d-b3300dac3d14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nirav

unread,
Sep 27, 2018, 6:29:33 PM9/27/18
to Jenkins Users
would that be useful without kubernetes cluster? And isn't this a common requirements? Without it my pipe looks like following. It's littered with same agent section in many stages. It works though.

#!/bin/groovy

def scmVars
pipeline {
    agent { label 'master'}

    stages {
        stage("Initialize") {
            agent {
                docker {
                    image 'maven:3-alpine' 
                    args '-v /Users/npatel/.m2:/root/.m2' 
                    reuseNode true
                }
            }
             steps{
                sh '''
                    pwd
                    ls -lah
                    printenv
                '''
             }
        }
        stage(“Checkout”) {
            agent {
                docker {
                    image 'maven:3-alpine' 
                    args '-v /Users/npatel/.m2:/root/.m2' 
                    reuseNode true
                }
            }
            steps {
                script {
                    scmVars = git credentialsId: 'bitbucket-npatel', url: 'http://....git'
                    
                 }
                    
            }
         }
        stage('Build') {
         agent {
            docker {
                image 'maven:3-alpine' 
                args '-v /Users/npatel/.m2:/root/.m2' 
                reuseNode true
            }
         }
         steps {
            sh '''
                echo "multiline shell steps"
                #mvn -B -DskipTests install
            '''
         }   
        }
        stage('test') {
            agent {
                docker {
                    image 'maven:3-alpine' 
                    args '-v /Users/npatel/.m2:/root/.m2' 
                    reuseNode true
                }
            }
            steps {
                sh '''
                    echo "skipping tests for now"
                    # mvn surefire:test
                '''
            }
        }
        stage('build dist') {
            agent {
                docker {
                    image 'maven:3-alpine' 
                    args '-v /Users/npatel/.m2:/root/.m2' 
                    reuseNode true
                }
            }
            steps {
                sh '''
                    #mvn play2:dist -Dplay2.distFormats=tar.gz
                '''
            }
        }
        stage('docker build') {
           agent {
              docker {
                image "docker:18.03.1-ce"
                registryUrl ""
                registryCredentialsId "dockerhub-npatel"
              }
            }    
           steps {       
        sh """
        docker -v
Reply all
Reply to author
Forward
0 new messages