The following example shows how to use Momentic with Jenkins.

For more usage examples, see the momentic-ai/examples repository.

For a given root package.json:

package.json
{
  "name": "my-momentic-repo",
  "scripts": {
    "install-browsers": "momentic install-browsers --all",
    "test": "momentic run",
    "upload-results": "momentic results upload test-results"
  },
  "devDependencies": {
    "momentic": "latest"
  }
}

Create a file called Jenkinsfile in your repository with the following contents:

Jenkinsfile
pipeline {
    agent any

    tools {
        nodejs 'NodeJS 20'
    }

    stages {
        stage('Checkout Code') {
            steps {
                checkout scm
            }
        }

        stage('Install Dependencies') {
            steps {
                sh 'npm ci'
            }
        }

        stage('Install Browsers for Momentic') {
            steps {
                sh 'npm run install-browsers'
            }
        }

        stage('Run Momentic Tests and Upload Results') {
            steps {
                sh 'npm run test'
            }
            post {
                always {
                    sh 'npm run upload-results'
                }
            }
        }
    }
}

Authentication

To run any commands, you must authenticate with Momentic. You can do this by adding the momentic-api-key credential to your Jenkins instance.

  1. Create an API key in Momentic Cloud.

Copy the value to a safe place. You’ll need it in a moment.

  1. Add a new global credential to your Jenkins controller.
  1. At the top of your Jenkinsfile, provide the following environment variables to the pipeline:
Jenkinsfile
pipeline {
    agent any

    // To authenticate, set the following environment variables.
    environment {
        MOMENTIC_API_KEY = credentials('momentic-api-key')
    }

    // ...
}