概要 †
主に以下の処理を行うデプロイジョブのサンプルです。
・CSS/JS/PNG/JPEGのminifyと最適化
・ファイル名をデプロイ毎にユニークにする
・SSHでデプロイする
・Nginxのキャッシュをクリアする
コード †
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
| -
!
-
|
|
|
|
|
|
!
-
|
|
|
!
-
!
-
!
-
!
-
!
-
|
|
!
-
|
|
|
|
|
!
-
|
|
!
-
|
-
-
!
|
-
!
!
|
|
|
!
-
|
!
-
|
|
!
-
|
|
|
|
|
|
!
-
|
|
!
-
|
|
|
|
|
!
-
!
-
-
|
|
|
!
-
!
-
|
|
!
-
!
-
!
-
-
|
|
|
|
|
|
!
-
|
|
!
|
RM="/bin/rm -f"
CP="/bin/cp -f"
MV="/bin/mv -f"
DATETIME=`date +%m%d%H%M%S`
function CSSOptimize() {
find "$workPath/code/skin" -type f ! -name '*.min.css' -and -name '*.css' -print0 |
while read -r -d '' orgFilePath
do
local cnvFilePath=${orgFilePath%%.css}_$DATETIME.min.css
uglifycss "$orgFilePath" > "$cnvFilePath" &&
find "$workPath/code" -type f -print0 |
xargs -0 sed -i "s/`basename $orgFilePath .css`\.css/`basename $cnvFilePath .min.css`\.min\.css/g" &&
$RM "$orgFilePath" || { echo "[!!ERROR!!] failed to CSS optimize"; exit 1; }
done
}
function JSOptimize() {
find "$workPath/code/skin" -type f ! -name '*.min.js' -and -name '*.js' -print0 |
while read -r -d '' orgFilePath
do
local cnvFilePath=${orgFilePath%%.js}_$DATETIME.min.js
if [ $isProduct = "true" ]; then
uglifyjs "$orgFilePath" > "$cnvFilePath" || { echo "[!!ERROR!!] failed to JS optimize(minify)"; exit 1; }
else
$MV "$orgFilePath" "$cnvFilePath" || { echo "[!!ERROR!!] failed to JS optimize(mv)"; exit 1; }
fi
find "$workPath/code" -type f -print0 |
xargs -0 sed -i "s/`basename $orgFilePath .js`\.js/`basename $cnvFilePath .min.js`\.min\.js/g" ||
{ echo "[!!ERROR!!] failed to JS optimize"; exit 1; }
done
}
function UpdateTitle() {
find "$workPath/code/skin" -type f -name '*.php' -print0 |
xargs -0 sed -i "s/<\/title>/\[テスト版\]<\/title>/g" ||
{ echo "[!!ERROR!!] failed to update title"; exit 1; }
}
function PNGOptimize() {
find "$workPath/code/image" -type f ! -name '*.min.png' -and -name '*.png' -print0 |
while read -r -d '' orgFilePath
do
local cnvFilePath=${orgFilePath%%.png}_$DATETIME.min.png
optipng "$orgFilePath" &&
$MV "$orgFilePath" "$cnvFilePath" &&
find "$workPath/code" -type f -print0 |
xargs -0 sed -i "s/`basename $orgFilePath .png`\.png/`basename $cnvFilePath .min.png`\.min\.png/g" ||
{ echo "[!!ERROR!!] failed to png optimize"; exit 1; }
done
}
function JPEGOptimize() {
find "$workPath/code/image" -type f ! -name '*.min.jpg' -and -name '*.jpg' -print0 |
while read -r -d '' orgFilePath
do
local cnvFilePath=${orgFilePath%%.jpg}_$DATETIME.min.jpg
jpegoptim --strip-all "$orgFilePath" -d"$cnvFilePath" &&
find "$workPath/code" -type f -print0 |
xargs -0 sed -i "s/`basename $orgFilePath .jpg`\.jpg/`basename $cnvFilePath .min.jpg`\.min\.jpg/g" &&
$RM "$orgFilePath" || { echo "[!!ERROR!!] failed to jpg optimize"; exit 1; }
done
}
function Deploy() {
if [ $isProduct = "true" ]; then
local envName=productenv
else
local envName=testenv
fi
ssh -t $envName <<-END
cd "$deployPath/hoge" &&
$RM -r htmlinsert image lib plugin skin &&
find . -maxdepth 1 -type f -print0 | xargs -0 $RM
END
if [ $? -ne 0 ]; then echo "[!!ERROR!!] failed to deploy(pre process)"; exit 1; fi
sftp $envName <<-END
cd "$deployPath/hoge" &&
mkdir htmlinsert &&
mkdir image &&
mkdir lib &&
mkdir plugin &&
mkdir skin &&
put -r $workPath/code/* .
END
if [ $? -ne 0 ]; then echo "[!!ERROR!!] failed to deploy(file copy)"; exit 1; fi
ssh -t $envName <<-END
cd "$deployPath/hoge" &&
chmod 777 attach backup cache counter diff trackback wiki &&
chmod 755 htmlinsert image lib plugin skin &&
chmod 666 wiki/* cache/* &&
find . -type f -name '*.php' -or -name '*.png' -or -name 'index.html' -or -name '.htaccess' -print0 | xargs -0 chmod 644
find "$nginxCachePath" -type f | xargs grep -l "hoge" | xargs $RM
END
if [ $? -ne 0 ]; then echo "[!!ERROR!!] failed to deploy(post process)"; exit 1; fi
}
if [ $# -ne 4 ]; then
echo "引数が足りません。"
echo '$1 : 作業ディレクトリパス' 1>&2
echo '$2 : デプロイ先パス(絶対パス)' 1>&2
echo '$3 : Nginxキャッシュパス(絶対パス)' 1>&2
echo '$4 : 本番環境へのデプロイか(true/false)' 1>&2
exit 1
fi
workPath=$(cd $1 && pwd)
deployPath=${2%%/}
nginxCachePath=${3%%/}
isProduct=$4
echo "Start CSS optimize"
CSSOptimize || exit 1
echo "Start JS optimize"
JSOptimize || exit 1
echo "Start PNG optimize"
PNGOptimize || exit 1
echo "Start JPEG optimize"
JPEGOptimize || exit 1
if [ $isProduct != "true" ]; then
echo "Start update title"
UpdateTitle || exit 1
fi
echo "Start deploy"
Deploy || exit 1
|
検証時の環境 †