// Run the build on a node with the 'docker' labelnode("docker"){checkoutscm// MAPI_TOKEN - The API Token secret text added to CredentialswithCredentials([string(credentialsId:"${MAPI_TOKEN}",variable:"MAPI_TOKEN")]){//// 1. BUILD AND TEST YOUR API HERE//stage("Run Mayhem"){//// 2. Start your API// eg. http://localhost:8080/api////// 3. Download the CLI (or use Jenkins Tools// see: https://github.com/jenkinsci/custom-tools-plugin/blob/master/README.md)// Replace $(MAYHEM_URL) with your instance's URL, e.g., https://app.mayhem.security//sh''' curl -Lo mapi $(MAYHEM_URL)/cli/mapi/linux-musl/latest/mapi \ && chmod +x mapi '''//// 4. Check your API//sh''' mapi run my-api auto <path_to_openapi_spec> \ --url 'http://localhost:8080/api' \ --junit results.xml '''//// 5. Collect junit results//junittestResults:'results.xml'}}}
Now that we've shown you the Jenkinsfile that you'll need to properly integrate Mayhem with Jenkins, let's walk through a working example.
Info
For this example we are forking the assets located at mcode-action-examples and integrating Mayhem into Jenkins for the underlying targets.
To get the config working in Jenkins, you will have to set up the following credentials in Jenkins. For this example, we've set the following pipeline variables:
MAYHEM_CREDENTIALS: Takes a username and password, where username is your Mayhem user and password is your Mayhem API token.
MAYHEM_URL: The URL to the Mayhem server. Here we set https://app.mayhem.security.
In addition, for the above Jenkinsfile configuration, we build and push our Docker image to the GitHub Container Registry. Therefore, make sure to set your project's visibility to Public to allow Mayhem to pull from the GitHub Container Registry for the repository Docker image.
Once you've completed the above steps, you should be able to run the Jenkins pipeline, which will execute a Mayhem run against the underlying targets to test for vulnerabilities.