bashjenkinsgroovy

How to get variable from a shell bash-script section sh into a groovy-file for jenkins-pipeline?


TLDR: How do I get a variable out of a sh section, into subsequent groovy stage to be used in a method? (As seen in below code which doesn't work unless ${ENV1_TOKEN}, ${ENV1_PERMIT} are replaced with hard-coded values) Let me know if my solutions are plausible or if I need something else entirely. Link me any similar questions or articles with semi-comparable format to this bash-script.

def serviceHelper
{
    node 
    {
        docker.image(env.DOCKER_REGISTRY + "/drainage/si-base-se:latest").inside('--privileged') 
        {
            withEnv(['HOME=.']) 
            {
                stage('Build Test') 
                {
                    withCredentials([
                            string(credentialsId: 'NPM_PERMIT', variable: 'NPM_PERMIT'),
                            string(credentialsId: 'AUTHORIZATION', variable: 'AUTHORIZATION')
                    ]) 
                    {
                        sh '''
                            rm -rf node_modules                                          
                            npm config set strict-ssl false
                            npm config set registry ${NPM_REGISTRY}
                            npm config set _auth ${NPM_PERMIT}
                            npm config set always-auth true
                            npx npm@6.xx.yy install
                            
                            ENV1_PERMIT=$(echo <code to get string from AUTHORIZATION> )
                            export ENV1_PERMIT
                            ENV1_TOKEN=$(echo <code to get string from AUTHORIZATION> )
                            export ENV1_TOKEN
                            
                            npm run build
                        '''
                    }
                }
    
                stage('Deploy-ENV1') 
                {
                    withCredentials([
                            cfCredentials
                    ]) 
                    {
                        serviceHelper.admitterDeploy(<redacted arguments>,${ENV1_TOKEN}, ${ENV1_PERMIT})
                    }
                }
            }
        }
    }
}

Working with a legacy UI-program which is deployed by a groovy file somehow run by Jenkins. (pipeline config not clear) This groovy file runs the program in a Jenkins pipeline, and I can check its console output there. This file has a method serviceHelper which has a node containing a method: admitterDeploy() The Node makes a docker.image which creates an environment which has several stages that happen in the Jenkins pipeline. The first stage Build Test checks Jenkins cloud foundry for some credentials/environment variables which are needed to continue the pipeline. Need to get credentials permit and tag for 3 different environments: ENV1, ENV2, and ENV3. So 6 different strings. Legacy program does this by running a shell bash-script to compare substrings on Jenkins (e.g. look for ENV1), and retrieve the string. It uses the bash-script in the code below, credentials are successfully displayed on the console output.

After the strings are located, stage Deploy-ENV1 uses the env-specific permit and tag values to run admitterDeploy(). permit and tag are 2 arguments of admitterDeploy. However, I can't figure out how to retrieve values from the bash-script shell. I can only make admitterDeploy work by hard-coding in the values for ${ENV1_TOKEN}, ${ENV1_PERMIT}).

Possible solutions (none of them work yet, maybe due to syntax error):

  1. Create some global variables outside ServiceHelper and assign their value to a shell command, e.g.
def ENV1_PERMIT2='';
def ENV1_TAG2='';
ENV1_PERMIT2=sh'''
echo($AUTHORIZATION <code to get string from AUTHORIZATION>)
'''
  1. Check jenkins.io Bash-script: returnstdout: returns standard output from the task as a string rather than being printed to the console output (build log). Then write code:
ENV1_PERMIT2=sh'''
(returnstdout:true,'$AUTHORIZATION <code to get string from AUTHORIZATION>')
'''
  1. The bash-script command export doesn't seem to be doing anything in the console output. Maybe this needs to be used somehow.

Solution

  • the easiest way:

    just make sure your script is not printing anything else

    def shOut = sh returnStdout:true, script:'''
      ENV1_PERMIT=...
      ENV1_TOKEN=...
      
      echo ENV1_PERMIT
      echo ENV1_TOKEN
    '''
    
    shOut=shOut.readLines()
    def env_permit = shOut[0]
    def env_token  = shOut[1]
    
    serviceHelper.admitterDeploy(..., env_token, env_permit)