How to Notify New Relic of Deployments From AppVeyor

During app deployments, previous company policy required us to prepare a sacrifice so that the favour of the gods might shine upon us. I was first asked to see if this process could be automated to save overhead - but I soon realised that we could ensure deployments went smoothly without paying the salaried workers in the embalming-and-sacrificing department at all!

Now we use New Relic to get error rate metrics, and with a POST request to the New Relic API we can even track error rates between, and since the last, deployment. It's much easier to discern our fortunes this way than with haruspicy.

To set up deployment tracking in New Relic when you deploy with AppVeyor, you just need to add this POST request into a post-deploy PowerShell script.

To use the API, you'll need to first generate an API key by going to Account Settings -> Integrations -> API Keys in the New Relic dashboard.

Here's the PowerShell one-liner that we ended up using to send the POST request

Invoke-RestMethod -Uri https://api.newrelic.com/deployments.xml -Method Post -Headers @{"x-api-key"="YOUR_API_KEY"} -Body @{"deployment[application_id]"="YOUR_APPLICATION_ID"; "deployment[description]"="Your Application Deployment"}  

The application id is generally just the name of the application in New Relic.

This is very simple, and will work just fine when added as a post-deploy script in AppVeyor. Here's an example appveyor.yml with the post-deploy script added.

branches:  
  only:
  - master
  - develop
configuration:  
- Release
force_https_clone: true  
build:  
  # Your build configuration
deploy:  
- provider: WebDeploy
  # Your deploy configuration
after_deploy:  
- ps: Invoke-RestMethod -Uri https://api.newrelic.com/deployments.xml -Method Post -Headers @{"x-api-key"="YOUR_API_KEY"} -Body @{"deployment[application_id]"="YOUR_APPLICATION_ID"; "deployment[description]"="Your Application Deployment"}

Notice how the PowerShell script is added under after_deploy?

But wait! It get's much better. We can take advantage of AppVeyor's built-in environment variables to notify New Relic of the build number along with the deployment.

It just takes a little of PowerShell's easy string interpolation and we can inline the environment variables provided by AppVeyor into the deployment description.

Invoke-RestMethod -Uri https://api.newrelic.com/deployments.xml -Method Post -Headers @{"x-api-key"="YOUR_API_KEY"} -Body @{"deployment[application_id]"="YOUR_APPLICATION_ID"; "deployment[description]"="Build $env:APPVEYOR_BUILD_ID commit ID $env:APPVEYOR_REPO_COMMIT"}