Using a declarative jenkinsfile to build a jenkins pipeline, I would like to capture the output of a shell command script to set as an environment variable using the environment directive
Take the below pipeline -
pipeline {
environment {
COMMIT_INFO_BRANCH = "${sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD')}"
}
stages {
stage("Env Variables") {
steps {
echo "Branch = ${env.COMMIT_INFO_BRANCH}"
}
}
}
}
Unfortunately, I see the below error -
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: dockerNode, node
Pipeline step methods are not feasible (edited: see comments below) in the base environment
or parameters
directives. However, Groovy code is feasible. That being said, you can accomplish this outside the environment
directive and within your pipeline:
stages {
stage('Env Variables') {
steps {
// append to env object
env.COMMIT_INFO_BRANCH = sh(label: 'Determine commit info branch', returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
// use your step to demonstrate the value is in the pipeline env object as a Groovy variable
print "Branch = ${env.COMMIT_INFO_BRANCH}"
// demonstrate it is also indeed an environment variable
sh(label: 'Output env var in shell interpreter', script: 'echo $COMMIT_INFO_BRANCH')
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.