Passing a variable to jenkins kubernetes inside yamlFile

7/22/2021

Similar to the question https://stackoverflow.com/questions/66705926/passing-variable-to-jenkins-yaml-podtemplate

But I am using the yamlFile 'pod.yaml' construct and the variables within that file do not seem to get parsed and replaced. Is is possible to use a environemnt variable inside the pod definition within the yamlFile?

Example

pipeline {
  environment {
    SHA = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
  }
  agent {
    kubernetes {
      yamlFile 'pod.yaml' #within this file the image is set to foo/foo:${env.SHA}
    }
  }
}

the issue with the above code is that env.SHA is empty in the yamlFile context. I have confirmed the variable is set and exists in the shell steps by printing it.

-- samuel beaulieu
jenkins
jenkins-pipeline
kubernetes

0 Answers