-
Notifications
You must be signed in to change notification settings - Fork 6
/
deploy.sh
executable file
·125 lines (100 loc) · 4.14 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash -e
#
# Deploy your branch on VIP Go.
#
# This script uses various Circle CI and Travis CI environment
# variables, Circle prefix their environment variables with
# `CIRCLE_` and Travis with `TRAVIS_`.
# Documentation:
# https://circleci.com/docs/1.0/environment-variables/
# https://docs.travis-ci.com/user/environment-variables/
set -ex
# The deploy suffix flexibility is mainly here to allow
# us to test Circle and Travis builds simultaneously on
# the https://github.com/Automattic/vip-go-skeleton/ repo.
DEPLOY_SUFFIX="${VIP_DEPLOY_SUFFIX:--built}"
BRANCH="${CIRCLE_BRANCH:-$TRAVIS_BRANCH}"
SRC_DIR="${TRAVIS_BUILD_DIR:-$PWD}"
BUILD_DIR="/tmp/vip-go-build-$(date +%s)"
if [[ $CIRCLECI ]]; then
CIRCLE_REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}";
fi
REPO_SLUG=${CIRCLE_REPO_SLUG:-$TRAVIS_REPO_SLUG}
REPO_SSH_URL="git@github.com:${REPO_SLUG}"
COMMIT_SHA=${CIRCLE_SHA1:-$TRAVIS_COMMIT}
DEPLOY_BRANCH="${BRANCH}${DEPLOY_SUFFIX}"
cd $SRC_DIR
COMMIT_AUTHOR_NAME="$( git log --format=%an -n 1 ${COMMIT_SHA} )"
COMMIT_AUTHOR_EMAIL="$( git log --format=%ae -n 1 ${COMMIT_SHA} )"
COMMIT_COMMITTER_NAME="$( git log --format=%cn -n 1 ${COMMIT_SHA} )"
COMMIT_COMMITTER_EMAIL="$( git log --format=%ce -n 1 ${COMMIT_SHA} )"
# Run some checks
# ---------------
if [[ -n $TRAVIS ]] && [[ -n $CIRCLECI ]]; then
echo "ERROR: this script requires either Circle CI or Travis CI"
echo "You will need to amend the setup above to set the required variables from the information specific to your CI service or tool."
exit 1
fi
if [[ -z "${BRANCH}" ]]; then
echo "ERROR: No branch specified!"
echo "This variable should be set by Travis CI and Circle CI, if you consistently experience errors please check with WordPress.com VIP support."
exit 1
fi
if [[ -d "$BUILD_DIR" ]]; then
echo "ERROR: ${BUILD_DIR} already exists."
echo "This should not happen, if you consistently experience errors please check with WordPress.com VIP support."
exit 1
fi
if [[ "${BRANCH}" == *${DEPLOY_SUFFIX} ]]; then
echo "NOTICE: Attempting to build from branch '${BRANCH}' to deploy '${DEPLOY_BRANCH}', seems like recursion so aborting."
echo "This is a protective measure, no action is required."
exit 0
fi
if [[ -n $TRAVIS ]] && [ $TRAVIS_PULL_REQUEST != 'false' ]; then
echo "NOTICE: Aborting a build to '${DEPLOY_BRANCH}' from a pull request on '${BRANCH}', only build from merges directly to the branch"
echo "This is a protective measure, no action is required."
exit 0
fi
# Everything seems OK, getting the built repo sorted
# --------------------------------------------------
echo "Deploying ${BRANCH} to ${DEPLOY_BRANCH}"
# Making the directory we're going to sync the build into
git init "${BUILD_DIR}"
cd "${BUILD_DIR}"
git remote add origin "${REPO_SSH_URL}"
if [[ 0 = $(git ls-remote --heads "${REPO_SSH_URL}" "${DEPLOY_BRANCH}" | wc -l) ]]; then
echo -e "\nCreating a ${DEPLOY_BRANCH} branch..."
git checkout --quiet --orphan "${DEPLOY_BRANCH}"
else
echo "Using existing ${DEPLOY_BRANCH} branch"
git fetch origin "${DEPLOY_BRANCH}"
git checkout --quiet "${DEPLOY_BRANCH}"
fi
# Copy the files over
# -------------------
if ! command -v 'rsync'; then
# @FIXME Probably there's a way we could check if APT is up to date or not
# so we don't have to run update every time
sudo apt-get update
sudo apt-get install -q -y rsync
fi
echo "Syncing files... quietly"
rsync --cvs-exclude -a "${SRC_DIR}/" "${BUILD_DIR}" --exclude='.git/'
# Make up the commit, commit, and push
# ------------------------------------
# Set Git committer
git config user.name "${COMMIT_COMMITTER_NAME}"
git config user.email "${COMMIT_COMMITTER_EMAIL}"
# Add changed files, delete deleted, etc, etc, you know the drill
git add -A .
if [ -z "$(git status --porcelain)" ]; then
echo "NOTICE: No changes to deploy"
exit 0
fi
# Commit it.
MESSAGE=$( printf 'Build changes from %s\n\n%s' "${COMMIT_SHA}" "${CIRCLE_BUILD_URL}" )
# Set the Author to the commit (expected to be a client dev) and the committer
# will be set to the default Git user for this CI system
git commit --author="${COMMIT_AUTHOR_NAME} <${COMMIT_AUTHOR_EMAIL}>" -m "${MESSAGE}"
# Push it (push it real good).
git push origin "${DEPLOY_BRANCH}"