Using OpsGenie To Track Deployments With CloudBees CodeShip Pro

3 minute read

About OpsGenie

OpsGenie lets you send alerts and notifications about critical events to your on-call or development team.

By using OpsGenie your engineering team can respond to important alerts quickly and run your applications without concern.

Their documentation does a great job of providing more information, in addition to the setup instructions below.

CloudBees CodeShip Pro

Webhook Method

The simplest way to integrate OpsGenie with CodeShip is to use the webhook integration. From inside your OpsGenie account, you will first want to enable the CodeShip integration.

Once the CodeShip integration is enabled in OpsGenie, you will want to create a custom webhook in your project’s notifications using the webhook destination URL provided by the integrations page in OpsGenie.

All successful CodeShip builds will now complete with a webhook to OpsGenie to trigger your configured alerts.

Manual Integration

For a more granular implementation, where you can choose exactly what to trigger on OpsGenie and when, you can manually integrate the OpsGenie API into your pipeline.

Setting Your API Key

First, you will need to add your OpsGenie API key to your encrypted environment variables that you encrypt and include in your codeship-services.yml file.

Sending Alerts

Next, you will need to add API calls to OpsGenie, in your codeship-steps.yml file.

There are a couple common ways you may do this:

  • To alert of a successful deployment, as the last step in your pipelines

  • Combined with your deployment commands in a script, to alert of a failed deployment.

This example will run a deployment command, and then a script that would theoretically call the relevant OpsGenie API endpoints if that deployment is successful (meaning, if it moves on to the next step.)

- name: deploy service: app tag: master command: your deployment commands here - name: opsgenie service: app tag: master command: opsgenie.sh

Alternatively, you could combine your deployment scripts with an OpsGenie API call, in a custom script that would alert OpsGenie if a deployment command fails before ultimately surfacing an exit status code 1 (or or anything other than 0) to instruct CodeShip to fail to the build:

- name: deploy service: app tag: master command: deploy.sh

The OpsGenie API call itself, however implemented, will likely look similar to:

curl -XPOST 'https://api.opsgenie.com/v1/json/alert' -d ' { "apiKey": "YOUR_API_KEY", "message" : "Deployment Failed", "teams" : ["ops", "managers"] }'

Although it is worth noting that their API provides a variety of endpoints and services you can implement.

CloudBees CodeShip Basic

Webhook Method

The simplest way to integrate OpsGenie with CodeShip is to use the webhook integration. From inside your OpsGenie account, you will first want to enable the CodeShip integration.

Once the CodeShip integration is enabled in OpsGenie, you will want to create a custom webhook in your project’s notifications using the webhook destination URL provided by the integrations page in OpsGenie.

All successful CodeShip builds will now complete with a webhook to OpsGenie to trigger your configured alerts.

Manual Integration

For a more granular implementation, where you can choose exactly what to trigger on OpsGenie and when, you can manually integrate the OpsGenie API into your pipeline.

Setting Your API Key

First, you will need to add your OpsGenie API key to your to your project’s environment variables.

You can do this by navigating to Project Settings and then clicking on the Environment tab.

Sending Alerts

Next, you will need to add API calls to OpsGenie via a custom-script deployment added to your deployment pipelines, most likely to alert of a successful deployment or combined with your deployment commands in a script to alert of a failed deployment.

The OpsGenie API call will likely look similar to:

curl -XPOST 'https://api.opsgenie.com/v1/json/alert' -d ' { "apiKey": "YOUR_API_KEY", "message" : "Deployment Failed", "teams" : ["ops", "managers"] }'

Although it is worth noting that their API provides a variety of endpoints and services you can implement.