Skip to content

Instantly share code, notes, and snippets.

@ambud
Created August 10, 2016 23:14
Show Gist options
  • Save ambud/d0848527ebb051f19ba764534f2d13cc to your computer and use it in GitHub Desktop.
Save ambud/d0848527ebb051f19ba764534f2d13cc to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# Ref: https://raw.githubusercontent.com/cdown/travis-automerge/master/travis-automerge
: "${BRANCHES_TO_MERGE_REGEX?}" "${BRANCH_TO_MERGE_INTO?}"
: "${GITHUB_SECRET_TOKEN?}" "${GITHUB_REPO?}"
export GIT_COMMITTER_EMAIL='travis@travis'
export GIT_COMMITTER_NAME='Travis CI'
if ! grep -q "$BRANCHES_TO_MERGE_REGEX" <<< "$TRAVIS_BRANCH"; then
printf "Current branch %s doesn't match regex %s, exiting\\n" \
"$TRAVIS_BRANCH" "$BRANCHES_TO_MERGE_REGEX" >&2
exit 0
fi
# Since Travis does a partial checkout, we need to get the whole thing
repo_temp=$(mktemp -d)
git clone "https://github.com/$GITHUB_REPO" "$repo_temp"
# shellcheck disable=SC2164
cd "$repo_temp"
printf 'Checking out %s\n' "$BRANCH_TO_MERGE_INTO" >&2
git checkout "$BRANCH_TO_MERGE_INTO"
printf 'Merging %s\n' "$TRAVIS_COMMIT" >&2
git merge --ff-only "$TRAVIS_COMMIT"
printf 'Pushing to %s\n' "$GITHUB_REPO" >&2
push_uri="https://[email protected]/$GITHUB_REPO"
# Redirect to /dev/null to avoid secret leakage
git push "$push_uri" "$BRANCH_TO_MERGE_INTO" >/dev/null 2>&1
git push "$push_uri" :"$TRAVIS_BRANCH" >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment