Eva
Eva

Reputation: 593

Access a Groovy variable within shell step in Jenkins pipeline

SSHUER in stage one prints as required. But in stage two it doesn't print and gives me a null value. All three attempt statements in stage two are not working. I can't use script block as I have some special characters in the actual code. Any suggestions as to how to fetch the value of SSHUSER in stage two.

def SSHUSER

environment {
if(params.CLOUD == 'google') {
   SSHUSER = "test1"
} else if (params.CLOUD == 'azure') {
   SSHUSER = "test2"
}
}

pipeline {
    stage('stage one') {
      steps {
        echo "first attempt '${SSHUSER}'"
      }
    }

    stage('stage two') {
      steps {
      sh '''#!/bin/bash
          echo "first attempt '${SSHUSER}'"
          echo "second attempt \${SSHUSER}"
          echo "thrid attempt \$SSHUSER"

          if ssh  \${SSHUSER}@$i 'test -e /tmp/test'";
          then
              echo "$i file exists "
          fi
         '''
      }
    }
  }

Upvotes: 1

Views: 1545

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

your usage of the environment block is a bit weird, it should reside inside the pipeline block and variables should be initialized inside the environment block.
If it is defined outside the variables will not be passed to the shell script as environment variables.

If you want to use the environment block inside the `pipeline' block just define the parameters (as strings) you want and they will be available for all stages, and they will be passed as environment variables for the shell.
For example:

pipeline {
    agent any
    parameters{
        string(name: 'CLOUD', defaultValue: 'google', description: '')
    }
    environment {
        SSHUSER = "${params.CLOUD == 'google' ? 'test1' : 'test2'}"
    }
    stages {
        stage('stage one') {
            steps {
                echo "first attempt '${SSHUSER}'"
            }
        }

        stage('stage two') {
            steps {
                sh '''#!/bin/bash
          echo "first attempt '${SSHUSER}'"
          echo "second attempt \${SSHUSER}"
          echo "thrid attempt \$SSHUSER"

          if ssh  \${SSHUSER}@$i 'test -e /tmp/test'";
          then
              echo "$i file exists "
          fi
         '''
            }
        }
    }
}

If you don't want to use the environment block or mange the parameters by yourself you can do it outside the pipeline block using Global variables but then they will not be passed environment variables and you will need to use string interpolation (""" instead of ''') to calculate the values when passing the command to the shell:

SSHUSER = ''

if(params.CLOUD == 'google') {
    SSHUSER = "test1"
} else if (params.CLOUD == 'azure') {
    SSHUSER = "test2"
}

pipeline {
    agent any
    parameters{
        string(name: 'CLOUD', defaultValue: 'google', description: 'Bitbucket Payload', trim: true)
    }
    stages {
        stage('stage one') {
            steps {
                echo "first attempt '${SSHUSER}'"
            }
        }

        stage('stage two') {
            steps {
                sh """#!/bin/bash
          echo "first attempt '${SSHUSER}'"
          echo "second attempt ${SSHUSER}"
          echo "thrid attempt $SSHUSER"

          if ssh  ${SSHUSER}@\$i 'test -e /tmp/test'";
          then
              echo "\$i file exists "
          fi
         """
            }
        }
    }
}

Upvotes: 1

Related Questions