-
Notifications
You must be signed in to change notification settings - Fork 14
/
ci_cd.sh
executable file
·168 lines (153 loc) · 4.79 KB
/
ci_cd.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
#!/bin/sh
# Exit immediately if a command returns a non-zero status.
set -e
# Usage of this script
program_name=$0
usage () {
echo "usage: $program_name [--android-api 29] [--build-tools "29.0.3"] [--build] [--test]"
echo " --android-api <androidVersion> Use specific Android version from \`sdkmanager --list\`"
echo " --build-tools <version> Use specific build tools version"
echo " --android-ndk Install Android NDK"
echo " --ndk-version <version> Install a specific Android NDK version from \`sdkmanager --list\`"
echo " --gcloud Install the latest GCloud SDK version"
echo " --build Build image"
echo " --test Test image"
echo " --large-test Run large tests on the image (Firebase Test Lab for example)"
echo " --deploy Deploy image"
echo " --desc Generate a .md file in /desc/ouput folder describing the builded image, on host machine"
exit 1
}
# Parameters parsing
android_ndk=false
gcloud=false
large_test=false
while true; do
case "$1" in
--android-api ) android_api="$2"; shift 2 ;;
--build-tools ) android_build_tools="$2"; shift 2 ;;
--build ) build=true; shift ;;
--test ) test=true; shift ;;
--android-ndk ) android_ndk=true; shift ;;
--large-test ) large_test=true; shift ;;
--gcloud ) gcloud=true; shift ;;
--ndk-version ) ndk_version="$2"; shift 2 ;;
--deploy ) deploy=true; shift ;;
--desc ) desc=true; shift ;;
* ) break ;;
esac
done
if [ -z "$android_api" ]; then
echo "Missing --android-api parameter"
usage
fi
if [ -z "$android_build_tools" ]; then
echo "Missing --build-tools parameter"
usage
fi
if [ $large_test = true ]; then
failed=false
if [ -z "$GCLOUD_SERVICE_KEY" ]; then
echo "GCLOUD_SERVICE_KEY environment variable is need to run large tests"
failed=true
fi
if [ -z "$FIREBASE_PROJECT_ID" ]; then
echo "FIREBASE_PROJECT_ID environment variable is need to run large tests"
failed=true
fi
if [ "$failed" = true ]; then
exit 1
fi
fi
# Compute image tag
org_name="fabernovel"
simple_image_name="api-$android_api"
if [ "$gcloud" = true ]; then
simple_image_name="$simple_image_name-gcloud"
fi
if [ "$android_ndk" = true ]; then
simple_image_name="$simple_image_name-ndk"
fi
branch="${GIT_REF##refs/heads/}"
if [ "$branch" = "develop" ]; then
simple_image_name="$simple_image_name-snapshot"
fi
if [ -n "$RELEASE_NAME" ]; then
simple_image_name="$simple_image_name-$RELEASE_NAME"
fi
full_image_name="$org_name/android:$simple_image_name"
# CI business
tasks=0
if [ "$build" = true ]; then
tasks=$((tasks+1))
if [ -n "$ndk_version" ]; then
ndk_version_build_arg="--build-arg ndk_version=$ndk_version"
fi
echo $ndk_version_build_arg
set -x
docker build \
--build-arg android_api=android-$android_api \
--build-arg android_build_tools="$android_build_tools" \
--build-arg android_ndk="$android_ndk" \
--build-arg gcloud="$gcloud" \
$ndk_version_build_arg \
--tag $full_image_name .
set +x
fi
ci=${CI:-false}
if [ "$ci" = true ]; then
echo "Running in CI"
volume_options="--volumes-from runner --workdir $GITHUB_WORKSPACE"
else
echo "Not running in CI"
volume_options="-v $PWD:/wd --workdir /wd"
fi
if [ "$test" = true ]; then
tasks=$((tasks+1))
echo "Testing image $full_image_name"
test_options="--check-base-tools --android-api $android_api --android-build-tools $android_build_tools"
if [ "$android_ndk" = true ]; then
test_options="$test_options --android-ndk"
fi
if [ "$gcloud" = true ]; then
test_options="$test_options --gcloud"
fi
if [ "$large_test" = true ]; then
echo "Large test: $FIREBASE_PROJECT_ID"
tasks=$((tasks+1))
set -x
docker run \
--env FIREBASE_PROJECT_ID="${FIREBASE_PROJECT_ID}" \
--env GCLOUD_SERVICE_KEY="${GCLOUD_SERVICE_KEY}" \
$volume_options \
--rm \
"$full_image_name" \
bash tests/run_tests.sh $test_options --large-test
set +x
else
set -x
docker run \
$volume_options \
--rm \
"$full_image_name" \
bash tests/run_tests.sh $test_options
set +x
fi
fi
if [ "$deploy" = true ]; then
tasks=$((tasks+1))
echo "Deploy image $full_image_name"
echo "$DOCKERHUB_TOKEN" | docker login --username botfabernovel --password-stdin
docker push $full_image_name
fi
if [ "$desc" = true ]; then
tasks=$((tasks+1))
echo "Generating image description $simple_image_name.md"
docker run \
$volume_options \
--rm $full_image_name \
sh desc/desc.sh | tee desc/output/$simple_image_name.md
fi
if [ "$tasks" = 0 ]; then
echo "No task was executed"
usage
fi