Dynamic container based on Matrix axis

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      In declarative pipelines, using Matrix axis to run parallel stages, doesn't play well with the `container` DSL. 
      In the example below, the `container(${CONTAINER})` is not being resolved.

      pipeline {
      agent none
          stages {
              stage('do') {
                  matrix {
                      axes {
                          axis {
                              name 'CONTAINER'
                              values 'alpha', 'beta'
                          }
                      }
                      stages {
                          stage('Init') {
                              agent {
                                  kubernetes {
                                      yaml '''
                                      apiVersion: v1
                                      kind: Pod
                                      spec:                                   containers:                                   - name:  alpha
                                          image: alpha:1.0
                                          tty: true
                                        - name: beta
                                          image: beta:2.1
                                          tty: true                   
                                      '''
                                  }
                              }
                              stages {
                                  stage('init') {
                                     // Init steps within JNLP container
                                  }
                                  stage('build') {
                                      steps {
                                          container("${CONTAINER}") {
                                              // Steps within the container, re-using the workspace.
                                              sh '...'
                                          }
                                      }
                                  }
      }}}}}}}
       

      Is there another alternative? 

       

      update:

      It seems that just by wrapping the `container` withing `script` solves this.
      Example:

       

      pipeline {
      agent none
          stages {
              stage('do') {
                  matrix {
                      axes {
                          axis {
                              name 'CONTAINER'
                              values 'alpha', 'beta'
                          }
                      }
                      stages {
                          stage('Init') {
                              agent {
                                  kubernetes {
                                      yaml '''
                                      apiVersion: v1
                                      kind: Pod
                                      spec:                                   containers:                                   - name:  alpha
                                          image: alpha:1.0
                                          tty: true
                                        - name: beta
                                          image: beta:2.1
                                          tty: true                   
                                      '''
                                  }
                              }
                              stages {
                                  stage('init') {
                                     // Init steps within JNLP container
                                  }
                                  stage('build') {
                                      steps {
                                          script {
                                              container("${CONTAINER}") {
                                                  // Steps within the container, re-using the workspace.
                                                  sh '...'
                                              }
                                          }
                                      }
                                  }
      }}}}}}}
       

       

       

       

       

       

       

            Assignee:
            Unassigned
            Reporter:
            Nikolaos
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Resolved:
              Archived: