4

Jenkins version 2.235.2

kubernetes-plugin version 1.26.4

I'm trying to parametrize the yamlFile used as pod template with a env variable based on the branch I'm building. What I have right now is:

pipeline {
  environment {
    MASTER_BRANCH = "origin/dev"
    BUILD_POD = "${env.GIT_BRANCH == env.MASTER_BRANCH ? 'jenkins/build-pod-prod.yaml' : 'jenkins/build-pod.yaml' }"
  }
  agent {
    kubernetes {
      idleMinutes 3
      yamlFile env.BUILD_POD
      defaultContainer 'docker'
    }
  }
}

But that is taking a default template with just the jnlp container. I've tried also putting:

yamlFile env.BUILD_POD
yamlFile "${env.BUILD_POD}"
yamlFile "${BUILD_POD}"
yamlFile "$BUILD_POD"
yamlFile $BUILD_POD

But none of that worked. I don't know if it's some misunderstanding from my side or a bug.

I tried also to do the pipeline as a scripted one, which seems like more versatile, but I cannot now neither how to accomplish what I need.

Thanks all in advance.

Ale Sanchez
  • 536
  • 1
  • 6
  • 17

1 Answers1

0

This will not work since your environment variable BUILD_POD is not populated when Jenkins is configuring agent {...} block. This lead to situation when variable is set as null or some empty string and Jenkins is using default jnlp container.

What you need to do is to set this variable earlier but it is possible only on master node.

This should work.