Kubernetes using external pod description yaml

Article ID:360033535231
2 minute readKnowledge base

Issue

  • I would like to use an external pod description yaml file for my build agent, either coming from:

  • The workspace

  • A Shared Library

Resolution

Getting the pod template from the workspace

Kubernetes plugin >= 1.17

  • Mention the path to the yaml file using the yamlFile parameter

pipeline {
  agent {
    kubernetes {
      defaultContainer 'maven'
      yamlFile 'KubernetesPod.yaml'
    }
  }
  stages {
    stage('Run maven') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}

Kubernetes plugin < 1.17

  • Mention the path to the yaml file using the yamlFile parameter

  • Provide the build agent name prefix using the label parameter

Not providing the label parameter will cause the system to use the default pod template, ignoring the yamlFile parameter.

pipeline {
  agent {
    kubernetes {
      defaultContainer 'maven'
      label 'build-agent-name-prefix'
      yamlFile 'KubernetesPod.yaml'
    }
  }
  stages {
    stage('Run maven') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}

Getting the pod template from a shared library

Kubernetes plugin >= 1.17

  • Mention the path to the yaml file using the yaml libraryResource parameter

pipeline {
  agent {
    kubernetes {
      defaultContainer 'maven'
      yaml libraryResource('podTemplates/maven.yaml')
    }
  }
  stages {
    stage('Run maven') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}

Kubernetes plugin < 1.17

  • Mention the path to the yaml file using the yaml libraryResource parameter

  • Provide the build agent name prefix using the label parameter

Not providing the label parameter will cause the system to use the default pod template, ignoring the yaml libraryResource parameter.

pipeline {
  agent {
    kubernetes {
      defaultContainer 'maven'
      label 'build-agent-name-prefix'
      yaml libraryResource('podTemplates/maven.yaml')
    }
  }
  stages {
    stage('Run maven') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}

Tested product/plugin versions

Kubernetes plugin >= 1.17

  • CloudBees Core on modern cloud platforms 2.176.3.2

  • Kubernetes Plugin 1.17.2

  • Pipeline Plugin 2.5

Kubernetes plugin < 1.17

  • CloudBees Core on modern cloud platforms 2.164.3.2

  • Kubernetes Plugin 1.14.9

  • Pipeline Plugin 2.5