-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_buildstep.sh
executable file
·164 lines (139 loc) · 3.86 KB
/
run_buildstep.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
#!/usr/bin/env bash
#
# Based on https://gist.github.com/epiloque/8cf512c6d64641bde388
step=$1
srcdir=$CODEBUILD_SRC_DIR
if [ -z "$WORKDIR" ]; then
WORKDIR="/codebuild/output"
fi
pushd `dirname $0` > /dev/null
scriptpath=`pwd`
popd > /dev/null
export PATH="$PATH:$scriptpath"
parse_yaml() {
local prefix=$2
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_]*'
fs="$(echo @|tr @ '\034')"
sed -e 's/"/\\"/g' \
-ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
}
}' | sed 's/_=/+=/g'
}
cleanup() {
rv=$?
if [[ "$rv" -eq 2 ]]; then
echo "Version matching exit code"
touch "$srcdir/VERSION_MATCHING"
exit 0
else
exit $rv
fi
}
trap "cleanup" EXIT
run_cmd() {
if eval "$@"; then
echo "Success"
return 0
else
retval="$?"
echo "Failure with code $retval"
return "$retval"
fi
}
GIT_STATUS=$(cd $srcdir; git describe --abbrev=0 --tags)
testversion() {
testversion_with_exit '' $@
}
testversion_skip_exit() {
testversion_with_exit '1' $@
}
testversion_with_exit() {
noexit=$1
shift
filename=$1
shift
echo "$GIT_STATUS"
checkversion --s3-version-suffix="-${GIT_STATUS}" --fail-on-match --print-remote --s3path "s3:::${BUILD_OUTPUT_BUCKET}/${BUILD_OUTPUT_PREFIX}/${filename}" "$@" > $srcdir/target_version.txt
if [ $? -gt 0 ]; then
if [ -z $noexit ]; then
touch "$srcdir/VERSION_MATCHING"
echo "No need to run build" && exit 0
fi
false
else
echo "Running build updating target version to"
cat "$srcdir/target_version.txt"
if [ -z $noexit ]; then
exit 0
fi
true
fi
}
buildspec="$srcdir/.buildspec.yml"
if [ -e "$srcdir/target_version.txt" ]; then
TARGETVERSION=$(<"$srcdir/target_version.txt")
UPLOADVERSION="${TARGETVERSION}-${GIT_STATUS}"
fi
export -f testversion
export -f testversion_skip_exit
export -f testversion_with_exit
export srcdir
export TARGETVERSION
export UPLOADVERSION
export GIT_STATUS
export WORKDIR
echo "Source dir is $srcdir"
if [[ -f "$srcdir/VERSION_MATCHING" ]]; then
echo "Skipping build step because of matching version"
exit 0
fi
if [ -e "$srcdir/buildspec.yml" ]; then
buildspec="$srcdir/buildspec.yml"
fi
yml_values=$(parse_yaml $buildspec "buildspec_")
eval $yml_values
IFS=""
install_command_variable="buildspec_phases_${step}_commands"
eval install_commands=(\${$install_command_variable[@]})
for cmd in "${install_commands[@]}"
do
echo "Executing $step $cmd"
run_cmd "$cmd"
retvalue="$?"
if [ "$retvalue" -gt 0 ]; then
if [[ ! -f "$srcdir/VERSION_MATCHING" && "$retvalue" -ne 2 ]]; then
touch "$srcdir/FAILED"
exit "$retvalue"
fi
fi
done
# pre_build
if [[ "$step" == "post_build" && ! -z "$BUILD_OUTPUT_BUCKET" && ! -f "$srcdir/FAILED" && ! -f "$srcdir/VERSION_MATCHING" ]]; then
echo "Doing custom uploads"
for file in "${buildspec_artifacts_files[@]}"
do
echo "Uploading $file with version $UPLOADVERSION"
if [ -d "$file" ]; then
aws s3 sync --metadata "version=$UPLOADVERSION" "${file}/" "s3://${BUILD_OUTPUT_BUCKET}/${BUILD_OUTPUT_PREFIX}/"
else
aws s3 cp --metadata "version=$UPLOADVERSION" "${file}" "s3://${BUILD_OUTPUT_BUCKET}/${BUILD_OUTPUT_PREFIX}/${file}"
fi
if [ $? -gt 0 ]; then
echo "Upload failure" && exit 1
else
echo "Upload success"
fi
done
fi