Skip to main content
No Result Found

Integrate your Quality Gate result with your CI/CD

Learn how to integrate Quality Gate with your CI/CD tools

In order to automate workflows like blocking or allowing deployments/PRs automatically, you can integrate via the Quality Gates API.

You can get started in minutes with the following recipes that you can use out of the box or modify for your specific workflows.

Examples

Generic Shell Script to fetch and interpret Quality Gate Result

Get started in minutes by using this shell script as a starter!

How it works:

  • Plug in variables like project_name, build_name, username, access_key which you are using to set up your builds (via your framework config files or browserstack.yml files) on Test Observability. This will help retrieve the Quality Gate analysis for the latest build matching these criteria.
  • The script will automatically get the Quality Gate result for your build, assert the response and throw the right exit code.
  • This script also implements a polling loop to retry the API call for a set number of times while Test Observability completes the Quality Gate analysis.

Note 1: This sample script uses the jq tool to be installed to process the Quality Gate response and throw the right exit code. You can install jq from brew or the jq website.

Note 2: If you use the BrowserStack Jenkins Plugin, ensure you specify the $BROWSERSTACK_BUILD_NAME environment variable in the shell script where you have to enter your build_name. This will ensure the correct build’s Quality Gate result is fetched.

If you wish to modify the script, please keep the following in mind:

  • The Quality Gate Status API endpoint requires you to provide the UUID of a build - a unique identifier to distinguish builds. This script leverages another API endpoint - the Get Latest Build Details API to help get you the UUID automatically, and plug it in to the API.


Terminal
Copy icon Copy


Pipeline Example for Jenkins

In order to integrate this into Jenkins, you can refer to the example below of how you can implement this in a multi-stage pipeline.

How it works:

  • The pipeline script uses the catchError capability in Jenkins to ensure that if a pipeline stage fails, it doesn’t stop the subsequent stages from executing.
  • The pipeline script then marks the overall build status using a post stage after the Quality Gate stage completes.
  • While you can use other approaches to catchError, ensure you allow the Quality Gate to execute after your tests complete - this will help you prevent false negatives from blocking deployments - as your Quality Gate could pass despite test failures.
  • Note: Insert the shell script listed above within this pipeline example. It has been omitted in this example for brevity.
Pipeline
pipeline {
    agent any

    stages {
        stage('Build / Test Step') {
            // Place your build and test steps in separate stages with the catchError handling like below
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    script {
                        // Build and Test Steps Go Here
                    }
                }
            }
        }

        stage('Quality Gate Stage') {
            // Implement a new Quality Gate Stage like below
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    script {
                        echo 'Performing Quality Gate Check...'
                        def qualityGateResult = sh(script: '''
                        # <paste sample shell script from above here>
                        ''', returnStatus: true)
                        
                        if (qualityGateResult == 0) {
                            echo 'Quality Gate passed.'
                            currentBuild.result = 'SUCCESS'
                        } else {
                            echo 'Quality Gate failed.'
                            currentBuild.result = 'FAILURE'
                        }
                    }
                }
            }
            
            post {
                always {
                    script {
                        // Mark the stage based on currentBuild.result
                        if (currentBuild.result == 'SUCCESS') {
                            echo 'Quality Gate passed.'
                        } else {
                            echo 'Quality Gate failed.'
                            error 'Quality Gate failed.'
                        }
                    }
                }
            }
        }
    }
}


We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle