Created
January 27, 2020 14:43
-
-
Save thiagoalmeidasa/d6374ae4bfb1c7ac7ba9748af2a8b17b to your computer and use it in GitHub Desktop.
Bitbucket status notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# Push GitLab CI/CD build status to Bitbucket Cloud | |
if [ -z "$BITBUCKET_ACCESS_TOKEN" ]; then | |
echo "ERROR: BITBUCKET_ACCESS_TOKEN is not set" | |
exit 1 | |
fi | |
if [ -z "$BITBUCKET_USERNAME" ]; then | |
echo "ERROR: BITBUCKET_USERNAME is not set" | |
exit 1 | |
fi | |
if [ -z "$BITBUCKET_NAMESPACE" ]; then | |
echo "Setting BITBUCKET_NAMESPACE to $CI_PROJECT_NAMESPACE" | |
BITBUCKET_NAMESPACE=$CI_PROJECT_NAMESPACE | |
fi | |
if [ -z "$BITBUCKET_REPOSITORY" ]; then | |
echo "Setting BITBUCKET_REPOSITORY to $CI_PROJECT_NAME" | |
BITBUCKET_REPOSITORY=$CI_PROJECT_NAME | |
fi | |
BITBUCKET_API_ROOT="https://api.bitbucket.org/2.0" | |
BITBUCKET_STATUS_API="$BITBUCKET_API_ROOT/repositories/$BITBUCKET_NAMESPACE/$BITBUCKET_REPOSITORY/commit/$CI_COMMIT_SHA/statuses/build" | |
BITBUCKET_KEY="ci/gitlab-ci/$CI_PIPELINE_ID" | |
case "$BUILD_STATUS" in | |
running) | |
BITBUCKET_STATE="INPROGRESS" | |
BITBUCKET_DESCRIPTION="The build is running!" | |
;; | |
passed) | |
BITBUCKET_STATE="SUCCESSFUL" | |
BITBUCKET_DESCRIPTION="The build passed!" | |
;; | |
failed) | |
BITBUCKET_STATE="FAILED" | |
BITBUCKET_DESCRIPTION="The build failed." | |
;; | |
esac | |
echo "Pushing status to $BITBUCKET_STATUS_API..." | |
curl --request POST $BITBUCKET_STATUS_API \ | |
--user $BITBUCKET_USERNAME:$BITBUCKET_ACCESS_TOKEN \ | |
--header "Content-Type:application/json" \ | |
--silent \ | |
--data "{ \"state\": \"$BITBUCKET_STATE\", \"key\": \"$BITBUCKET_KEY\", \"description\": | |
\"$BITBUCKET_DESCRIPTION\",\"url\": \"$CI_PIPELINE_URL\" }" | |
#vim set ft=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment