ema
ema

Reputation: 21

Defining agent labels in a single separated file

I am currently facing a problem, I have about 90 jenkinsfile, we recently updated one of the Jenkins agent and it has a new label now, which means that we have to go and update every jenkinsfile with the new label of that agent, you agree that this is a bit of a pain, especially since we will have to do it every time we update the agent.I was thinking if we can define all of the agents is a single file (variable=value) than we reference the variable in our jenkinsfile, so next time we upgrade the agent we do the changes in that particular file instead of 90 jenkinsfile

Upvotes: 0

Views: 137

Answers (1)

ycr
ycr

Reputation: 14574

Yes, you can do this. I'm assuming you have the agent details in the same SCM repo you have the Pipelines. In this case, you can do something like the below.

pipeline {
    agent {label getAgentFromFile()}
    stages {
        stage('Hello6') {
            steps {
                script {
                  echo "Hello Something"                 
                }
            }
        }
   }
}

def getAgentFromFile(){
    def agent = "default"
    node {
        agent = new File( pwd() + '/agent.txt').text.trim()
        println agent
    } 
    return agent
}

Upvotes: 1

Related Questions