pace-neutrons / Herbert

Herbert is the collection of utilities for visualisation and analysis of neutron spectroscopy data. It can be used alone or as the package, which provide low level data manipulation routines for the Horace package
GNU General Public License v3.0
1 stars 0 forks source link

Add Status flags on master/branch CI builds #51

Open nickbattam-tessella opened 4 years ago

nickbattam-tessella commented 4 years ago

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 of git rev-parse HEAD

nickbattam-tessella commented 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)