Bitbucket Pipelines' after-script section runs after each step and receives the step's exit code, making it a reliable hook for logging deployments to Alert24.
Before you start
You'll need:
- An Alert24 API key with
writescope, stored as a Bitbucket repository variable (ALERT24_API_KEY) under Repository settings → Pipelines → Repository variables (mark as secured) - A Bitbucket Pipelines configuration with a deployment step
Add to your bitbucket-pipelines.yml
pipelines:
branches:
main:
- step:
name: Deploy to Production
deployment: production
script:
- ./scripts/deploy.sh
after-script:
- |
if [ $BITBUCKET_EXIT_CODE -eq 0 ]; then
curl -s -X POST https://app.alert24.net/api/v1/changes \
-H "Authorization: Bearer $ALERT24_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"summary\": \"Deployed $BITBUCKET_REPO_SLUG @ ${BITBUCKET_COMMIT:0:8}\",
\"change_type\": \"deployment\",
\"environment\": \"production\",
\"branch\": \"$BITBUCKET_BRANCH\",
\"commit_sha\": \"$BITBUCKET_COMMIT\",
\"pipeline_url\": \"https://bitbucket.org/$BITBUCKET_REPO_FULL_NAME/pipelines/results/$BITBUCKET_BUILD_NUMBER\",
\"changed_by\": \"$BITBUCKET_STEP_TRIGGERER_UUID\",
\"source\": \"bitbucket-pipelines\",
\"tags\": [\"bitbucket\", \"$BITBUCKET_BRANCH\"]
}"
fi
Using Bitbucket Deployments environments
If you use Bitbucket's deployment environments feature, the $BITBUCKET_DEPLOYMENT_ENVIRONMENT variable is available and maps directly to Alert24's environment field:
-d "{
\"environment\": \"$BITBUCKET_DEPLOYMENT_ENVIRONMENT\",
...
}"
Key Bitbucket Pipelines variables
| Bitbucket variable | Alert24 field |
|---|---|
$BITBUCKET_COMMIT |
commit_sha |
$BITBUCKET_BRANCH |
branch |
$BITBUCKET_BUILD_NUMBER |
Part of pipeline_url |
$BITBUCKET_REPO_SLUG |
Part of summary |
$BITBUCKET_DEPLOYMENT_ENVIRONMENT |
environment |
$BITBUCKET_PR_ID |
pr_number |
$BITBUCKET_EXIT_CODE |
Used to check deploy success |
Tips
$BITBUCKET_EXIT_CODE: Inafter-script, this holds the exit code of the precedingscriptblock. Check it before calling Alert24 so you only log successful deployments.- Secured variables: Bitbucket secured repository variables are masked in logs but still available in
after-script. Don't use plain text variables for the API key. - Pull request pipelines: For PR-based deployment workflows,
$BITBUCKET_PR_IDgives the PR number — include it aspr_numberin the payload.