Search code examples
jenkinsjenkins-pipeline

Jenkins parallel stage not able to resolve variable


Im trying to add a parallel stage, running a angular build command. Trying to change "Build apps" stage to make it parallel, but ${configuration_template} doesn't get set with the value. However it works without the parallel stage.

here's a snippet on what was before:

try{
  withFolderProperties{
    timeout(time: TIMEOUT, unit: "MINUTES") {
      node("fnode"){
        def new_workspace = "${env.WORKSPACE}/${BRANCH}"
        def configuration_template = "${configurationPrefix}-vm"


        if (SKIP_LINTING_TESTS == 'false') {
          stage('Parallel Lint') {
            dir("$new_workspace"){
              parallel(
                "Lint @fa/core-lib" : {
                  sh 'ng lint @fa/core-lib'
                },
                "Lint @fa/domain-lib" : {
                  sh 'ng lint @fa/domain-lib'
                },
                "Lint @fa/ui-lib" : {
                  sh 'ng lint @fa/ui-lib'
                },
              )
            }
          }
        }

        stage('Build apps') {
          dir("$new_workspace"){
            sh """
              node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en
              node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-fr --output-path=dist-fr
              mv dist-en/* dist-fr/* dist/fa-presentation
            """
          }
        }

here's where Im at:

stage('Build apps') {
          dir("$new_workspace"){
            timeout(45) {
              try {
                parallel(
                  "Build -en" : {
                    sh 'node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en'
                  },
                  "Build -fr" : {
                    sh 'node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-fr --output-path=dist-fr'
                  }
                )
              } finally {
                 sh 'mv dist-en/* dist-fr/* dist/fa-presentation'
               }
            }
          }
         } 

it gets executed as below; node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=-en --output-path=dist-en


Solution

  • Your code structure is fine, your issue is with the string interpolation of your parameter.
    In groovy string interpulation, the interpolation works only for Double-quoted strings, while Single-quoted strings are plain java.lang.String and don’t support interpolation.

    Therefore the following block:

    sh """
         node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en
         node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-fr --output-path=dist-fr
         mv dist-en/* dist-fr/* dist/fa-presentation
    """
    

    will work as the configuration_template parameter is interpolated correctly due to the use of multi line Double-quoted string.

    But in the parallel stage you are using Single-quoted strings, which means there is no interpolation being done by groovy, the string is passed to the shell "as is" and the shell is trying to evaluate it as an Environment variable - which of course doesn't exist and hence the empty value.

    To solve the issue just change the code to use Double-quoted strings:

    parallel(
       "Build -en" : {
           sh "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en"
        },
        "Build -fr" : {
           sh "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-fr --output-path=dist-fr"
         }
    )