1

I am trying to run a test docker image in kubernetes which will test my application. The application container and test container have the same version which is incremented if any of tests or application changes. How can I define pod yaml dynamically for kubernetes plugin so that I can get the version in the first stage(which is outside the kubernetes cluster) and then update pod yaml with the right version of the container?

APP_VERSION = ""

pod_yaml = """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: ci--my-app
spec:
  containers:
  - name: test-runner
    image: my.docker.registry/app-tester:${-> APP_VERSION}
    imagePullPolicy: Always
    command:
    - cat
    tty: true
"""
pipeline {
  agent none
  stages {
    stage('Build and Upload') {
        agent { node { label 'builder' } }
        steps {

            script {
                APP_VERSION = sh(
                  script: "cat VERSION",
                  returnStdout: true
                  ).trim()
            }
        }
    }
    stage('Deploy and Test application') {
      agent {
        kubernetes {
          label 'ci--data-visualizer-kb'
          defaultContainer 'jnlp'
          yaml pod_yml
        }
      }
      steps {
        container('test-runner') {
          sh "echo ${APP_VERSION}"
          sh "ls -R /workspace"
        }
      }
    }        
  }
}

The kubernetes block in pipeline do not accept lazy evaluation of string pod_yaml which contains ${-> APP_VERSION}. Is there any workaround for this or I am doing it totally wrong?

PS: I cannot use the scripted pipeline for other reasons. So, I have to stick to the declarative pipeline.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kanwar Saad
  • 2,138
  • 2
  • 21
  • 27
  • Looks similar to https://stackoverflow.com/questions/46630168/in-a-declarative-jenkins-pipeline-can-i-set-the-agent-label-dynamically – mkobit Jun 28 '18 at 15:40

1 Answers1

0

It might be a bit odd, but if you're out of other options, you can use jinja2 template engine and python to dynamically generate the file you want. Check it out - it's quite robust.

  • good point. but the pipeline script is picked up directly from git repo by jenkins and there is not much room to wrap it by some template engine. – Kanwar Saad Jun 28 '18 at 08:26