Example of using Jenkins#

PRO

Goals#

  • Run tests in Jenkins.

  • Receive SLA results automatically.

  • Run a pipeline depending on SLA results.

Solution#

  1. Create and configure a test in PFLB Platform.

  2. Install Jenkins plugins:

  3. Create a pipeline to run the test.

  4. Create a pipeline to get SLA and webhook from PFLB Platform.

  5. Create pipelines to run depending on SLA results.

  6. Run the test or configure it to run on a schedule.

  7. View the results.

Create and configure a test in PFLB Platform#

  1. Create a test.

  2. Configure SLA.

  3. Create a settings set.

  4. In the settings set, go to the Webhooks tab.

  5. Click + Add new webhook.

  6. From the Test status drop-down list, select the Finished and Canceled values.

  7. Click Edit request.

  8. In the URL field, enter JENKINS_URL/generic-webhook-trigger/invoke.

  9. In the Key field, enter token. In the Value field enter a Jenkins token:

    ../_images/um_ci_jenkins_webhook.en.png
  10. Click Save.

Create a pipeline to run the test#

  1. Click New Item in Jenkins.

  2. Enter a name for your project, select Pipeline, and click OK.

  3. From the GitLab Connection drop-down list, select the necessary value.

  4. Select the This project is parameterized checkbox.

  5. Click Add Parameter and select String Parameter.

  6. In the Name field, enter secret_api_keyAPI token:

    ../_images/um_ci_jenkins_string_parameter.en.png
  7. Select the Trim the string checkbox.

  8. Similarly, add the parameters:

    • testProjectId. The ID of your test.

    • testProjectVersionId. The ID of your test’s version.

    Parameter values are specified when building a Jenkins project.

  9. On the Pipeline tab, from the Definition drop-down list, select Pipeline script:

    ../_images/um_ci_jenkins_pipeline.png
  10. In the Script box, specify the code:

    pipeline {
        agent { node "master" }
    
        stages {
            stage("Start test") {
                steps {
                    script {
                        def patchOrg = """
                            {"testProjectId": ${test_project_id}, "testProjectVersionId": ${test_project_version_id}}
                        """
    
                        def response = httpRequest(url: "https://api.platform.io/test-srv/api/test",
                            customHeaders: [[name: 'api-token', value: "${secret_api_key}"]],
                            requestBody: patchOrg,
                            httpMode: 'POST',
                            consoleLogResponseBody: true,
                            contentType: 'APPLICATION_JSON'
                        )
    
                        def responseBody = readJSON text: response.content
                    }
                }
            }
        }
    }
    

    The specified script:

    1. sends the POST request for running a test.

    2. saves the response to the request.

  11. Make sure that the Use Groovy Sandbox checkbox is cleared.

  12. Click Save.

Create a pipeline to get an SLA and a webhook from PFLB Platform#

After the test is completed, PFLB Platform sends a webhook to Jenkins. The pipeline automatically sends a request to get an SLA.

To create a pipeline, follow these steps:

  1. Click New Item in Jenkins.

  2. Enter a name for your project, select Pipeline, and click OK.

  3. From the GitLab Connection drop-down list, select the necessary value.

  4. On the Build Triggers tab, select the Generic Webhook Trigger checkbox.

  5. For Post content parameters pane, click Add:

  6. Fill in the fields:

    • In the Variable field, enter test_id. This is the test ID.

    • Select JSONPath.

    • In the Expression field, enter $.id. The JSONPath expression is used to extract the test ID after the test has run to get the SLA.

  7. In the Token field, specify the Jenkins token.

  8. In the Cause field, enter Generic Cause.

  9. Select the Print post content and Print contributed variables checkboxes.

  10. On the Pipeline tab, from the Definition drop-down list, select Pipeline script.

  11. In the Script box, specify the code:

    def SLA_SUCCESS_STATUS = "SUCCESS"
    def SLA_FAILED_STATUS = "FAILURE"
    def GREEN_JOB_NAME = "Green_Job"
    def RED_JOB_NAME = "Red_Job"
    
    
    pipeline {
        agent { node "master" }
    
        stages {
            stage("Receive webhook") {
                steps {
                    script {
                        echo "pipeline executed via webhook"
    
                        def response = httpRequest(url: "https://api.platform.io/test-srv/api/testResult/sla?testId=${test_id}",
                            customHeaders: [[name: 'api-token', value: 'API token']],
                            httpMode: 'GET',
                            consoleLogResponseBody: true,
                            contentType: 'APPLICATION_JSON'
                        )
    
                        def responseBody = readJSON text: response.content
    
                        def slaStatus = responseBody['slaResults']['status']
    
                        echo "${slaStatus}"
    
                        if (slaStatus.equalsIgnoreCase(SLA_SUCCESS_STATUS)) {
                            build(job: GREEN_JOB_NAME)
                        }
    
                        if (slaStatus.equalsIgnoreCase(SLA_FAILED_STATUS)) {
                            build(job: RED_JOB_NAME)
                        }
                    }
                }
            }
        }
    }
    

    The specified script:

    1. sends a GET request to get the SLA with the test ID received after the test was run.

    2. analyzes the SLA status:

      • If the SLA is SUCCESS, then the script runs the Green_Job pipeline.

      • If the SLA is FAILURE, then the script runs the Red_Job pipeline.

  12. Make sure that the Use Groovy Sandbox checkbox is cleared.

  13. Click Save.

Create pipelines that run depending on the result of the SLA#

Based on the results of the SLA, a decision is made to move to the next stage of the pipeline, for example:

  • If the test results aren’t satisfactory, the code is returned for revision.

  • If testing was successful, the app is deployed into the production environment.

For our example, let’s create pipelines that just output a message to the console:

  1. Click New Item in Jenkins.

  2. Enter Green_Job as the project name, select Pipeline, and click OK.

  3. From the GitLab Connection drop-down list, select the necessary value.

  4. On the Pipeline tab, from the Definition drop-down list, select Pipeline script.

  5. In the Script box, specify the code:

    pipeline {
        agent { node "master" }
    
        stages {
            stage("Execute green quality gate step") {
                steps {
                    script {
                        echo "This is green job for demo!"
                    }
                }
            }
        }
    }
    
  6. Make sure that the Use Groovy Sandbox checkbox is cleared.

  7. Click Save.

  8. Similarly, create the Red_Job pipeline using the script:

    pipeline {
        agent { node "master" }
    
        stages {
            stage("Execute red quality gate step") {
                steps {
                    script {
                        echo "This is green job for demo!"
                    }
                }
            }
        }
    }
    

Run test in Jenkins#

  1. Open the Jenkins project to run the test.

  2. Click Build with parameters.

  3. Fill in the fields:

    • secret_api_key. API token.

    • test_project_id. The ID of the test. To find the ID, follow these steps:

      1. In PFLB Platform, go to the Tests page.

      2. Select the test. The ID of the test is displayed in the address bar:

    • test_project_version_id. The ID of the test’s version. To find the ID, follow these steps:

      1. In PFLB Platform, go to the Test runs page.

      2. Select the test run.

      3. Click Test. The version of the test is displayed in the address bar: testVersionId

  4. Click Build.

After the test is started, the pipeline for getting SLA is automatically started.

Run a test periodically#

  1. Open the Jenkins project to run the test and click Configure.

  2. On the Build Triggers tab, select the Build periodically checkbox.

  3. In the Schedule field, specify a cron expression. For example, to make requests every working day at 22:00:

    0 22 * * 1-5
    
  4. Click Save.

To configure the execution of requests by event, for example, by commit, use the Jenkins instructions for the technology stack you are using.

View results#

To view SLA results, follow these steps:

  1. Open the Jenkins project to get SLA.

  2. In the build history, select a build:

    ../_images/um_ci_jenkins_build_history.en.png
  3. Click Console Output.