Skip to content

Enable rollback option #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions generators/app/templates/static/bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,44 @@ pipelines:
- dist/**
- step:
name: Quick Deploy
deployment: production
trigger: manual
script:
- ./build/setup.sh
- DEPLOY_ID=$(cat dist/deploy_id.txt)
- echo $DEPLOY_ID
- sfdx force:mdapi:deploy -q $DEPLOY_ID -w 1000
- ./build/merge.sh
"rollback/*":
- step:
name: "Build Package"
script:
- if git log -1 | grep -q "\\[skip ci\\]"; then printf 'CI Commit... Skipping pipeline!'; exit; fi
- if [ "${BITBUCKET_PR_DESTINATION_BRANCH}" != "master" ]; then printf 'Destination is not master... Skipping pipeline'; exit; fi
- ./build/setup.sh
- pr_description=$(curl -s https://${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}@api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/pullrequests/${BITBUCKET_PR_ID} | jq -r '.description')
- SKIP_SYNC=$(echo "$pr_description" | grep -i -c \!skipsync) || true
- echo Skip Sync $SKIP_SYNC
- ./build/sync.sh $SKIP_SYNC
- ./build/package.sh
artifacts:
- dist/**
- step:
name: Check Package
script:
- ./build/setup.sh
- pr_description=$(curl -s https://${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}@api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/pullrequests/${BITBUCKET_PR_ID} | jq -r '.description')
- TESTS=$(echo "$pr_description" | grep -i \!tests= | cut -d "=" -f 2-) || true
- echo Tests $TESTS
- DEPLOY_ID=$(sfdx force:mdapi:deploy ${TESTS:+ -l RunSpecifiedTests -r "${TESTS}"} -d dist/$BITBUCKET_BRANCH -c --json | jq -r .result.id)
- echo $DEPLOY_ID
- echo $DEPLOY_ID > dist/deploy_id.txt
- sfdx force:mdapi:deploy:report --jobid $DEPLOY_ID -w 1000
artifacts:
- dist/**
- step:
name: Deploy Rollback
deployment: production
trigger: manual
script:
- ./build/setup.sh
Expand All @@ -59,6 +97,13 @@ pipelines:
- ./build/package.sh
- sfdx force:mdapi:deploy -d dist/$BITBUCKET_BRANCH -w 5000 --verbose
- ./build/merge.sh
Rollback:
- variables:
- name: CommitHash
- step:
name: Prepare rollback branch
script:
- ./build/prepareRollbackBranch.sh $CommitHash
Deploy to Production (Selective Tests):
- variables:
- name: Enter1ToSkipProdSync
Expand Down
18 changes: 13 additions & 5 deletions generators/app/templates/static/build/merge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
DEPLOY_SUCCESS=$(sfdx force:mdapi:deploy:report --json | jq .result.success)
[ $DEPLOY_SUCCESS != 'true' ] && echo "Deployment Failed" && exit 1

# echo 'merge branch into master'
git checkout master
git merge $BITBUCKET_BRANCH
git push
git push origin --delete $BITBUCKET_BRANCH

echo ''
echo "Merging and squashing $BITBUCKET_BRANCH into master"
curl POST -H "Content-Type: application/json" https://${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}@api.bitbucket.org/2.0/repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/pullrequests/${BITBUCKET_PR_ID}/merge -d '{ "type": "", "close_source_branch": true, "merge_strategy": "squash" }'

# NOTE below on why we are using the bitbucket API to merge and squash the pull request
# Squashing a pull request in via command line causes the pull requests to remain open in the Bitbucket UI

# https://support.atlassian.com/bitbucket-cloud/docs/merge-a-pull-request/
# Note: When you enter git merge --squash in the command line locally, the pull request will remain in the ‘open’ state after you push the changes to Bitbucket.
# This is because we use the commit graph to detect that changes were applied, and when ‘squash merge’ is used, we cannot
# detect that the pull request was merged or display an accurate diff. The pull request will now contain identical changes
# between the two branches, so the pull request will show no diff. However, you will be able to see the commit history of the pull request and view the individual commits.
10 changes: 10 additions & 0 deletions generators/app/templates/static/build/package.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#!/bin/bash

echo 'merge master into branch'
git merge master

echo ''
echo 'building package'
sfdx git:package -d dist/$BITBUCKET_BRANCH -s $BITBUCKET_BRANCH --purge

echo ''
echo 'building backup'
sfdx git:package -d dist/backup -s master -t $BITBUCKET_BRANCH -f --purge

echo ''
echo 'package.xml for deployment'
cat dist/$BITBUCKET_BRANCH/package.xml
16 changes: 16 additions & 0 deletions generators/app/templates/static/build/prepareRollbackBranch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

echo "Reverting commit on a rollback branch"
git stash -u
git checkout master
git pull
git checkout -b rollback/${1}
git revert ${1} --no-edit

echo ""
echo "Pushing rollback branch to org"
git push origin rollback/${1}

echo ""
echo "Making a pull request"
curl -X POST -H "Content-Type: application/json" https://${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}@api.bitbucket.org/2.0/repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/pullrequests -d "{ \"title\": \"rollback/${1}\", \"description\": \"Rolling back commit ${1}\", \"source\": { \"branch\": { \"name\": \"rollback/${1}\" }, \"destination\": { \"branch\": { \"name\": \"master\" } } }, \"close_source_branch\": true }"