Jenkins

In this guide, we'll explore how to set up a Jenkins pipeline that triggers a test using the Niteco Performance Insights REST API. We'll focus on the /v1/tests/runtest endpoint.


Step 1: Create a New Pipeline Job

Access Jenkins Dashboard:

Open your web browser and navigate to your Jenkins instance.

Create a New Job:

Click on New Item enter a name for your job and select Pipeline. Click OK.


Configure the Pipeline

Edit the Pipeline Script:

In the job configuration, scroll down to the Pipeline section and click onPipeline script. Add the following code:

pipeline {
    agent any

    stages {
        stage('Run Tests') {
            steps {
                script {
                    // Variables for API request
                    def apiUrl = "https://perf.niteco.com/api/v1/tests/runtest"
                    def pageId = "your_page_id"         // Replace with your actual page ID
                    def device = "DESKTOP"               // Adjust as necessary
                    def location = "your_location"       // Replace with your actual location
                    def formFactor = "DESKTOP"           // Can be DESKTOP, MOBILE, etc.

                    // Make the API request
                    def response = sh(script: "curl -s -o /dev/null -w '%{http_code}' '${apiUrl}?page_id=${pageId}&device=${device}&location=${location}&formFactor=${formFactor}'", returnStdout: true).trim()

                    // Check the response status
                    if (response == "200") {
                        echo "Tests ran successfully!"
                    } else {
                        error "Tests failed with status code: ${response}"
                    }
                }
            }
        }
    }

    post {
        always {
            echo 'Pipeline completed.'
        }
        success {
            echo 'Tests passed successfully!'
        }
        failure {
            echo 'Tests failed. Please check the logs.'
        }
    }
}

Make sure you configure your test variables above:

pageId: Set this to the ID of the page you want to test.
device, location, formFactor: Modify these values according to your testing requirements.


Step 3: Save and Build the Job

Save the Configuration:

Click "Save" at the bottom of the page.

Run the Pipeline:

Click on "Build Now" to execute the pipeline.


Step 4: Monitor the Build

View Build Output:

Click on the build number in the Jenkins dashboard to see the console output.

Observe the Results:

If the tests are successful, you'll see "Tests ran successfully!" in the console. If they fail, you'll see the failure status along with the HTTP code.