Jenkins#
Goals#
Run tests in Jenkins.
Receive SLA results automatically.
Run a pipeline depending on SLA results.
Solution#
Install Jenkins plugins:
Create a pipeline to get SLA and webhook from PFLB Platform.
Run the test or configure it to run on a schedule.
Create and configure a test in PFLB Platform#
In the settings set, go to the Webhooks tab.
Click + Add new webhook.
From the Test status drop-down list, select the Finished and Canceled values.
Click Edit request.
In the URL field, enter
JENKINS_URL/generic-webhook-trigger/invoke
.In the Key field, enter
token
. In the Value field enter a Jenkins token:Click Save.
Create a pipeline to run the test#
Click New Item in Jenkins.
Enter a name for your project, select Pipeline, and click OK.
From the GitLab Connection drop-down list, select the necessary value.
Select the This project is parameterized checkbox.
Click Add Parameter and select String Parameter.
In the Name field, enter
secret_api_key
— API token:Select the Trim the string checkbox.
Similarly, add the parameters:
testId. The unique identifier of your test.
testVersionId. The unique identifier of your test’s version.
Parameter values are specified when building a Jenkins project.
On the Pipeline tab, from the Definition drop-down list, select Pipeline script:
In the Script box, specify the code:
pipeline { agent { node "master" } stages { stage("Start test") { steps { script { def patchOrg = """ {"testId": ${test_id}, "testVersionId": ${test_version_id}} """ def response = httpRequest(url: "https://platform.pflb.us/public-api/v1/testRun", 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:
sends the POST request for running a test.
saves the response to the request.
Make sure that the Use Groovy Sandbox checkbox is cleared.
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:
Click New Item in Jenkins.
Enter a name for your project, select Pipeline, and click OK.
From the GitLab Connection drop-down list, select the necessary value.
On the Build Triggers tab, select the Generic Webhook Trigger checkbox.
For Post content parameters pane, click Add:
Fill in the fields:
In the Variable field, enter
testRunId
. This is the test run ID.Select JSONPath.
In the Expression field, enter
$.id
. The JSONPath expression is used to extract the test run ID after the test has run to get the SLA.
In the Token field, specify the Jenkins token.
In the Cause field, enter
Generic Cause
.Select the Print post content and Print contributed variables checkboxes.
On the Pipeline tab, from the Definition drop-down list, select Pipeline script.
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://platform.pflb.us/public-api/v1/testRun/sla?testRunId=${test_run_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:
sends a GET request to get the SLA with the test ID received after the test was run.
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.
Make sure that the Use Groovy Sandbox checkbox is cleared.
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:
Click New Item in Jenkins.
Enter
Green_Job
as the project name, select Pipeline, and click OK.From the GitLab Connection drop-down list, select the necessary value.
On the Pipeline tab, from the Definition drop-down list, select Pipeline script.
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!" } } } } }
Make sure that the Use Groovy Sandbox checkbox is cleared.
Click Save.
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#
Open the Jenkins project to run the test.
Click Build with parameters.
Fill in the fields:
secret_api_key. API token.
test_id. The unique identifier of the test. To find the ID, follow these steps:
In PFLB Platform, go to the Tests page.
Select the test. The ID of the test is displayed in the address bar:
test_version_id. The unique identifier of the test’s version. To find the ID, follow these steps:
In PFLB Platform, go to the Test runs page.
Select the test run.
Click Test. The ID of the test version is displayed in the address bar:
Click Build.
After the test is started, the pipeline for getting SLA is automatically started.
See also
Run a test periodically#
Open the Jenkins project to run the test and click Configure.
On the Build Triggers tab, select the Build periodically checkbox.
In the Schedule field, specify a cron expression. For example, to make requests every working day at 22:00:
0 22 * * 1-5
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:
Open the Jenkins project to get SLA.
In the build history, select a build:
Click Console Output.