1

I am trying to customize a shared Library which run a stage in a specify agent by adding the possibility to specify the agent.

def call(String agentLabel) {
  pipeline {
    agent { label "$agentLabel" }
    stages {
      stage('Hello') { steps { echo 'Hello World' } }
    }
  }
}

This is OK but now I want to customize the type of agent (IE. either use label or docker agent) ? This don't work :

def call(String agentLabel, String agentDockerImage = null) {
  Closure agentRunner = null
  if (agentDockerImage) {
    agentRunner = {
      docker {
        label "$agentLabel"
        image "$agentDockerImage"
      }
    }
  } else {
    agentRunner = {
      label "$agentLabel"
    }
  }
  pipeline {
    agent { agentRunner.call() }
    stages {
      stage('Hello') { steps { echo 'Hello World' } }
    }
  }
}

WorkflowScript: 15: Invalid agent type "call" specified. Must be one of [any, docker, dockerfile, label, none] @ line 15, column 29.
agent { agentRunner.call() }
^

C3PO
  • 53
  • 4
  • There might be a more elegant way, but if all else fails, you may use `evaluate` to dynamically generate your pipeline script from string: https://stackoverflow.com/a/61108773/7571258 – zett42 May 02 '22 at 20:35

0 Answers0