From ff39a434e915f081ac35cd8e6a60728be2659ed0 Mon Sep 17 00:00:00 2001 From: Trevor Yager Date: Thu, 16 Sep 2021 12:56:06 -0400 Subject: [PATCH 1/5] Making the merges also squash commits. When this is done via the CLI, the pull request will remain open in the Bitbucket UI. That is why I switched to having Bitbucket merge and squash the pull request via the Bitbucket API --- generators/app/templates/static/build/merge.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/generators/app/templates/static/build/merge.sh b/generators/app/templates/static/build/merge.sh index 3267aac..4e037cf 100755 --- a/generators/app/templates/static/build/merge.sh +++ b/generators/app/templates/static/build/merge.sh @@ -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. \ No newline at end of file From 063799f85fd89ed2fe6e556dac72342c34f6e516 Mon Sep 17 00:00:00 2001 From: Trevor Yager Date: Thu, 16 Sep 2021 12:56:31 -0400 Subject: [PATCH 2/5] Updating the package.sh file to always build a backup of what already existed in the org. --- generators/app/templates/static/build/package.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/generators/app/templates/static/build/package.sh b/generators/app/templates/static/build/package.sh index f7761a0..282d241 100755 --- a/generators/app/templates/static/build/package.sh +++ b/generators/app/templates/static/build/package.sh @@ -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 From 8441c58d899de1a8cd0665117820102099ed7a00 Mon Sep 17 00:00:00 2001 From: Trevor Yager Date: Thu, 16 Sep 2021 12:57:17 -0400 Subject: [PATCH 3/5] Adding a new shell file that will create a pull request that contains the revert of a specified hash. It then uses the Bitbucket API to create a pull request that should then trigger a pipeline for it. --- .../static/build/prepareRollbackBranch.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 generators/app/templates/static/build/prepareRollbackBranch.sh diff --git a/generators/app/templates/static/build/prepareRollbackBranch.sh b/generators/app/templates/static/build/prepareRollbackBranch.sh new file mode 100755 index 0000000..0c8ad37 --- /dev/null +++ b/generators/app/templates/static/build/prepareRollbackBranch.sh @@ -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 }" \ No newline at end of file From e57f94728090a6cec843a1300357983750e83070 Mon Sep 17 00:00:00 2001 From: Trevor Yager Date: Thu, 16 Sep 2021 12:58:13 -0400 Subject: [PATCH 4/5] Updating the Bitbucket pipeline yaml file to include a custom pipeline for rollbacks. Added a new pipeline for pull requests that start with rollback/ . We want to automatically build and check rollback packages when a rollback pull request is created. --- .../templates/static/bitbucket-pipelines.yml | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/generators/app/templates/static/bitbucket-pipelines.yml b/generators/app/templates/static/bitbucket-pipelines.yml index d5f32d7..132ea54 100644 --- a/generators/app/templates/static/bitbucket-pipelines.yml +++ b/generators/app/templates/static/bitbucket-pipelines.yml @@ -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 @@ -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 From e41125eea42799e3d2d6033a5a971ec0719e9a23 Mon Sep 17 00:00:00 2001 From: Trevor Yager Date: Thu, 16 Sep 2021 13:07:46 -0400 Subject: [PATCH 5/5] Fixing issue with rollback pull requests. Needed the * --- generators/app/templates/static/bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/app/templates/static/bitbucket-pipelines.yml b/generators/app/templates/static/bitbucket-pipelines.yml index 132ea54..6da59ed 100644 --- a/generators/app/templates/static/bitbucket-pipelines.yml +++ b/generators/app/templates/static/bitbucket-pipelines.yml @@ -44,7 +44,7 @@ pipelines: - echo $DEPLOY_ID - sfdx force:mdapi:deploy -q $DEPLOY_ID -w 1000 - ./build/merge.sh - "rollback/": + "rollback/*": - step: name: "Build Package" script: