-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·132 lines (117 loc) · 4.06 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
#!/usr/bin/env bash
set +e
read -r -d '' USAGE <<- EOM
\n
Usage: ./build.sh [OPTIONS]\n
\n
Options:\n
--build-tools-version=VERSION \t Android build tools version. Default: 29.0.3\n
--platform-version \t\t Android platform version. Default same as the build tools version.\n
-l, --latest \t\t\t Flag build to latest\n
-d, --dry-run \t\t Push to a local repository. (always flag build to latest). Without push the image to the docker hub.\
-p, --push \t\t Push repository otherwise skips
-h, --help \t\t\t Print usage description\n
EOM
function trim() {
while read -r line; do
echo "$line"
done
}
function findPlatformVersion() {
local -r platformVersion="$1"
local -r findPlatform=$(sdkmanager --list | grep "platform;android-${platformVersion}")
if [ "$findPlatform" == "" ]; then
"$ANDROID_HOME"/cmdline-tools/latest/bin/sdkmanager --list | grep 'platforms;android-[^0-9]' | grep -v Sandbox | head -n 1 | cut -d'|' -f1 | cut -d'-' -f2 | trim
else
echo "$platformVersion"
fi
}
IMAGE_NAME=android-build-tools
IMAGE=warnyul/"$IMAGE_NAME"
# Parameters
PUSH_TO_LOCAL=false
PUSH=false
LATEST=false
BUILD_TOOLS_VERSION="34.0.0"
PLATFORM_VERSION=$(echo "$BUILD_TOOLS_VERSION" | cut -d'.' -f1)
COMMAND_LINE_TOOLS_VERSION=11076708
BASE_IMAGE="ubuntu:jammy"
while [ $# -gt 0 ]; do
case "$1" in
--base-image=*)
BASE_IMAGE="${1#*=}"
if [ -z $BASE_IMAGE ]; then
echo -e "\n --base-image is required.\n See './build.sh --help'.\n"
echo -e "$USAGE"
exit 1
fi
;;
--build-tools-version=*)
BUILD_TOOLS_VERSION="${1#*=}"
if [ -z $BUILD_TOOLS_VERSION ]; then
echo -e "\n --build-tools-version is required.\n See './build.sh --help'.\n"
echo -e "$USAGE"
exit 1
fi
PLATFORM_VERSION=$(echo "$BUILD_TOOLS_VERSION" | cut -d'.' -f1)
;;
--command-line-tools-version=*)
COMMAND_LINE_TOOLS_VERSION="${1#*=}"
if [ -z $COMMAND_LINE_TOOLS_VERSION ]; then
echo -e "\n --command-line-tools-version is required.\n See './build.sh --help'.\n"
echo -e "$USAGE"
exit 1
fi
;;
--platform-version=*)
PLATFORM_VERSION="${1#*=}"
if [ -z $PLATFORM_VERSION ]; then
echo -e "\n --platform-version is required.\n See './build.sh --help'.\n"
echo -e "$USAGE"
exit 1
fi
;;
-l|--latest)
LATEST=true
;;
-d|--dry-run)
LATEST=true
PUSH_TO_LOCAL=true
IMAGE=localhost:5000/"$IMAGE_NAME"
;;
-p|--push)
PUSH=true
;;
-h|--help|*)
echo -e "\n Unknown argument: '$1'.\n See './build.sh --help'.\n"
echo -e "$USAGE"
exit 1
;;
esac
shift
done
PLATFORM_VERSION=$(findPlatformVersion "$PLATFORM_VERSION")
UBUNTU_DISTRIBUTION=$(echo "$BASE_IMAGE" | grep -Eo 'ubuntu:(.*)' | cut -d':' -f2)
OPEN_JDK_VERSION=$(grep -Eo 'openjdk-([[:digit:]]+)' Dockerfile | cut -d'-' -f2)
IMAGE_VERSION="${BUILD_TOOLS_VERSION}-${UBUNTU_DISTRIBUTION}-openjdk${OPEN_JDK_VERSION}"
docker pull "$BASE_IMAGE"
docker tag "$BASE_IMAGE" base-image
# Build
docker build \
--build-arg "ANDROID_COMMAND_LINE_TOOLS_VERSION=$COMMAND_LINE_TOOLS_VERSION" \
--build-arg "ANDROID_BUILD_TOOLS_VERSION=$BUILD_TOOLS_VERSION" \
--build-arg "ANDROID_PLATFORM_VERSION=$PLATFORM_VERSION" \
-t "$IMAGE":"$IMAGE_VERSION" .
# Start a local registry if necessary
if [ "$PUSH_TO_LOCAL" == "true" ] && [ "$PUSH" == "true" ]; then
docker run -d -p 5000:5000 --restart=always --name registry registry 2>&1
fi
# Tag as latest if necessary
if [ "$LATEST" == "true" ]; then
docker tag "$IMAGE":"$IMAGE_VERSION" "$IMAGE":latest
fi
# Push the image
if [ "$PUSH" == "true" ]; then
echo push "$IMAGE":"$IMAGE_VERSION"
docker image push --all-tags "$IMAGE"
fi