initial revision.
[pipelinelib] / vars / agentsetup.groovy
diff --git a/vars/agentsetup.groovy b/vars/agentsetup.groovy
new file mode 100644 (file)
index 0000000..ffa1d36
--- /dev/null
@@ -0,0 +1,69 @@
+// inspired/copied from https://github.com/liejuntao001/jenkins-k8sagent-lib
+
+import org.wamblee.jenkins.pipelinelib.MyYaml
+
+// containers: comma-separated list of containers to include. The order of the containers i 
+//    important. Each container in the list corresponds to a yaml file in the podtemplates resource
+//    resource directory. 
+// repo: docker repo, default is the repo configured in the CONTAINER_REGISTRY environment variable
+// version: version to use, defaults to BRANCH_NAME 
+// label: label to use for the agent. Defaults to the stage name if the agent is configured within a stage,
+//      otherwise the job name is used. 
+//
+// All of the arguments specified in the call to agentsetup are passed without change to the 
+// pod template files and can be accessed in the yaml file as ${name} where name is the 
+// argument name. 
+def call(Map args) { 
+  def defaults = [ 
+    version: env.BRANCH_NAME,
+    repo: env.CONTAINER_REGISTRY,
+    label: env.STAGE_NAME ? env.STAGE_NAME: env.JOB_NAME,
+    idleMinutes: 0,
+    defaultContainer: null // initialized to first container later on
+  ]
+  args.label = env.JOB_NAME
+  if (env.STAGE_NAME) { 
+    args.label = args.label + "-" + env.STAGE_NAME
+  } 
+  args = defaults << args
+    
+  // combine the configured application templates 
+  
+  def containers = args.containers.split(',').toList()
+  // always include the jnlp container to increase resource requirements.
+  // Otherwise, the checkout will be slow because of the limite amount of cpu that is 
+  // reserverd. 
+  if (!args.defaultContainer) { 
+    args.defaultContainer = containers[0]
+  }
+  containers = containers.plus(0, 'jnlp')
+  
+  args.label = args.label.toLowerCase().replaceAll("[^a-zA-Z0-9]", "-").replaceAll("-+", "-")
+  println("agentsetup: containers to include: " + containers)
+  println("agentsetup: label '${args.label}'")
+  
+  
+  // all args are available to the templates 
+  Map template_vars = args
+  
+  def templates = []
+  for (container in containers ) { 
+    template = libraryResource 'podtemplates/' + container + '.yaml'
+    template = renderTemplate(template, template_vars)
+    templates.add(template)
+  }
+
+  def myyaml = new MyYaml()
+  def final_template = myyaml.merge(templates)
+
+  ret = [:]
+  ret.idleMinutes = args.idleMinutes
+  ret.label = args.label  
+  ret.yaml = final_template
+  ret.defaultContainer = args.defaultContainer
+  
+  println('agentsetup: parameters returned' + ret); 
+  
+  ret 
+                      
+}