Search code examples
kubernetesjenkinsjenkins-pluginscicdjenkins-kubernetes

String interpolation inside PodTemplate in Jenkinsfile


I am trying to add the value of image_tag and region inside podtemplate but its not working. Error failed to add image information to the policy rule context: invalid image 'aws-cli:${image_version}' (bad image: docker.io/aws-cli:${image_version}, defaultRegistry: docker.io, enableDefaultRegistryMutation: true: invalid reference format)

I am using this plugin https://plugins.jenkins.io/kubernetes/

image_version = '2.15.40'
region = 'us-east-1'
podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        label: label
    spec:
     containers:
     - name: aws-cli:${image_version}
       image: 
       env:
       - name: AWS_REGION
         value: ${region}
       resources:
         requests:
           cpu: "2"
           memory: "3Gi"
         limits:
           cpu: "2"
           memory: "3Gi"
       alwaysPullImage: true
       tty: true
       securityContext:
         privileged: true
''')

Solution

  • You are using triple single quoted string ''', which don't do variable interpolation. You need to use triple double quoted strings """ instead. See groovy docs.