Jenkins Plugins for PR events

You can use Generic Webhook Trigger plugin for triggering Jenkins builds on PR events and get the pr number from the payload it sends to trigger test runs from HyperTest CLI.

To achieve it, you will have to do the following

  1. Send webooks to Jenkins webhooks trigger on pull request events from your codebase

  2. Install Generic Webhook Plugin in Jenkins

  3. Configure Plugin in your pipeline

Steps:

1. Send Webhooks to Jenkins

  1. Go to Webhooks

  2. Add a new Webhook

  3. Payload Url: Wehook trigger listens on /generic-webhook-trigger/invoke.

If you have any authentication on jenkins, pass it in webhook url like below http://<username>:<password>:@<jenkins_ip>:<jenkins_port>/generic-webhook-trigger/invoke

  1. Content type: application/json

  2. Add any secret if required

  3. To trigger the webhooks on pull request events only, select let me select individual events and check pull requests

  4. Save and add Webhook

2. Install Plugin in Jenkins

Install Generic Webhook Plugin from Jenkins plugins page and restart Jenkins after the plugin is installed

3. Configure Plugin in pipeline to pick PR number

  1. Go to configure tab in your pipeline

  2. Check the Generic Webhook plugin option under Build triggers

  1. To fetch the pull request id, add the parameter in post content parameter Variable Name: pull_request_number Expression: $.pull_request.number

  2. Add other envs/token or other settings if required and save

  3. Now you access the parameter by $pull_request_number

  4. Export HT_PR parameter inside your Jenkinsfile export HT_PR=$pull_request_number

Sample files for CI/CD with Generic Trigger

Alternatively you can also control environment variables of Generic Webhook plugin from your Jenkinsfile like below, and add it in HT_PR in environment section

Jenkins Plugins for PR events

2. Install Plugin in Jenkins

Install Generic Webhook Plugin from Jenkins plugins page and restart jenkins after the plugin is installed

3. Configure Plugin in pipeline to pick PR number

  1. 1.Go to configure tab in your pipeline

  2. 2.Check the generic webhook plugin option under Build triggers

  1. 3.To fetch the pull request id, add the parameter in post content parameter Variable Name: pull_request_number Expression: $.pull_request.number

  2. 4.Add other envs/token or other settings if required and save

  3. 5.Now you access the parameter by $pull_request_number

  4. 6.Export HT_PR parameter inside your Jenkinsfile export HT_PR=$pull_request_number

Sample files for CI/CD with Generic Trigger

Alternatively you can also control environment variables of Generic Webhook plugin from your Jenkinsfile like below, and add it in HT_PR in environment section

Jenkinsfile
#!/usr/bin/env groovy

// This is a basic workflow to help you get started with Jenkins Pipeline

// This pipeline should ony be triggered when you raise a PR/make changes to it

pipeline {
    agent any
    
    triggers {
      GenericTrigger(
        genericVariables: [
          [key: 'pull_request_number', value: '$.pull_request.number'],
        ],
        causeString: 'Triggered on $action in $pull_request_number',
        regexpFilterExpression: '',
        regexpFilterText: '',
        printContributedVariables: false,
        printPostContent: false
      )
    }
    
    // Setting a few env for HyperTest Cli
    environment {
       HT_BASE_URL='http://<hypertest-vm-ip>:<dashboard-port>'
       HT_COMMIT_HASH="${sh (returnStdout: true, script: 'echo ${GIT_COMMIT}').trim()}"
       HT_BRANCH="${sh (returnStdout: true, script: 'echo ${GIT_BRANCH}').trim()}"
       HT_PR="${sh (returnStdout: true, script: 'echo ${pull_request_number}').trim()}"
       HT_API_TOKEN='<ht-api-token>'
    }

    stages {
    
        // Steps to build your application
        stage('Build') { 
            steps {
                sh 'echo "steps to build"'
            }
        }
      
        // Steps to deploy your application
        stage('Deploy') {
            steps {
                sh 'echo "steps to deploy"'
            }
        }
        
          // Steps to test your application
        stage('Test') {
            steps {
                sh 'echo "steps to test"'
            }
        }
        
        // Steps to start a new test run
        stage('Start new test run') {
        
            steps { 
                sh 'echo "This job downloads HyperTest Cli and start a new test"'               
                sh 'printenv|sort'
                sh 'wget -O ht-cli.tar.xz https://hypertest-binaries-1.s3.ap-south-1.amazonaws.com/ht-cli/ht-cli-latest.tar.xz'
                sh 'tar xvf ht-cli.tar.xz'
                sh './hypertest can-run-test'
                sh './hypertest start-new-test'
            }
        }
    }

Last updated