Record and monitor deployments using GitHub Actions and New Relic

In my previous post, we saw how to use GitHub Actions to deploy your application manually. Now let's learn how to keep a record of your production deployments.

Record and monitor deployments using GitHub Actions and New Relic

In my previous post, we saw how to use GitHub Actions to deploy your application manually. Now let's learn how to keep a record of your production deployments.

Manual application deployment using GitHub Actions
Did you know that you can configure your workflows to run only when manually triggered through the Actions tab on GitHub, GitHub CLI, or the REST API?

As you may already know, you can define dependent jobs within your GitHub Actions workflow. So, now let's define a release phase for our deploy job, which will run only when a production deployment is triggered.

name: Deploy

on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Environment to deploy'
        required: true
        default: 'staging'

jobs:
  deploy:
    name: Deploy to ${{ github.event.inputs.environment }}
    runs-on: ubuntu-latest
    env:
      echo "Ommited for brevity (see: https://tomasmuller.dev/manual-application-deployment-using-github-actions/)."

    steps:
      echo "Ommited for brevity (see: https://tomasmuller.dev/manual-application-deployment-using-github-actions/)."
      
  release:
    needs: deploy
    if: github.event.inputs.environment == 'production'
    runs-on: ubuntu-latest
    name: Release
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Get last commit message
        run: |
          LAST_COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s")
          echo "LAST_COMMIT_MESSAGE=$LAST_COMMIT_MESSAGE" >> $GITHUB_ENV
      - name: Create New Relic deployment marker
        run: |
          curl -X POST "https://api.newrelic.com/v2/applications/${{ secrets.NEW_RELIC_APP_ID }}/deployments.json" \
            -H "X-Api-Key:${{ secrets.NEW_RELIC_API_KEY }}" \
            -i \
            -H 'Content-Type: application/json' \
            -d \
            '{
              "deployment": {
                "revision": "${{ github.sha }}",
                "description": "${{ env.LAST_COMMIT_MESSAGE }}",
                "user": "${{ github.actor }}"
              }
            }'

Use GitHub Actions secrets to store your NEW_RELIC_APP_ID and NEW_RELIC_API_KEY.

Deploying an app can be an exciting event, but often your app breaks because of bad changes. A record of your application deployments can guide you to the root cause.

New Relic allows you to track deployments to correlate any deployment to your app's performance and anomalies. Also, tracking deployments create deployment markers that appear in APM charts.

After the deployment is recorded using the REST API, you can optionally notify a webhook endpoint of the deployment.

The destination of the webhook can be your Slack instance. To use webhooks to set up a deployment notification for a Slack channel:

  1. Log in to your Slack account as an admin, then go to App directory > Manage > Apps.
  2. Search for your New Relic app, then select Add configuration.
  3. From Post to channel, select an existing Slack channel or add a new channel, then Add configuration.
  4. From the list of options, copy the webhook URL.
  5. Go to one.newrelic.com > (account dropdown) > Account settings > Integrations > Deploy notifications > Webhook.
  6. Paste the Slack webhook URL, then save.
  7. Optional: Send a test message.

That's all. Simple and effective. Now, it's your time! Go ahead and ship something!