Assia Khateeb
Assia Khateeb

Reputation: 33

How to Evaluate Environment Variables from a POD_YAML File in Jenkins Pipeline using Kubernetes Agent

In the Jenkinsfile, I'm using a Kubernetes agent along with the yamlFile directive to define pods.
Below is the Jenkinsfile configuration:

pipeline {
  agent {
    kubernetes {
      yamlFile 'pod.yaml'
      defaultContainer "my-container"
    }
  }

  stages {
    stage("echo") {
      steps {
        sh """
        echo "123"
        """
      }
    }
  }
}

The yamlFile directive is referencing a pod.yaml file which contains the pod configuration.
Below is the pod.yaml configuration:

pod.yaml:
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: my-container
    image: ${REPO_NAME}:${VERSION_TAG}
      env:
        - name: MY_JOB_NAME
          value: ${JOB_NAME}
    ports:
    - containerPort: 80

Note: REPO_NAME and VERSION_TAG are sent by Jenkins parameters.

After running Jenkins, I encounter the following error:

[Warning][jenkins/AAA][InspectFailed] Failed to apply default image tag "${REPO_NAME}:${VERSION_TAG}": couldn't parse image reference "${REPO_NAME}:${AVERSION_TAG}": invalid reference format: repository name must be lowercase

After the Jenkins pipeline runs, the variables defined in pod.yaml aren't evaluated properly. Is there a way to populate these variables while maintaining code DRY?

I'm aware that we can define the YAML content in a Groovy string variable like so:

    def pod_yaml = """
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    ....
    """

Then use this variable with the yaml pod_yaml directive in the Jenkinsfile. However, this approach doesn't keep the code DRY since we have to repeat it in each required Jenkinsfile.

Thanks

Upvotes: 0

Views: 82

Answers (0)

Related Questions