Skip to content

Commit db936c0

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

4 files changed

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

-338 KB
Binary file not shown.

0 commit comments

Comments
 (0)