Skip to content

Instantly share code, notes, and snippets.

@karimsa
Last active September 6, 2018 16:09
Show Gist options
  • Save karimsa/1b517493a185c74aff499238253a41ba to your computer and use it in GitHub Desktop.
Save karimsa/1b517493a185c74aff499238253a41ba to your computer and use it in GitHub Desktop.
Create a combined branch for deploying multiple branches to a single test environment
#!/bin/sh -ex
## merge.sh
##
## The purpose of this script is to cover the merging of a given list of branches so that
## multiple branches can be deployed to the same test environment in order to test PRs on
## a staging environment before merging the PR.
##
## Licensed under MIT license.
## Copyright (C) Foko Inc. All rights reserved.
## Define your list of branches separated by commas
## Typically, this would be specified using build params and not be
## statically defined in the script
export BRANCHES="branch_a,branch_b,develop"
## Define the temporary branch name - this can be statically defined
## just needs to avoid a reserved branch name that may be a part of your
## workflow
export DEPLOYMENT_BRANCH="tmp_develop"
## Fetch the remote - not required typically on a CI but needed locally
## Watch for auth here
git fetch --all --tags --prune
## Grab first of the branches to use as the base since the branches might
## not share master as a common branch
git checkout "$(echo $BRANCHES | cut -d, -f1)"
## Cleanup branch if it exists, otherwise ignore
git branch -D "$DEPLOYMENT_BRANCH" || true
## Create a temporary branch for deployment
git checkout -b "$DEPLOYMENT_BRANCH"
## Execute non-interactive merges against each of the target branches
echo "$BRANCHES" | tr ',' '\n' | while read BRANCH; do
## On failure, abort the merge to cleanup for the next run & fail
if ! git merge --no-edit "origin/$BRANCH"; then
git merge --abort
echo "Failed to merge branches."
exit 1
fi
done
## ...
## You can now deploy "$DEPLOYMENT_BRANCH" however you wish - or just the current
## directory since it contains the merged branch.
##
## Note: the branch only exists locally so if you have a pull-based deployment, you
## need to do a `git push origin $DEPLOYMENT_BRANCH`.
## ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment