Skip to content

Commit b84c6e2

Browse files
committed
feat(build): introduce reproducible builds
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 4123450 commit b84c6e2

4 files changed

Lines changed: 93 additions & 18 deletions

File tree

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
# SPDX-License-Identifier: AGPL-3.0-or-later
33
.PHONY: updater.phar
44

5-
updater.phar: updater.php lib/*.php buildVersionFile.php
6-
php buildVersionFile.php
7-
composer dump-autoload
8-
composer run box
9-
chmod +x updater.phar
10-
rm lib/Version.php
5+
updater.phar: updater.php lib/*.php bin/compile
6+
bin/compile
117

128
clean:
139
rm updater.phar index.php

bin/compile

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
declare -r ROOT_DIRECTORY="$(readlink -f "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")"
5+
declare -r COMPOSER_BIN="${COMPOSER_BIN:-$(which composer)}"
6+
declare -r BOX_BIN="${ROOT_DIRECTORY}/vendor/bin/box"
7+
declare -ri DEBUG="${DEBUG:-0}"
8+
declare -ri ALLOW_DIRTY="${ALLOW_DIRTY:-0}"
9+
10+
if [[ ${DEBUG} -gt 1 ]]; then
11+
set -x
12+
fi
13+
14+
function debug() {
15+
if [[ ${DEBUG} -lt 1 ]]; then
16+
return
17+
fi
18+
printf "%s\n" "${*}"
19+
}
20+
21+
# Ignore changes on updater.phar
22+
git restore updater.phar
23+
24+
# Build information
25+
declare -i BUILD_TIMESTAMP
26+
BUILD_TIMESTAMP=$(git rev-list --no-commit-header -n1 --format=%ct HEAD)
27+
declare BUILD_VERSION
28+
BUILD_VERSION=$(git describe --tags)
29+
declare -i DIRTY_BUILD
30+
if [[ -z "$(git status --porcelain)" ]]; then
31+
DIRTY_BUILD=0
32+
else
33+
DIRTY_BUILD=1
34+
BUILD_VERSION="${BUILD_VERSION} dirty"
35+
fi
36+
37+
debug "Build version ${BUILD_VERSION} with timestamp ${BUILD_TIMESTAMP}"
38+
39+
# Create version file
40+
cat >lib/Version.php <<EOF
41+
<?php
42+
43+
declare(strict_types=1);
44+
45+
namespace NC\Updater;
46+
47+
class Version {
48+
function get(): string {
49+
return '${BUILD_VERSION}';
50+
}
51+
}
52+
EOF
53+
54+
# Checks
55+
if [[ ${BUILD_TIMESTAMP} -lt 1 ]]; then
56+
echo 'Could not retrieve timestamp from latest git commit'
57+
exit 1
58+
fi
59+
if [[ ${DIRTY_BUILD} -gt 0 ]]; then
60+
if [[ ${ALLOW_DIRTY} -lt 1 ]]; then
61+
echo 'Version name contains "dirty" suffix. Clean your repository or use ALLOW_DIRTY=1 to bypass.'
62+
exit 2
63+
fi
64+
echo '⚠️ Dirty build'
65+
fi
66+
67+
# Force Composer suffix
68+
debug "Use ComposerPhar_${BUILD_TIMESTAMP} suffix in composer"
69+
"${COMPOSER_BIN}" config autoloader-suffix "ComposerPhar_${BUILD_TIMESTAMP}"
70+
# Set build timestamp
71+
sed -i "s/@timestamp@/$(date '+%Y-%m-%d %H:%I:%S %Z' -d "@${BUILD_TIMESTAMP}")/" "${ROOT_DIRECTORY}/box.json"
72+
73+
# Build
74+
debug "Start build"
75+
"${BOX_BIN}" -n compile --sort-compiled-files --composer-bin="${COMPOSER_BIN}"
76+
77+
# Reset composer.json and version file
78+
debug "Reset changes"
79+
"${COMPOSER_BIN}" config autoloader-suffix --unset
80+
git restore box.json
81+
rm lib/Version.php

box.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{
2-
"directories": [
3-
"lib"
4-
],
5-
"finder": [
6-
{
7-
"name": "*.php",
8-
"exclude": [
9-
"Tests"
10-
],
11-
"in": "vendor"
12-
}
2+
"alias": "nextcloud-updater",
3+
"banner": [
4+
"Nextcloud Server Updater",
5+
"",
6+
"SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors",
7+
"SPDX-License-Identifier: AGPL-3.0-or-later"
138
],
9+
"compactors": ["KevinGH\\Box\\Compactor\\Php"],
10+
"directories": ["lib"],
11+
"force-autodiscovery": true,
1412
"main": "updater.php",
15-
"force-autodiscovery": true
13+
"timestamp": "@timestamp@"
1614
}

updater.phar

-340 KB
Binary file not shown.

0 commit comments

Comments
 (0)