This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·231 lines (196 loc) · 6.04 KB
/
build.sh
File metadata and controls
executable file
·231 lines (196 loc) · 6.04 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~
# set colours and echo templates
# ~~~~~~~~~~~~~~~~~~~~~~~
red='\033[1;31m' # red
green='\033[1;32m' # green
bold='\033[1;37m' # bold white
nc='\033[0m' # no colour
function echo_error() {
echo -e "${red}>>> ERROR: ${bold}$1${nc}"
}
function echo_notice() {
echo -e "${green}>>> ${bold}$1${nc}"
}
function echo_bold() {
echo -e "${bold}$1${nc}"
}
# ~~~~~~~~~~~~~~~~~~~~~~~
# get parameters from user
# ~~~~~~~~~~~~~~~~~~~~~~~
helpFunction() {
echo ""
echo_bold "Usage: $0 -v <version> -a <arch> -k <key> -i <input> -o <output>"
echo_bold "\t-v Version: Alpine version to use (Optional)"
echo_bold "\t-a Architecture: Build architecture (Optional)"
echo_bold "\t-k Key: Full path to your private signing key"
echo_bold "\t-i Input: Path to the directory containing the APKBUILD file"
echo_bold "\t-o Output: Path to the output directory"
echo_bold "\t-t Testing: Add the alpine testing repository (Optional)"
exit 1
}
while getopts ":v:a:k:i:o:t" opt; do
case "${opt}" in
v) VERSION="${OPTARG}" ;;
a) ARCH="${OPTARG}" ;;
k) KEY="${OPTARG}" ;;
i) INPUT="${OPTARG}" ;;
o) OUTPUT="${OPTARG}" ;;
t) TESTING="true" ;;
?) helpFunction ;;
esac
done
clear
if [ -z "${VERSION}" ]; then
echo_notice "Defaulting version tag to :latest as it is not specified"
VERSION="latest"
fi
if [ -z "${ARCH}" ]; then
echo_notice "Defaulting build architecture to $(arch) as it is not specified"
ARCH="os"
fi
# print helpFunction in case parameters are empty
if [ -z "${ARCH}" ] || [ -z "${INPUT}" ] || [ -z "${OUTPUT}" ]; then
echo_error "Some or all of the parameters are empty"
helpFunction
fi
if [ "${TESTING}" = "true" ]; then
echo_notice "Testing repository enabled"
args="-e testing=true"
fi
# ~~~~~~~~~~~~~~~~~~~~~~~
# validate supplied parameters
# ~~~~~~~~~~~~~~~~~~~~~~~
if ! { [ "${ARCH}" = "amd64" ] || [ "${ARCH}" = "arm/v6" ] || [ "${ARCH}" = "arm/v7" ] || [ "${ARCH}" = "arm64" ] || [ "${ARCH}" = "386" ] || [ "${ARCH}" = "ppc64le" ] || [ "${ARCH}" = "s390x" ] || [ "${ARCH}" = "os" ]; }; then
echo_error "${ARCH} is not a supported architecture"
echo_bold "Supported architectures: amd64, arm/v6, arm/v7, arm64, 386, ppc64le and s390x"
exit 1
fi
if ! { [ "${VERSION}" = "latest" ] || [ "${VERSION}" = "edge" ] || [ "${VERSION}" = "dev" ] || [ "${VERSION}" = "3.14" ]; }; then
echo_error "${VERSION} is not a supported version"
echo_bold "Supported architectures: latest, edge, 3.14"
exit 1
fi
if [ ${VERSION} == "dev" ]; then
if [ ! "${ARCH}" == "os" ]; then
echo_error "Architecture cannot be specified when using dev version"
exit 1
fi
fi
# ~~~~~~~~~~~~~~~~~~~~~~~
# validate supplied folder/file locations
# ~~~~~~~~~~~~~~~~~~~~~~~
if [ -n "${KEY}" ]; then
if [ -f "${KEY}" ]; then
args="${args} -v ${KEY}:/config/key.rsa"
else
echo_error "${KEY} is not a valid file"
exit 1
fi
else
echo_notice "No private key supplied, a new signing key pair will be generated in ${OUTPUT}/apk-packager/keys for you to use"
fi
if [ -d "${INPUT}" ]; then
INPUT=${INPUT//APKBUILD/}
else
echo_error "${INPUT} is not a valid folder"
exit 1
fi
if [ ! -d "${OUTPUT}" ]; then
echo_error "${OUTPUT} is not a valid folder"
exit 1
fi
#~~~~~~~~~~~~~~~~~~~~~~~
# check deps and arch support
#~~~~~~~~~~~~~~~~~~~~~~~
if ! command -v docker &>/dev/null; then
docker="false"
fi
if ! command -v jq &>/dev/null; then
jq="false"
fi
ls=$(docker buildx ls) || buildx="false"
# error out if jq, docker or docker buildx is needed but not installed/working
if [ ! ${ARCH} = "os" ]; then
if [ "${docker}" = "false" ] || [ "${jq}" = "false" ] || [ "${buildx}" = "false" ]; then
[[ "${docker}" = "false" ]] &&
echo_error "Docker is not installed"
[[ "${buildx}" = "false" ]] &&
echo_error "Docker buildx is not installed"
[[ "${jq}" = "false" ]] &&
echo_error "Jq is not installed"
exit 1
fi
# jq and buildx are not required when docker decideds architecture
elif [ "${jq}" = "false" ] || [ "${buildx}" = "false" ]; then
[[ "${buildx}" = "false" ]] &&
echo_notice "Docker buildx is not installed, but is not needed"
[[ "${jq}" = "false" ]] &&
echo_notice "Jq is not installed, but is not needed"
fi
# check if docker is running
if ! docker info >/dev/null 2>&1; then
echo_error "Cannot connect to the docker daemon. Is the docker daemon running?"
exit 1
fi
# check if os supports buildx emulation for specified build os
if [ ! ${ARCH} = "os" ]; then
if ! echo "$ls" | grep -o "linux/${ARCH}" | sed -n 1p | grep -q "linux/${ARCH}"; then
echo_error "Your system does not support ${ARCH} emulation"
echo_bold "It is possible QEMU is not installed, try install qemu-user-static"
exit 1
fi
fi
# ~~~~~~~~~~~~~~~~~~~~~~~
# set build function
# ~~~~~~~~~~~~~~~~~~~~~~~
function build() {
echo ""
echo_notice "Pulling hydaz/apk-packager:${VERSION} (${ARCH})"
docker pull "${repo}"
echo_notice "Packaging... This may take a long time"
docker run -it --rm \
-v "${INPUT}":/config/apk-build \
-v "${OUTPUT}":/out \
${args} \
"${repo}"
}
# ~~~~~~~~~~~~~~~~~~~~~~~
# set architecture
# not my best work, i have no idea how to use jq
# ~~~~~~~~~~~~~~~~~~~~~~~
if [ ! ${ARCH} = "os" ]; then
MANIFEST="$(docker buildx imagetools inspect hydaz/apk-packager:${VERSION} --raw)" # 'cache' manifest
if [[ ${ARCH} = "amd64" ]]; then
select="0"
elif [[ ${ARCH} = "arm/v6" ]]; then
select="1"
elif [[ ${ARCH} = "arm/v7" ]]; then
select="2"
elif [[ ${ARCH} = "arm64" ]]; then
select="3"
elif [[ ${ARCH} = "386" ]]; then
select="4"
elif [[ ${ARCH} = "ppc64le" ]]; then
select="5"
elif [[ ${ARCH} = "s390x" ]]; then
select="6"
fi
repo="docker.io/hydaz/apk-packager:${VERSION}@$(echo "${MANIFEST}" | jq '.manifests['${select}'] .digest' | sed 's/"//g')"
else
repo="hydaz/apk-packager:${VERSION}"
ARCH=$(arch)
fi
# ~~~~~~~~~~~~~~~~~~~~~~~
# finally build
# ~~~~~~~~~~~~~~~~~~~~~~~
if build; then
echo ""
echo_notice "Yipee! Your package built successfully, files have been saved to ${OUTPUT}/apk-packager"
exit 0
else
code=$?
echo ""
echo_error "Uh-oh! Something went wrong building your package, check above for possible errors"
exit $code
fi