Search code examples
shelljenkinsgroovyjenkins-pipeline

How to get the variable value outside sh part in Jenkins?


I was trying to store the output of for loop in in varble and use it outside sh part.

I have declared the lambda_deployers outside pipeline as

def lambda_deployers=""

        
        stage('Fetch lambda paths') {
            steps {
                
                sh '''
                        cd $WORKDIR/$BASE_DIR/
                        for k in $(find . -type f -name 'package.json')
                        do
                            FILE_PATH=$(echo "${k}" | cut -c 2-)
                            DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                            lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                        done
                     echo $lambda_deployers
                        
                     ''' 
                     sh """echo $lambda_deployers"""
                 
                
                
            }
        }

The first echo inside sh ''' ''' works but one outside sh ''' ''' doesn't

Also tried with declaring it as env variable and adding sh ''' under script

          environment {
       
        LAMBDA_BASE_DIR="lambda"
        lambda_deployers=""
     
     
    }
stage('Fetch lambda paths') {
            steps {
                script{
                
                sh '''
                        cd $WORKDIR/$BASE_DIR/
                        for k in $(find . -type f -name 'package.json')
                        do
                            FILE_PATH=$(echo "${k}" | cut -c 2-)
                            DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                            lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                        done
                        echo $lambda_deployers
                  
                     ''' 
                       echo "${env.lambda_deployers}"
                 
                }

The echo outside sh ''' displays as null


Solution

  • You can't assign values to Pipeline variables from within Shell blocks. Few different ways to achieve what you need, write the output to a file and then read from that file when you want to use the variable. Or return the standard out of the sh block and assign it to the variable. Something like below.

    lambda_deployers = sh (
        script: '''
              cd $WORKDIR/$BASE_DIR/
              for k in $(find . -type f -name 'package.json')
              do
                  FILE_PATH=$(echo "${k}" | cut -c 2-)
                  DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                  lambda_deployers+="/$BASE_DIR/$DIR_PATH"
              done
              echo $lambda_deployers
            ''',
        returnStdout: true
    ).trim()
    

    Update Full Pipeline

    pipeline {
      agent any
      stages {
        stage('Wrapper') {
          steps {
            script {
                lambda_deployers = sh (
                        script: '''
                              cd $WORKDIR/$BASE_DIR/
                              for k in $(find . -type f -name 'package.json')
                              do
                                  FILE_PATH=$(echo "${k}" | cut -c 2-)
                                  DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                                  lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                              done
                              echo "this is test content"
                            ''',
                        returnStdout: true
                    ).trim()
                
                echo "$lambda_deployers"
            }
          }
        }
      }
    }