-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
140 lines (120 loc) · 4.31 KB
/
run.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Suffix for missing options.
error_suffix='Please add this option to the wercker.yml or add a heroku deployment target on the website which will set these options for you.'
if [ -z "$WERCKER_HEROKU_DEPLOY_KEY" ]
then
if [ ! -z "$HEROKU_KEY" ]
then
export WERCKER_HEROKU_DEPLOY_KEY="$HEROKU_KEY"
else
fail "Missing or empty option heroku_key. $error_suffix"
fi
fi
if [ -z "$WERCKER_HEROKU_DEPLOY_APP_NAME" ]
then
if [ ! -z "$HEROKU_APP_NAME" ]
then
export WERCKER_HEROKU_DEPLOY_APP_NAME="$HEROKU_APP_NAME"
else
fail "Missing or empty option heroku_app_name. $error_suffix"
fi
fi
if [ -z "$WERCKER_HEROKU_DEPLOY_USER" ]
then
if [ ! -z "$HEROKU_USER" ]
then
export WERCKER_HEROKU_DEPLOY_USER="$HEROKU_USER"
else
export WERCKER_HEROKU_DEPLOY_USER="[email protected]"
fi
fi
if [ -z "$WERCKER_HEROKU_DEPLOY_SOURCE_DIR" ]
then
export WERCKER_HEROKU_DEPLOY_SOURCE_DIR=$WERCKER_ROOT
debug "Option source_dir not set. Will deploy directory $WERCKER_HEROKU_DEPLOY_SOURCE_DIR"
else
debug "Option source_dir found. Will deploy directory $WERCKER_HEROKU_DEPLOY_SOURCE_DIR"
fi
# Install heroku toolbelt if needed
if ! type heroku &> /dev/null ;
then
info 'heroku toolbelt not found, starting installing it'
cd $TMPDIR
result=$(sudo wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh)
if [[ $? -ne 0 ]];then
warning $result
fail 'heroku toolbelt installation failed';
else
info 'finished heroku toolbelt installation';
fi
else
info 'heroku toolbelt is available, and will not be installed by this step'
debug "type heroku: $(type heroku)"
debug "heroku version: $(heroku --version)"
fi
curl -H "Accept: application/json" -u :$WERCKER_HEROKU_DEPLOY_KEY https://api.heroku.com/apps/$WERCKER_HEROKU_DEPLOY_APP_NAME
echo "machine api.heroku.com" > /home/ubuntu/.netrc
echo " login $WERCKER_HEROKU_DEPLOY_USER" >> /home/ubuntu/.netrc
echo " password $WERCKER_HEROKU_DEPLOY_KEY" >> /home/ubuntu/.netrc
chmod 0600 /home/ubuntu/.netrc
git config --global user.name "$WERCKER_HEROKU_DEPLOY_USER"
git config --global user.email "$WERCKER_HEROKU_DEPLOY_USER"
cd
mkdir -p key
chmod 0700 ./key
cd key
if [ ! -n "$WERCKER_HEROKU_KEY_NAME" ]
then
debug "will use specified key in key-name option"
export key_file_name="$WERCKER_HEROKU_KEY_NAME"
export privateKey=$(eval echo "\$${WERCKER_HEROKU_KEY_NAME}_PRIVATE")
debug "Writing key file to $key_file_name"
echo -e "$privateKey" > $key_file_name
chmod 0600 "$key_file_name"
else
debug "no key-name specified, will generate key and add it to heroku"
#Generate random key to prevent naming collision
# This key will only be used for this deployment
export key_file_name="deploy-$RANDOM"
export key_name="[email protected]"
debug 'generating random ssh key for this deploy'
ssh-keygen -f "$key_file_name" -C "$key_name" -N '' -t rsa -q
debug "generated ssh key $key_name for this deployment"
chmod 0600 "$key_file_name"
# Add key to heroku
heroku keys:add "/home/ubuntu/key/$key_file_name.pub"
debug "added ssh key $key_file_name.pub to heroku"
fi
echo "ssh -e none -i \"/home/ubuntu/key/$key_file_name\" -o \"StrictHostKeyChecking no\" \$@" > gitssh
chmod 0700 /home/ubuntu/key/gitssh
export GIT_SSH=/home/ubuntu/key/gitssh
cd $WERCKER_HEROKU_DEPLOY_SOURCE_DIR || fail "could not change directory to source_dir \"$WERCKER_HEROKU_DEPLOY_SOURCE_DIR\""
heroku version
# If there is a git repository, remove it because
# we want to create a new git repository to push
# to heroku.
if [ -d '.git' ]
then
debug "found git repository in $(pwd)"
warn "Removing git repository from $WERCKER_ROOT"
rm -rf '.git'
fi
# Create git repository and add all files.
# This repository will get pushed to heroku.
git init
git add .
git commit -m 'wercker deploy'
# Deploy with a git push
debug "starting heroku deployment with git push"
git push -f [email protected]:$WERCKER_HEROKU_DEPLOY_APP_NAME.git master
exit_code=$?
debug "git pushed exited with $exit_code"
# Cleanup ssh key
heroku keys:remove "$key_name"
debug "removed ssh key $key_name from heroku"
# Validate git push deploy
if [ $exit_code -eq 0 ]
then
success 'deployment to heroku finished successfully'
else
fail 'git push to heroku failed'
fi