Bitbucket Pipelines

Notify Bugsnag of builds using Bitbucket Pipelines.

By sending the source revision when you build a new version of your app using Bitbucket Pipelines, you’ll be able to see which build each error was introduced in and link back to the code in the repo.

Set up Pipeline

  1. Create the following script called bugsnag-build.sh in your repo and make sure it has execute permissions.

    #!/bin/bash -e
    
    # Usage: bugsnag-build.sh [release-stage]
    # release-stage           The release stage (Optional, defaults to “production”).
    # Environment variable:
    # BUGSNAG_API_KEY         The API key associated with the project, required.
    # BUGSNAG_AUTO_ASSIGN     Auto assign all new errors in this release-stage to this build (Optional, defaults to “false“)
    # BUGSNAG_APP_VERSION     The version of the app being built (Optional, defaults to “BITBUCKET_BUILD_NUMBER“)
    
    RELEASE_STAGE=$1
    if [ -z "$RELEASE_STAGE" ]; then
      RELEASE_STAGE="production"
    fi
    
    if [ -z "$BUGSNAG_API_KEY" ]; then
      echo "Bugsnag API key must be set"
      exit 1
    fi
    
    if [ "$BUGSNAG_AUTO_ASSIGN" = true ]; then
      AUTO_ASSIGN_RELEASE="true"
    else
      AUTO_ASSIGN_RELEASE="false"
    fi
    
    if [ -z "$BUGSNAG_APP_VERSION" ]; then
      APP_VERSION=${BITBUCKET_BUILD_NUMBER}
    else
      APP_VERSION=${BUGSNAG_APP_VERSION}
    fi
    
    curl https://build.bugsnag.com/ \
      -X POST \
      -H "Content-Type: application/json" \
      -d "{\"apiKey\":\"${BUGSNAG_API_KEY}\",\"appVersion\":\"${APP_VERSION}\",\"releaseStage\":\"${RELEASE_STAGE}\",\"autoAssignRelease\":${AUTO_ASSIGN_RELEASE},\"buildTool\":\"bitbucket-pipelines\",\"sourceControl\":{\"repository\":\"https://bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}\",\"revision\":\"${BITBUCKET_COMMIT}\"}}"
    
  2. Define the following environment variable in Bitbucket Repository Settings -> Pipelines -> Environment variables.

    • BUGSNAG_API_KEY - The API key for your Bugsnag project
  3. In bitbucket-pipelines.yml execute the script in your build, passing the release stage as an argument to the script. For example, to notify Bugsnag of a production build every time a commit is made to master add the following:

    pipelines:
      branches:
        master:
          - step:
             script:
               - ./bugsnag-build.sh production
    
  4. Configure the BUGSNAG_APP_VERSION depending on how the appVersion is set in your application:

    • If appVersion is configured within your notifier you should export this value to the BUGSNAG_APP_VERSION environment variable to be picked up by the script. BUGSNAG_AUTO_ASSIGN should not be set.
    • If not, you should set BUGSNAG_AUTO_ASSIGN to true. BUGSNAG_APP_VERSION can optionally be set, and will default to the automatically incrementing BITBUCKET_BUILD_NUMBER.

Configuration options

You can set the following environmental variables to override or specify additional deploy information:

Variable Description
BUGSNAG_API_KEY Your Bugsnag API key (required)
BUGSNAG_APP_VERSION The building applications version (optional, defaults to BITBUCKET_BUILD_NUMBER)
BUGSNAG_AUTO_ASSIGN Whether new errors in this release stage should be automatically assigned to this build. This should only be set to true if you are unable to configure the appVersion in your notifier (optional, defaults to false)