forked from wp-invoice/wp-invoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
232 lines (205 loc) · 6.51 KB
/
build.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
############################################################################################
#
# Automatic Distribution Build and Tag creating on GitHub
#
############################################################################################
#
# Script by default does the following steps:
# - creates temp directory
# - clones git repository there
# - creates temp branch
# - installs composer and nodes dependencies
# - adds vendor directory ( composer dependencies ) to commit
# - clears out build
# - commits build to temp branch
# - creates new tag
# - removes temp branch
# - removes temp directory
#
############################################################################################
#
# Options:
# - $1 ( $RELEASE_VERSION ) - Required. Tag version which will be created for current build
#
############################################################################################
#
# Features:
# - The current script generates new Tag on GitHub for your build (Distributive).
# - circleci compatible. It can use the latest commit log for creating new tag via CircleCI.
# Log message should contain [release:{tag}] shortcode
#
############################################################################################
#
# Examples:
#
# Run remote sh file:
# curl -s https://url-to-release-sh-file.sh | RELEASE_VERSION=1.2.3 sh
#
# Run local sh file
# sh build.sh 1.2.3
#
# Run grunt task ( see information about gruntfile.js below )
# grunt release:1.2.3
#
############################################################################################
#
# CircleCi
# The current script can be triggered on CircleCi.
# Add the following settings to your circle.yml file:
#
# deployment:
# production:
# branch: master
# commands:
# - sh build.sh
#
# Notes:
# - Log ( commit ) message should contain [release:{tag}] shortcode for running script.
# - script will be triggered only on successful (green) build for 'master' branch in
# current example.
# - in random cases gist file is not available on curl request, I suggest to
# download script and call it directly.
#
# More details about CircleCi deployment:
# https://circleci.com/docs/configuration#deployment
#
############################################################################################
#
# Gruntfile.js
#
# module.exports = function release( grunt ) {
#
# grunt.initConfig( {
#
# shell: {
# release: {
# command: function( tag ) {
# return 'sh build.sh ' + tag;
# },
# options: {
# encoding: 'utf8',
# stderr: true,
# stdout: true
# }
# }
# }
#
# } );
#
# grunt.registerTask( 'release', 'Run release tasks.', function( tag ) {
# if ( tag == null ) grunt.warn( 'Release tag must be specified, like release:1.0.0' );
# grunt.task.run( 'shell:release:' + tag );
# });
#
# }
#
#
######################################################################################
echo " "
echo "Running build script..."
echo "---"
if [ -z $RELEASE_VERSION ] ; then
# Try to get Tag version which should be created.
if [ -z $1 ] ; then
echo "Tag version parameter is not passed."
echo "Determine if we have [release:{version}] shortcode to deploy new release"
RELEASE_VERSION="$( git log -1 --pretty=%s | sed -n 's/.*\[release\:\(.*\)\].*/\1/p' )"
else
echo "Tag version parameter is "$1
RELEASE_VERSION=$1
fi
else
echo "Tag version parameter is "$RELEASE_VERSION
fi
echo "---"
if [ -z $RELEASE_VERSION ] ; then
echo "No [release:{tag}] shortcode found."
echo "Finish process."
exit 0
else
echo "Determine current branch:"
if [ -z $CIRCLE_BRANCH ]; then
CIRCLE_BRANCH=$(git rev-parse --abbrev-ref HEAD)
fi
echo $CIRCLE_BRANCH
echo "---"
# Remove temp directory if it already exists to prevent issues before proceed
if [ -d temp-build-$RELEASE_VERSION ]; then
rm -rf temp-build-$RELEASE_VERSION
fi
echo "Create temp directory"
mkdir temp-build-$RELEASE_VERSION
cd temp-build-$RELEASE_VERSION
echo "Do production build from scratch to temp directory"
ORIGIN_URL="$( git config --get remote.origin.url )"
git clone $ORIGIN_URL
cd "$( basename `git rev-parse --show-toplevel` )"
# Be sure we are on the same branch
git checkout $CIRCLE_BRANCH
echo "---"
#echo "Clean up structure ( remove composer relations )"
#rm -rf composer.lock
#rm -rf vendor
#echo "Running: composer install --no-dev --no-interaction"
#composer install --no-dev --no-interaction --quiet
#echo "---"
echo "Create local and remote temp branch temp-automatic-branch-"$RELEASE_VERSION
git checkout -b temp-branch-$RELEASE_VERSION
git push origin temp-branch-$RELEASE_VERSION
git branch --set-upstream-to=origin/temp-branch-$RELEASE_VERSION temp-branch-$RELEASE_VERSION
echo "---"
# It's used only by CircleCi. Should not be called directly.
#
#echo "Set configuration to proceed"
#git config --global push.default simple
#git config --global user.email "$( git log -1 --pretty=%an )"
#git config --global user.name "$( git log -1 --pretty=%ae )"
#echo "---"
echo "Install Node modules to minify composer.json"
npm install
grunt json-minify
echo "Be sure we do not add node and other specific files needed only for development"
rm -rf vendor/composer/installers
rm -rf coverage.clover
rm -rf ocular.phar
rm -rf build
rm -rf node_modules
rm -rf composer.lock
rm -rf .scrutinizer.yml
rm -rf circle.yml
rm -rf build.sh
rm -rf gruntfile.js
rm -rf makefile
rm -rf package.json
rm -rf test
echo "Be sure we do not add .git directories"
find ./vendor -name .git -exec rm -rf '{}' \;
echo "Be sure we do not add .svn directories"
find ./vendor -name .svn -exec rm -rf '{}' \;
echo "Git Add"
git add --all
echo "Be sure we added vendor directory"
git add -f vendor
echo "---"
echo "Now commit our build to remote branch"
git commit -m "[ci skip] Distributive Auto Build" --quiet
git pull
git push --quiet
echo "---"
echo "Finally, create tag "$RELEASE_VERSION
git tag -a $RELEASE_VERSION -m "v"$RELEASE_VERSION" - Distributive Auto Build"
git push origin $RELEASE_VERSION
echo "---"
echo "Remove local and remote temp branches, but switch to previous branch before"
git checkout $CIRCLE_BRANCH
git push origin --delete temp-branch-$RELEASE_VERSION
git branch -D temp-branch-$RELEASE_VERSION
echo "---"
# Remove temp directory.
echo "Remove temp directory"
cd ../..
rm -rf temp-build-$RELEASE_VERSION
echo "---"
echo "Done"
fi