-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·262 lines (209 loc) · 6.27 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
set -e
# -----------------------------------------------------------------------------
# Colors
ALL_OFF="\e[0m"
BOLD="\e[1m"
BLUE="${BOLD}\e[34m"
GREEN="${BOLD}\e[32m"
PURPLE="${BOLD}\e[35m"
RED="${BOLD}\e[31m"
readonly ALL_OFF BOLD BLUE GREEN PURPLE RED
msg() {
local mesg=$1; shift
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
msg2() {
local mesg=$1; shift
printf "${BLUE} =>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
msg3() {
local mesg=$1; shift
printf "${PURPLE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
error() {
local mesg=$1; shift
printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
# -----------------------------------------------------------------------------
# Dependency check
is_installed() {
FOUND=0
for dep in "$@"; do
if command -v "$dep" >/dev/null 2>/dev/null; then
FOUND=1
break
fi
done
if [[ $FOUND -eq 0 ]]; then
error "Could not find (at least one of) the following dependencies" "$@" >&2
return 1
fi
return 0
}
installed_or_exit() {
if ! is_installed "$@"; then
exit 1
fi
}
# -----------------------------------------------------------------------------
prepare() {
msg2 "Preparing build image"
msg3 "Installing base development packages"
echo "CacheDir=/pacmanCache" >> /etc/pacman.conf
echo "ParallelDownloads = ${PACMAN_PARALLEL_DOWNLOADS:-1}" >> /etc/pacman.conf
pacman -Suy --noconfirm base-devel sudo devtools git rsync >/dev/null
msg3 "Creating build user and build directories"
useradd ci -M
echo "ci ALL= NOPASSWD: /usr/bin/pacman" >> /etc/sudoers
mkdir -p /home/ci/{,pkgdest,build,srcdest,srcpkgdest,aurdir,remote}
chown -R ci:ci /home/ci
chmod -R 777 /home/ci
msg3 "Initializing gpg key"
if [ -z ${GPG_KEY+x} ]; then
error "GPG_KEY not set"
exit 1
fi
echo "${GPG_KEY}" | sed 's@\\n@\n@g' > /tmp/sign.key
chmod 444 /tmp/sign.key
sudo -u ci gpg --import /tmp/sign.key >/dev/null 2>/dev/null
gpg --import --import-options show-only /tmp/sign.key 2>/dev/null | head -n 2 | tail -n 1 | tr -d ' ' > /home/ci/gpgkey
rm /tmp/sign.key
msg3 "Setting makepkg.conf env variables"
cat >> /etc/makepkg.conf << EOF
PACKAGER=\"${PACKAGER}\"
PKGDEST=/home/ci/pkgdest
LOGDEST=/home/ci/pkgdest
BUILDDIR=/home/ci/build
SRCDEST=/home/ci/srcdest
SRCPKGDEST=/home/ci/srcpkgdest
MAKEFLAGS=\"-j$(nproc)\"
CARCH=x86_64
BUILDTOOL=build.sh
BUILDTOOLVER=0.1
BUILDENV=(!distcc !color !ccache check sign)
GPGKEY=$(cat /home/ci/gpgkey)
EOF
msg3 "Setting git"
git config --global --add safe.directory /build
git config --global --add safe.directory /home/ci/aurdir
}
finish() {
msg2 "Copying package and logs to output directory"
mv /home/ci/pkgdest/* /output
}
build() {
local pkgdir="$1"
msg2 "Build start"
pushd "$pkgdir" > /dev/null || exit
if [ -z ${SOURCE_DATE_EPOCH+x} ]; then
local SOURCE_DATE_EPOCH
SOURCE_DATE_EPOCH="$(git log -1 --format=%ct .)"
fi
local LC_ALL=C
if ! sudo -u ci --preserve-env=SOURCE_DATE_EPOCH,LC_ALL makepkg -s --noprogressbar --noconfirm -L >/dev/null 2>/dev/null; then
error "Build failed"
finish
exit 1
fi
finish
popd >/dev/null || exit
}
package() {
msg "Building custom package ${PKGNAME}"
prepare
build "/pkgdir"
}
aur() {
local pkgname="$1"
msg "Building AUR package ${PKGNAME}"
prepare
msg2 "Cloning AUR repository for package ${PKGNAME}"
if ! sudo -u ci git clone -q -- "https://aur.archlinux.org/${pkgname}.git" /home/ci/aurdir; then
error "Unable to clone AUR git repository"
exit 1
fi
build "/home/ci/aurdir"
}
run_docker() {
local cmd="$1"
shift
local docker_args=("$@")
# ulimit: https://www.mail-archive.com/[email protected]/msg1892339.html
set -x
pwd
ls -la
docker run --rm \
--ulimit nofile=1024:524288 \
-v "$(pwd)":/app:ro \
-v "$(pwd)"/output:/output \
-v "$(pwd)"/cache:/pacmanCache \
--env-file=.env \
${docker_args[@]} \
archlinux \
bash -c "ls -la /app && /app/build.sh ${cmd}"
set +x
}
repo_prepare() {
msg "Creating repo '${REPONAME}' from packages in output directory"
prepare
pushd /output >/dev/null || exit
msg2 "Running repo-add"
if ! sudo -u ci repo-add --nocolor --sign --key "$(cat /home/ci/gpgkey)" "${REPONAME}.db.tar.xz" ./*.pkg.tar.zst >/dev/null; then
error "Creating repository failed with return code $?"
exit 1
fi
popd >/dev/null || exit
}
repo_publish() {
msg "Publishing repo '${REPONAME}'"
prepare
installed_or_exit "rsync"
msg2 "Setting up SSH client"
mkdir -p /root/.ssh
echo "${SSH_KNOWN_HOSTS}" > /root/.ssh/known_hosts
echo "${SSH_PRIVATE_KEY}" | sed 's@\\n@\n@g' > /root/.ssh/id_ed25519
chmod 400 /root/.ssh/id_ed25519
msg2 "Copying repository using rsync"
if ! rsync -rlptO /output/ "${SSH_REMOTE}:${SSH_REMOTE_DEST}/archlinux/${REPONAME}/x86_64"; then
error "rsync failed with return code $?"
rm /root/.ssh/id_ed25519
exit 1
fi
rm /root/.ssh/id_ed25519
}
main() {
installed_or_exit "docker"
# rm -fr output/
mkdir -p output/
mkdir -p cache/
msg "Pulling docker image"
docker pull -q archlinux
find packages -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' pkgdir
do
local pkgname
pkgname="$(basename "${pkgdir}")"
run_docker "package" \
"-v $(pwd)/packages/${pkgname}:/pkgdir:ro" \
"-e PKGNAME=${pkgname}" \
"-e SOURCE_DATE_EPOCH=$(git log -1 --format=%ct "packages/${pkgname}")"
done
while read -r pkgname; do
run_docker "aur ${pkgname}" "-e PKGNAME=${pkgname}"
done < packages/aur.txt
run_docker "repo-prepare"
run_docker "repo-publish"
}
if [[ "$#" == 0 ]]; then
main
elif [[ "$#" == 1 ]]; then
case "$1" in
package) package ;;
repo-prepare) repo_prepare ;;
repo-publish) repo_publish ;;
*) echo "Invalid option"; exit 1 ;;
esac
elif [[ "$#" == 2 && "$1" == "aur" ]]; then
aur "$2"
fi
exit 0