The following Jenkinsfile snippet will achieve this
// Import JSON Slurper import groovy.json.JsonSlurperClassic // Get the Git payload def payload = new JsonSlurperClassic().parseText(env.payload) // Now define a method to filter the event type private String getEventType ( payload ){ if( payload.pull_request && payload.action.toLowerCase().contains("opened") ){ return "pull_req" } else if( payload.ref && payload.head_commit){ if( payload.ref.split('/')[1].toLowerCase().contains('head') ){ return "push" } else if( payload.ref.split('/')[1].toLowerCase().contains('tag') ){ return "tag" } } } // Now decide what action to take def eventType = getEventType( payload ) switch (eventType) { case "push": ............ do something ............ break; case "pull_req": ............ do something else ............ break; default: println "Git event ignored"; currentBuild.displayName = "Git event ignored" // Tidy up the Jenkins GUI by deleting the ignored build def buildNumber = env.BUILD_ID node { sh "curl -X POST http://<username>:<apikey>@<jenkins host>:443/job/execute-pipeline/${buildNumber}/doDelete" }
No comments:
Post a Comment