English 中文(简体)
Reuse agent without needing to declare it multiple times
原标题:

I m trying to define a agent in a jenkinsfile once, but use it in multiple stages without needing to fully declare it for each stage. As can be seen, I am using a stage to figure out which docker image to use and then setting a global to use in the agent statement, which works fine and if I put the agent statement in each stage it works as desired. This however, seems ineffcient and not very maintainable if I need to modify the agent s args statement or similar. I want to do something similar to below:

def imageURI = ""
def myagent = docker {
    image "$imageURI"
    args "--user 1000:1000 --name ${env.BUILD_NUMBER}_${env.jobName}"
}
pipeline {
    agent none
    stages {
        stage ("Once use agent") {
            agent {
                docker {
                    image "python:3.5-apline"
                }
            }
            steps {
                script {
                    imageURI = sh "command to get image URI"
            }
        }
        stage ("Build" ) {
            agent = myagent
            steps {
                sh "build code"
            }
        stage ( "Run Unit Test" ) {
            parallel {
                stage ( "UT 1" ) {
                    agent = myagent
                    steps {
                        sh "run unit test 1"
                    }
                }
                stage ( "UT 2" ) {
                    agent = myagent
                    steps {
                        sh "run unit test 2"
                    }
                }
            }
        }
    }
}               

Using suggestion from comment, I m getting farther. But now the agent s image string is getting evaluated before it s set. With below, I would need some way to set the image variable or a way to defer the variable s evaluation

def imageURI = ""
 
pipeline {
    agent {
        docker {
            image "$imageURI"
            args "--user 1000:1000 --name ${env.BUILD_NUMBER}_${env.jobName}"
        }
    }
    stages {
        stage ("Once use agent") {
            agent {
                docker {
                    image "python:3.5-apline"
                }
            }
            steps {
                script {
                    imageURI = sh "command to get image URI"
            }
        }
        stage ("Build" ) {
            steps {
                sh "build code"
            }
        stage ( "Run Unit Test" ) {
            parallel {
                stage ( "UT 1" ) {
                    steps {
                        sh "run unit test 1"
                    }
                }
                stage ( "UT 2" ) {
                    steps {
                        sh "run unit test 2"
                    }
                }
            }
        }
    }
}               
最佳回答

As you correctly note, the variable evaluates when the pipeline begins its execution. To have it ready before your pipeline executes, you can run a combination of scripted and declarative pipeline. The way to do it is outlined in this answer which includes a working example and the output.

In your case, something like this will work:

def imageURI = null
echo "imageURI should be Null: $imageURI"


node() {
    stage("Define imageURI") {
        imageURI = sh "command to get image URI"
        echo "imageURI is now $imageURI"
    }
}

pipeline {
    agent {
        docker {
            image "$imageURI"
            args "--user 1000:1000 --name ${env.BUILD_NUMBER}_${env.jobName}"
        }
    }

问题回答

In my case, many properties needed to be set for the docker agent, but the values for all of the properties were known before the pipeline started. And the docker agent needed to be used in multiple but not all stages. To avoid having to duplicate all the properties of the docker agent in multiple places, I defined the properties once in a buildAgentConfig() method that returns a map. Then within each stage, I defined agent blocks that used docker agents as docker(buildAgentConfig()). A simplified version of the final pipeline looked something like below:


pipeline {
    agent any

    stages {
        stage( Init ) {
            steps {
                sh    #!/bin/bash
                    some command
                   
            }
        }

        stage( test ) {
            agent {
                docker(buildAgentConfig())
            }
            steps {
                sh    #!/bin/bash
                   ./gradlew test
                   
            }
        }

        stage( build ) {
            when {
                anyOf {
                    branch  master 
                    branch  main 
                }
            }
            agent {
                docker(buildAgentConfig())
            }
            steps {
                sh    #!/bin/bash
                   ./gradlew build -x test
                   
            }
        }
    }
}

def buildAgentConfig() {
    [
        label:  somelabel ,
        image:  myprivateregistry.com/openjdk:11.0-jdk ,
        registryUrl:  myprivateregistry.com ,
        registryCredentialsId:  some-credentials-id ,
        args:  -v /var/lib/jenkins/.gradle/:/home/jenkins/.gradle/  +
                --env GRADLE_USER_HOME=/home/jenkins/.gradle/ 
        reuseNode: true,
    ]
}




相关问题
Reuse agent without needing to declare it multiple times

I m trying to define a agent in a jenkinsfile once, but use it in multiple stages without needing to fully declare it for each stage. As can be seen, I am using a stage to figure out which docker ...

在Jenkins Templating引擎实施版本

我们正计划利用JTE(Jenkins Templating的发动机)在我们org建造管道。 有许多管道将使用这一模板。 可能的话,模板的任何改动都会中断......

Hudson gitorious plugin?

Has anyone integrated a local copy of gitorious with Hudson? Specifically, linking the sha1 on Hudson web page back to the gitorious web page.

Problem with Hudson-JIRA-Perforce integration

I m looking at using the Hudson JIRA plugin to post a JIRA comment on each build. The SCM I m using is perforce which is already linked to the JIRA tasks using perforce jobs. I can see that the JIRA ...

Two Hudson Masters on the same Windows Server

I want to setup two Hudson Masters on the same Hardware. This will make administering Hudson easier, since both servers are used by two different (and independent) teams. So far I tried to install ...

Using Hudson and build steps with multiple git repositories

I m trying out Hudson to replace our current Buildbot setup. I installed the git plugin. Our current setup is like: ssh://server:/repo/test_framework.git ssh://server:/repo/project_a.git Now, to ...

热门标签