-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_package.sh
executable file
·400 lines (347 loc) · 13 KB
/
build_package.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
#
# releng-build-package - builds a Debian package (to be used in CI systems)
# Copyright (C) 2020 Eugenio "g7" Paolantonio <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -e
info() {
echo "I: $@"
}
warning() {
echo "W: $@" >&2
}
error() {
echo "E: $@" >&2
exit 1
}
# Assume we are in 'CI' if running on a container. Also set IS_CONTAINER
# variable and try to obtain informations on the build from there.
if [ -z "${CI}" ] && ([ -e /.dockerenv ] || [ -e /run/.containerenv ]); then
CI="true"
IS_CONTAINER="true"
fi
[ -n "${CI}" ] || error "This script must run inside a CI environment or in an OCI container!"
# Set some defaults. These can be specified in the CI build environment
[ -n "${RELENG_TAG_PREFIX}" ] || export RELENG_TAG_PREFIX="droidian/"
[ -n "${RELENG_LEGACY_TAG_PREFIX}" ] || export RELENG_LEGACY_TAG_PREFIX="hybris-mobian/"
[ -n "${RELENG_BRANCH_PREFIX}" ] || export RELENG_BRANCH_PREFIX="feature/"
[ -n "${RELENG_FULL_BUILD}" ] || export RELENG_FULL_BUILD="no"
# Newer git releases complain about "dubious ownership"
git config --global --add safe.directory ${PWD} || true
# There are three different "build types" that match the destination
# repository
# - feature-branch: this is meant only for testing purposes, a new
# throwaway debian repository must be created by the receiver
# - staging: this comes from a push in the branch meant for production,
# but still hasn't been tagged yet
# - group: Droidian groups (production)
# - production: this comes from a push in the branch meant for production,
# and it has been also tagged.
#
# Default build type is "feature-branch", per-CI logic should determine
# which build type is by looking at available data.
BUILD_TYPE="feature-branch"
if [ "${HAS_JOSH_K_SEAL_OF_APPROVAL}" == "true" ]; then
# Travis CI
CI_CONFIG="./travis.yml"
BRANCH="${TRAVIS_BRANCH}"
COMMIT="${TRAVIS_COMMIT}"
if [ -n "${TRAVIS_TAG}" ]; then
TAG="${TRAVIS_TAG}"
# Fetch the release name from the tag, and use that as comment,
# appending the -production suffix
COMMENT=$(echo "${TAG//${RELENG_TAG_PREFIX}/}" | cut -d "/" -f1).production
BUILD_TYPE="production"
else
# Use the branch name as the comment, append -pr if it's a pull request
COMMENT="${TRAVIS_BRANCH}"
# If the branch doesn't start with feature/..., this is going to be
# a staging build
if [[ "${TRAVIS_BRANCH}" != feature/* ]]; then
BUILD_TYPE="staging"
fi
if [ "${TRAVIS_EVENT_TYPE}" == "pull_request" ]; then
COMMENT="${COMMENT}.pull.request.test"
fi
fi
elif [ "${DRONE}" == "true" ]; then
# Drone CI
CI_CONFIG="debian/drone.star"
BRANCH="${DRONE_BRANCH}"
COMMIT="${DRONE_COMMIT}"
if [ -n "${DRONE_TAG}" ]; then
TAG="${DRONE_TAG}"
# Fetch the release name from the tag, and use that as comment,
# appending the -production suffix
COMMENT=$(echo "${TAG//${RELENG_TAG_PREFIX}/}" | cut -d "/" -f1).production
BUILD_TYPE="production"
else
# Use the branch name as the comment, append -pr if it's a pull request
COMMENT="${DRONE_BRANCH}"
# If the branch doesn't start with feature/..., this is going to be
# a staging build
if [[ "${DRONE_BRANCH}" != feature/* ]]; then
BUILD_TYPE="staging"
fi
if [ -n "${DRONE_PULL_REQUEST}" ]; then
COMMENT="${COMMENT}.pull.request.test"
fi
fi
elif [ "${AZURE_PIPELINES}" == "true" ]; then
# CI and AZURE_PIPELINES must be set in the pipeline yaml, they
# are not provided by Azure!
CI_CONFIG="./debian/azure-pipelines.yml"
BRANCH="${BUILD_SOURCEBRANCH/refs\/heads\//}"
COMMIT="${BUILD_SOURCEVERSION}"
if [[ "${BUILD_SOURCEBRANCH}" == refs/tags/* ]]; then
TAG="${BUILD_SOURCEBRANCH/refs\/tags\//}"
# Fetch the release name from the tag, and use that as the branch
# name (we can't reliably get it from azure on tags) and as a comment,
# appending the -production suffix
BRANCH=$(echo "${TAG//${RELENG_TAG_PREFIX}/}" | cut -d "/" -f1)
COMMENT="${BRANCH}.production"
BUILD_TYPE="production"
else
# Use the branch name as the comment, append -pr if it's a pull request
COMMENT="${BRANCH}"
# If the branch doesn't start with feature/..., this is going to be
# a staging build
if [[ "${BRANCH}" != feature/* ]]; then
BUILD_TYPE="staging"
fi
if [[ "${BUILD_SOURCEBRANCH}" == refs/pull/* ]]; then
# Not supported yet
error "Pull Requests are not supported for now with the Azure Pipelines provider"
fi
fi
elif [ "${CIRCLECI}" == "true" ]; then
# CircleCI
CI_CONFIG=".circleci/config.yml"
BRANCH="${CIRCLE_BRANCH}"
COMMIT="${CIRCLE_SHA1}"
REMOTE="${CIRCLE_REPOSITORY_URL}"
if [ -n "${CIRCLE_TAG}" ]; then
TAG="${CIRCLE_TAG}"
# Fetch the release name from the tag, and use that as comment,
# appending the -production suffix
COMMENT=$(echo "${TAG//${RELENG_TAG_PREFIX}/}" | cut -d "/" -f1).production
BUILD_TYPE="production"
else
# Use the branch name as the comment, append -pr if it's a pull request
COMMENT="${CIRCLE_BRANCH}"
# If the branch doesn't start with feature/..., this is going to be
# a staging build
if [[ "${CIRCLE_BRANCH}" == group/* ]]; then
BUILD_TYPE="group"
elif [[ "${CIRCLE_BRANCH}" == feature/* ]]; then
BUILD_TYPE="feature-branch"
else
BUILD_TYPE="staging"
fi
if [ -n "${CIRCLE_PULL_REQUEST}" ]; then
COMMENT="${COMMENT}.pull.request.test"
fi
fi
elif [ "${IS_CONTAINER}" == "true" ]; then
# Obtain stuff from the current directory
# Note: "production" builds are not supported at the moment.
BRANCH=$(git rev-parse --abbrev-ref HEAD)
COMMIT=$(git rev-parse HEAD)
COMMENT="${BRANCH}"
REMOTE=$(git remote get-url origin)
FORCE_ALLOW_EXTRA_REPOS="yes"
# If the branch doesn't start with feature/ or group/..., this is going to be
# a staging build
if [[ "${BRANCH}" == group/* ]]; then
BUILD_TYPE="group"
elif [[ "${BRANCH}" == feature/* ]]; then
BUILD_TYPE="feature-branch"
else
BUILD_TYPE="staging"
fi
fi
# Install git-lfs if .gitattributes is present
if [ "${IS_CONTAINER}" != "true" ]; then
if [ -e .gitattributes ]; then
git lfs install
git fetch origin
git checkout origin/${BRANCH}
fi
# Always fetch tags
git fetch --tags
fi
# Build debian/changelog
info "Building changelog from git history"
if [ "${BUILD_TYPE}" == "group" ]; then
RELENG_BRANCH_PREFIX="group/"
fi
ARGS="--commit ${COMMIT} --comment ${COMMENT} --tag-prefix ${RELENG_TAG_PREFIX} ${RELENG_LEGACY_TAG_PREFIX} --branch-prefix ${RELENG_BRANCH_PREFIX}"
case "${BUILD_TYPE}" in
"production")
ARGS="${ARGS} --tag ${TAG}"
# 2024-05: Droidian switched to snapshots, and we have to handle that...
# This is only needed for bootstraps, and it might be removed at a later date
suite=$(echo "${TAG//${RELENG_TAG_PREFIX}/}" | cut -d "/" -f1)
if [ "${suite}" == "next" ]; then
ARGS="${ARGS} --rolling-release next"
fi
;;
"feature-branch")
ARGS="${ARGS} --branch ${BRANCH} --rolling-release next"
;;
"staging")
ARGS="${ARGS} --branch ${BRANCH}"
;;
esac
# NOTE: On Travis CI we're stuck to depth 50 unless we unshallow.
#git fetch --unshallow
if [ "${IS_CONTAINER}" == "true" ]; then
# Handle debian/changelog. First try restoring it from git...
git checkout -- debian/changelog &> /dev/null || \
# Otherwise, remove it altogether
rm -f debian/changelog
fi
eval releng-build-changelog "${ARGS}"
# TODO? Build arch checks?
package_info=$(head -n 1 debian/changelog)
package_name=$(echo "${package_info}" | awk '{ print $1 }')
# Add extra repositories if required
if [ -n "${EXTRA_REPOS}" ]; then
if [ "${FORCE_ALLOW_EXTRA_REPOS}" != "yes" ] && [ "${BUILD_TYPE}" != "feature-branch" ]; then
error "EXTRA_REPOS is specified but BUILD_TYPE is not 'feature-branch'. Aborting..."
fi
IFS="|"
repos=($(echo "${EXTRA_REPOS}"))
for repo in "${repos[@]}"; do
info "Enabling ${repo}"
echo "${repo}" >> /etc/apt/sources.list.d/releng-build-package-extra-repos.list
done
fi
# Enable staging repository for staging builds
if [ "${BUILD_TYPE}" == "staging" ] && [ "${IS_CONTAINER}" != "true" ]; then
info "Enabling staging repository"
apt-get --yes install droidian-apt-config-staging
fi
# Refresh APT database
info "Refreshing APT database"
apt-get update
# Install build dependencies
info "Installing build dependencies"
mk-build-deps --remove --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes" debian/control
# Remove clutter
rm -f ${package_name}-build-deps_*.*
[ -z "${CI_CONFIG}" ] || rm -f "${CI_CONFIG}"
# Handle non-native packages
current_dir="${PWD}"
non_native="no"
if [ -e "debian/source/format" ] && grep -q "quilt" debian/source/format; then
non_native="yes"
info "Package is non-native"
package_orig_version=$(echo "${package_info}" | awk '{ print $2 }' | cut -d- -f1 | sed 's/(//' | cut -d':' -f2-)
package_orig_version_tag="${package_orig_version/\~/_}"
if [ "${BUILD_TYPE}" == "production" ] && [ "${DRONE}" == "true" ]; then
# Ensure the branch gets actually downloaded...
git fetch origin "+refs/heads/${BRANCH}"
git checkout --track "origin/${BRANCH}"
fi
# git archive doesn't support submodules, which is not ideal.
# Workaround this by creating a new worktree from the upstream tag,
# fetch submodules, then create the orig file
temp_dir=$(mktemp -d)
orig_dir=${temp_dir}/source
git worktree add ${orig_dir} upstream/${package_orig_version_tag}
cd ${orig_dir}
git submodule update --init --recursive
# Limitation of this approach is that we HAVE to check out eventual
# new submodules :(
if git checkout ${BRANCH} .gitmodules; then
for submodule in $(git submodule status | awk '{ print $2 }'); do
git checkout ${BRANCH} ${submodule}
done
git submodule sync
git submodule update
fi
tar \
--exclude "./debian" \
--exclude "./.git" \
--exclude "./.gitmodules" \
--exclude "./.gitattributes" \
-cJf "${temp_dir}/${package_name}_${package_orig_version}.orig.tar.xz" .
cd "${current_dir}"
# Try to generate quilt patches
git add .
git config user.email "releng@localhost"
git config user.name "releng-build-package"
git commit -m "temporary commit"
mkdir -p debian/patches
# Entirely replace the series file with our patches, we don't support
# an hybrid quilt+git configuration
git diff upstream/${package_orig_version_tag}..${BRANCH} \
--text \
--ignore-submodules=all \
-- . ':!debian/' ':!.gitmodules' \
> debian/patches/0001-autogenerated-by-releng-build-package.patch
echo "0001-autogenerated-by-releng-build-package.patch" > debian/patches/series
# Copy the new directory to ${orig_dir} as we're going to build
# there
rm -rf ${orig_dir}/debian
cp -Rav debian ${orig_dir}/debian
# Finally enter in ${orig_dir}
cd ${orig_dir}
else
git submodule update --init --recursive --depth 1
fi
# Finally build the package
info "Building package"
ARGS="--no-lintian -d -sa --no-sign --jobs=$(nproc)"
if [ "${RELENG_FULL_BUILD}" == "yes" ]; then
# Full build, build source,any,all
ARGS="${ARGS} -F"
# Note on the -F usage: debuild crashes trying to read a not existing
# .changes files when building source packages without supplying the
# old style arguments, so here we are.
else
# Build only arch-dependent packages
ARGS="${ARGS} --build=any"
fi
# Support --host-arch (-aARCH in debuild, see DEBBUGS#898706)
if [ -n "${RELENG_HOST_ARCH}" ]; then
ARGS="${ARGS} -a${RELENG_HOST_ARCH}"
fi
eval debuild \
--set-envvar="DROIDIAN_VCS_URL=${REMOTE}" \
--set-envvar="DROIDIAN_VCS_REV=${COMMIT}" \
--prepend-path="/usr/lib/releng-tools/wrappers" \
"${ARGS}"
# Move artifacts to the correct location if this is a non-native build
if [ "${non_native}" == "yes" ]; then
info "Moving artifacts to correct location"
find ${temp_dir}/ \
-maxdepth 1 \
-type f \
-regextype posix-egrep \
-regex "${temp_dir}/.*\.(u?deb|tar\..*|dsc|buildinfo|changes)$" \
-exec mv {} ${current_dir}/.. \;
fi