Open nickbattam-tessella opened 4 years ago
The candidate function switches on a non-empty PR_STATUSES_URL
variable. This is defined in the Groovy for a PR, but not for a branch build. Attempts to force the execution of the the bash-switch to generate that value where needed failed.
def post_github_status(String state, String message) {
withCredentials([string(credentialsId: 'GitHub_API_Token',
variable: 'api_token')]) {
sh '''
if [[ -z "$PR_STATUSES_URL" ]]; then \
PR_STATUSES_URL="https://api.github.com/repos/pace-neutrons/Herbert/statuses/\$(git rev-parse HEAD)"; \
fi && \
curl -H "Authorization: token ${api_token}" \
--request POST \
--data '{"state": "${state}", \
"description": "${message}", \
"target_url": "$BUILD_URL", \
"context": "$JOB_BASE_NAME"}' \
$PR_STATUSES_URL > /dev/null
'''
}
}
Further efforts to use a Groovy function to grab the revision sha returned an empty string:
return sh(script: 'git rev-parse HEAD', returnStdout: true)
The Jenkinsfile sets status flags during the execution of the PR build pipeline using an API URL included in the PR notification and passed to the the script.
For the
master
or a named branch build this information is not available and a URL must be constructed.The API required is
https://api.github.com/repos/pace-neutrons/Herbert/statuses/<sha>
where<sha>
is the output ofgit rev-parse HEAD