Skip to content

Instantly share code, notes, and snippets.

@dtsolis
Created May 13, 2018 20:04
Show Gist options
  • Save dtsolis/4e45b1fb7057678775102f1d51be365e to your computer and use it in GitHub Desktop.
Save dtsolis/4e45b1fb7057678775102f1d51be365e to your computer and use it in GitHub Desktop.
Update build status on last commit, on bitbucket, whenever build on AppCenter finishes.
#!/usr/bin/env bash -e
#
# Update build status on last commit, on bitbucket,
# whenever build on AppCenter finishes.
#
# Copyright 2018, Dimitris-Sotiris Tsolis
# MIT License, http://www.opensource.org/licenses/mit-license.php
cd $APPCENTER_SOURCE_DIRECTORY
ORG=MyOrganization # !!! replace with your orginazation on AppCenter
APP=MySampleApplication # !!! replace with your app's name as it appears on the url next to "/apps/"
# DO NOT KEEP those 2 vars here...
# ...export them on AppCenter's build configuration instead
USERNAME='<REPLACE-WITH-YOUR-BITBUCKET-USERNAME>'
PASSWORD='<REPLACE-WITH-APP-PASSWORD-WITH-READ-PERMISSIONS>' # See how to get one: https://developer.atlassian.com/bitbucket/api/2/reference/meta/authentication#app-pw
# 1. Info about bitbucket repo
REMOTE_URL=`git config --get remote.origin.url` # NOTE: AppCenter uses https://.. url when cloning
LAST_PATH_COMP="$(echo ${REMOTE_URL} | cut -d'/' -f5)"
REPO_OWNER="$(echo ${REMOTE_URL} | cut -d'/' -f4)"
REPO_SLUG="$(echo ${LAST_PATH_COMP} | cut -d'.' -f1)"
COMMIT_HASH=`git rev-parse HEAD`
# 2. Info related to the status update on bitbucket
description=`git reflog -1 | sed 's/^.*: //'`
key="${APP}-${APPCENTER_BRANCH}"
name="${APP}-${APPCENTER_BRANCH}-${APPCENTER_BUILD_ID}"
# 3. URLs
build_url=https://appcenter.ms/orgs/$ORG/apps/$APP/build/branches/$APPCENTER_BRANCH/builds/$APPCENTER_BUILD_ID
bitbucket_url="https://api.bitbucket.org/2.0/repositories/${REPO_OWNER}/${REPO_SLUG}/commit/${COMMIT_HASH}/statuses/build"
bitbucket_mark_build() {
local state
local "${@}"
curl -X POST \
${bitbucket_url} \
-u ${USERNAME}:${PASSWORD} \
-H 'content-type: application/json' \
-d @<(cat <<EOF
{
"key": "${key}",
"state": "${state}",
"name": "${name}",
"url": "${build_url}",
"description": "${description}"
}
EOF
)
}
bitbucket_mark_build_passed() {
bitbucket_mark_build state="SUCCESSFUL"
}
bitbucket_mark_build_failed() {
bitbucket_mark_build state="FAILED"
}
bitbucket_mark_build_inprogress() {
bitbucket_mark_build state="INPROGRESS"
}
if [ "$AGENT_JOBSTATUS" != "Succeeded" ]; then
bitbucket_mark_build_failed
exit 0
else
bitbucket_mark_build_passed
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment