This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
168 lines (133 loc) · 3.65 KB
/
deploy
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
source 'common'
PWD=`pwd`
ORIGIN_DIR=`basename $PWD`
DEPLOYMENT_DIR="deployment"
COMPILE_ASSETS=1
DEPLOY_ASSETS=1
DEPLOY_APP=1
ENV=$1
if [ ! -e "config/environments/$ENV.rb" ]
then
echo "Select your target heroku environment: staging, production"
exit 1
fi
ASSETS='public/assets'
if [ $ENV != 'production' ]
then
ASSETS="$ASSETS-$ENV"
DEPLOYMENT_DIR="$DEPLOYMENT_DIR-$ENV"
fi
HEROKU_ENV="heroku"
if [ $ENV != 'production' ]
then
HEROKU_ENV="$HEROKU_ENV-$ENV"
fi
if [[ $@ =~ ' --without-assets' ]]
then
log_brief "No assets will be deployed"
DEPLOY_ASSETS=0
fi
if [[ $@ =~ ' --without-app' ]]
then
log_brief "No app, only assets will be deployed"
DEPLOY_APP=0
fi
if [[ $@ =~ ' --without-asset-compilation' ]]
then
log_brief "No assets will be compiled"
COMPILE_ASSETS=0
fi
log create a changelog
LAST_DEPLOY_REF='-'
if [[ "$@" =~ --ref:([a-z0-9]{4,})\s* ]]; then
LAST_DEPLOY_REF="${BASH_REMATCH[1]}"
CHANGE_LOG=`git log ...$LAST_DEPLOY_REF --no-merges --pretty=format:'-- %s'`
else
CHANGE_LOG="No comment"
fi
echo Deploy from: $ORIGIN_DIR
echo Deploy to: $DEPLOYMENT_DIR
echo Target local environment: $ENV
echo Target Heroku environment: $HEROKU_ENV
echo Last Deploy Ref: $LAST_DEPLOY_REF
echo Changelog: $CHANGE_LOG
# TODO: If there are uncommitted changes to the source code, cancel the deployment
log temporarily save the assets
mv app/assets/images/ ..
# add more assets here
ls app/assets/images
log remove the old assets
rm -rf $ASSETS
log clear the asset caches
bundle exec rake tmp:clear
rake assets:clean RAILS_ENV=$ENV
log compile the assets
if [ $COMPILE_ASSETS -eq 1 ]
then
rake assets:precompile --quiet RAILS_ENV=$ENV
fi
log_brief save $ASSETS/manifest.yml
if [ ! -e "$ASSETS/manifest.yml" ]; then
echo "$ASSETS/manifest.yml not found..."
echo " be sure that it is in the source directory so it can be deployed "
echo " heroku"
exit 2
else
log_brief save $ASSETS/manifest.yml
fi
if [ $DEPLOY_ASSETS -eq 1 ]
then
# be sure that rsync_pass is chmod 600
log update assets on cdn
rsync -var --exclude 'assets*' $ASSETS rsync://yourcdn.com --password-file config/rsync_pass
log refresh the cdn edge server
curl --data "queryparameters" https://yourcdn.com/purge/all
fi
mv $ASSETS/manifest.yml ./manifest.yml
rm -rf $ASSETS; mkdir $ASSETS
mv ./manifest.yml $ASSETS/manifest.yml
changedir ..
log remove the old deployment directory and create a new one
if [ -e $DEPLOYMENT_DIR ]; then
rm -rf $DEPLOYMENT_DIR
log_brief deployment directory removed
fi
mkdir $DEPLOYMENT_DIR
log_brief done
log copy the source codes to the pre-deployment directory
changedir $ORIGIN_DIR
tar zcpf - . | (cd ../$DEPLOYMENT_DIR; tar xfp -)
log_brief done
log put back the assets into their original directories
#mv ../your-assets app/assets/images
ls app/assets/images
log_brief done
changedir ../$ORIGIN_DIR
# copy robots.txt into the deployment directory
if [ $ENV != 'production' ]
then
log_brief copying robots.txt for $ENV
cp public/robots-$ENV.txt ../$DEPLOYMENT_DIR/public/robots.txt
fi
changedir ../$DEPLOYMENT_DIR
log remove git, bundle and tmp temporary files
rm -rf .git .bundle tmp log app/assets
log init git
git init
git add .
mkdir -p public/assets
cp $ASSETS/manifest.yml public/assets/
git add public/assets/manifest.yml --force
git add $ASSETS/manifest.yml --force
log copy .git/config to the deployment directory
cp ../$ORIGIN_DIR/.git/config .git/config
log commit deployment directory with changelog message
git commit -am "$CHANGE_LOG"
if [ $DEPLOY_APP -eq 1 ]
then
log start deployment to heroku
git push $HEROKU_ENV --force
fi
changedir ../$ORIGIN_DIR
echo Last deployment ref: `git log HEAD^.. --pretty=format:'%h'`