Reputation: 15
How to get the output of kubectl describe deployment nginx | grep Image
in an environment variable?
My code:
stage('Deployment'){
script {
sh """
export KUBECONFIG=/tmp/kubeconfig
kubectl describe deployment nginx | grep Image"""
}
}
Upvotes: 0
Views: 135
Reputation: 1
You can use the syntax:
someVariable = sh(returnStdout: true, script: some_script).trim()
Upvotes: 0
Reputation: 28884
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' }
}
Upvotes: 1