Enabling webhook in pipeline

Webhooks typically are used to connect two different applications. When an event happens on the trigger application, it serializes data about that event and sends it to a webhook URL from the action application—the one you want to do something based on the data from the first application.

Adding Jenkins Webhook in Github

step1: Open the Github repository.

step2: Go to “settings” and then to “hooks”.

step3: Click the “Add webhook” button.

step4: Fill in the form, as shown in the image below. For the payload URL, provide your Jenkins URL and the GitHub webhook path at the end of the URL — https://${jenkins_url}/github-webhook/. You can disable SSL verifications if you don’t have a valid SSL cert for your Jenkins server. Finally, choose the option “Calling the webhook only for push events.” (This actually depends on what you are trying to do. You can change this by selecting other options).



Click on “Add webhook”

Now let’s see how to enable the webhook in the Jenkins pipeline. The below pipeline 

pipeline {
    agent any    

    tools {
        // Install the Maven version configured as "M3" and add it to the path.
        maven "MVN3"
        jdk "JDK1.8"
    }    

    stages {
        stage('enable webhook') {
            steps {
                script {
                    properties([pipelineTriggers([githubPush()])])
                }
            }
        }        

        stage('pullscm') {
            steps {
                git credentialsId: 'github', URL: '[email protected]:sathishbob/jenkins_test.git'
            }
        }
        stage('Build') {
            steps {
                // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true -f api-gateway clean package"                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
            }            post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                    junit 'api-gateway/target/surefire-reports/*.xml'
                    archiveArtifacts 'api-gateway/target/*.jar'
                }
            }
        }
    }
}


we can add a webhook to our job and ensure that every time a developer commits a code to GitHub, our build will be triggered


Recent Comments

No comments

Leave a Comment