Home > other >  How to extract command output from the multi lines shell in Jenkins
How to extract command output from the multi lines shell in Jenkins

Time:01-25

stage('Deployment'){
                            script {                                   
                                sh """
                                export KUBECONFIG=/tmp/kubeconfig
                                kubectl describe deployment nginx | grep Image"""

                            }
                    }

How to get the output of "kubectl describe deployment nginx | grep Image" in an environment variable

CodePudding user response:

The current pipeline version supports returnStdout, which enables you to get the output from sh/bat commands, like so:

script {
    ENV_VARIABLE = sh(script: 'kubectl describe deployment nginx | grep Image', returnStdout: true)
    echo ENV_VARIABLE
}

See also:

CodePudding user response:

you can use the sytax:

someVariable = sh(returnStdout: true, script: some_script).trim()

CodePudding user response:

In this situation, you can access the environment variables in the pipeline scope within the env object, and assign values to its members to initialize new environment variables. You can also utilize the optional returnStdout parameter to the sh step method to return the stdout of the method, and therefore assign it to a Groovy variable (because it is within the script block in the pipeline).

script {             
  env.IMAGE = sh(script: 'export KUBECONFIG=/tmp/kubeconfig && kubectl describe deployment nginx | grep Image', returnStdout: true).trim()
}

Note you would also want to place the KUBECONFIG environment variable within the environment directive at the pipeline scope instead (unless the kubeconfig will be different in different scopes):

pipeline {
  environment { KUBECONFIG = '/tmp/kubeconfig' }
}
  • Related