-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·165 lines (132 loc) · 4.37 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
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
#!/bin/bash
set -e
# Set the following environment variables:
# DEPLOY_BUCKET = your bucket name
# DEPLOY_BUCKET_PREFIX = a directory prefix within your bucket
# DEPLOY_BRANCHES = regex of branches to deploy; leave blank for all
# DEPLOY_EXTENSIONS = whitespace-separated file exentions to deploy; leave blank for "jar war zip"
# PURGE_OLDER_THAN_DAYS = Files in the .../deploy and .../pull-request prefixes in GCP older than this number of days will be deleted; leave blank for 90, 0 to disable.
if [[ -z "${DEPLOY_BUCKET}" ]]
then
echo "Bucket not specified via \$DEPLOY_BUCKET"
fi
DEPLOY_BUCKET_PREFIX=${DEPLOY_BUCKET_PREFIX:-}
DEPLOY_BRANCHES=${DEPLOY_BRANCHES:-}
DEPLOY_EXTENSIONS=${DEPLOY_EXTENSIONS:-"jar war zip"}
DEPLOY_SOURCE_DIR=${DEPLOY_SOURCE_DIR:-$log_BUILD_DIR/target}
PULL_REQUEST=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
PURGE_OLDER_THAN_DAYS=${PURGE_OLDER_THAN_DAYS:-"90"}
if [[ ! -z "$PULL_REQUEST" && "$PULL_REQUEST" != "" && "$PULL_REQUEST" != "null" ]]
then
target_path=pull-request/$PULL_REQUEST
elif [[ -z "$DEPLOY_BRANCHES" || "$BRANCH" =~ "$DEPLOY_BRANCHES" ]]
then
echo "Deploying branch ${GITHUB_REF##*/}"
BUILD_NUM=${GITHUB_RUN_NUMBER}
if ! [[ -z "${BUILD_NUM_OFFSET}" ]]
then
BUILD_NUM=$((GITHUB_RUN_NUMBER+BUILD_NUM_OFFSET))
fi
target_path=deploy/${GITHUB_REF##*/}/$BUILD_NUM
else
echo "Not deploying."
exit
fi
# BEGIN fold/timer support
echo "OpenSSL"
openssl version
openssl des3 -d -in .github/gcp-deploy.json.des3 -out .github/gcp-deploy.json -pass pass:$DEPOLOY_CREDENTIALS -md sha512
gcloud auth activate-service-account --key-file=.github/gcp-deploy.json
activity=""
timer_id=""
start_time=""
log_start() {
if [[ -n "$activity" ]]
then
echo "Nested log_start is not supported!"
return
fi
activity="$1"
timer_id=$RANDOM
start_time=$(date +%s%N)
start_time=${start_time/N/000000000} # in case %N isn't supported
echo "log_fold:start:$activity"
echo "log_time:start:$timer_id"
}
log_end() {
if [[ -z "$activity" ]]
then
echo "Can't log_end without log_start!"
return
fi
end_time=$(date +%s%N)
end_time=${end_time/N/000000000} # in case %N isn't supported
duration=$(expr $end_time - $start_time)
echo "log_time:end:$timer_id:start=$start_time,finish=$end_time,duration=$duration"
echo "log_fold:end:$activity"
# reset
activity=""
timer_id=""
start_time=""
}
# END fold/timer support
discovered_files=""
for ext in ${DEPLOY_EXTENSIONS}
do
discovered_files+=" $(ls $DEPLOY_SOURCE_DIR/*.${ext} 2>/dev/null || true)"
done
files=${DEPLOY_FILES:-$discovered_files}
if [[ -z "${files// }" ]]
then
echo "Files not found; not deploying."
exit
fi
target=builds/${DEPLOY_BUCKET_PREFIX}${DEPLOY_BUCKET_PREFIX:+/}$target_path/
log_start "gcp_rm"
gsutil ls gs://$DEPLOY_BUCKET/$target | \
while read -r line
do
if [[ $line != CommandException* ]] && [[ $line != "" ]]; then
echo "Deleting existing artifact [$line]."
gsutil rm $line
fi
done
log_end
log_start "gcp_cp"
for file in $files
do
echo "Uploading ${file} to gs://${DEPLOY_BUCKET}/${target}"
gsutil cp $file gs://$DEPLOY_BUCKET/$target
done
log_end
if [[ $PURGE_OLDER_THAN_DAYS -ge 1 ]]
then
log_start "clean_gcp"
echo "Cleaning up builds in GS older than $PURGE_OLDER_THAN_DAYS days . . ."
cleanup_prefix=builds/${DEPLOY_BUCKET_PREFIX}${DEPLOY_BUCKET_PREFIX:+/}
# TODO: this works with GNU date only
older_than_ts=`date -d"-${PURGE_OLDER_THAN_DAYS} days" +%s`
for suffix in deploy pull-request
do
gsutil ls -l gs://$DEPLOY_BUCKET/$cleanup_prefix$suffix/ | \
while read -r line
do
last_modified=`echo "$line" | awk -F'[[:space:]][[:space:]]' '{print $4}'`
if [[ -z $last_modified ]]
then
continue
fi
last_modified_ts=`date -d"$last_modified" +%s`
filename=`echo "$line" | awk -F'\t' '{print $3}'`
if [[ $last_modified_ts -lt $older_than_ts ]]
then
if [[ $filename != "" ]]
then
echo "gs://$DEPLOY_BUCKET/$filename is older than $PURGE_OLDER_THAN_DAYS days ($last_modified). Deleting."
gsputil rm "gs://$DEPLOY_BUCKET/$filename"
fi
fi
done
done
log_end
fi