Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-53472

Expand environment variables in yaml

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • kubernetes-plugin
    • None
    • jenkins/jenkins:lts
      k8s plugin 1.12.4

      Hi,

      I'm trying to access and environment variable when using a yaml pod definition but I only get null values. This is what I'm using:

            yaml """                                                                                                                                                                                                       
      apiVersion: v1                                                                                                                                                                                                       
      kind: Pod                                                                                                                                                                                                            
      metadata:
        annotations:
          iam.amazonaws.com/role: arn:aws:iam::${TARGET_ACCOUNT_ID}:role/jenkins-admin-mgmt
      spec:
        containers:
        - name: docker
          image: ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/docker:${DOCKER_IMAGE_TAG}
          command: ["sleep"]
          args: ["86400"]
      """
      

      ACCOUNT_ID, REGION and DOCKER_IMAGE_TAG are all set on the jenkins master pod as env vars. TARGET_ACCOUNT_ID is a job parameter and expands just fine.

      Elsewhere, these variables are available, like so:

        agent {    
          kubernetes {             
            label "${JOB_NAME}"                                                                                                            
            inheritFrom 'default'
            containerTemplate {
              name 'jnlp'
              image '${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/jenkins/jnlp-slave:${AGENT_IMAGE_TAG}'
      

      That works, even though it's in single quotes ^^, but in yaml it doesn't.

      I've tried all sorts of combinations of escaping and setting variables with environment blocks, trying to set variables outside the pipeline etc, but I can't make it work. Maybe I'm doing something wrong, or maybe it's broken. Maybe it's not supposed to work?

      Any advice greatly appreciated.

      Thanks,

      Matt

            Unassigned Unassigned
            fascinatedcow Matt Buckland
            Votes:
            5 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: