Pull request: all: switch to SOURCE_DATE_EPOCH for source date

Closes #4221.

Squashed commit of the following:

commit c84a5699280cf4c0b1c2ed034a44f05ffc74d30d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Feb 1 21:13:30 2022 +0300

    all: switch to SOURCE_DATE_EPOCH for source date
This commit is contained in:
Ainar Garipov 2022-02-01 21:44:01 +03:00
parent 9146df5493
commit 0ee34534c6
6 changed files with 52 additions and 27 deletions

View File

@ -21,6 +21,11 @@ and this project adheres to
### Changed ### Changed
- Instead of adding the build time information, the build scripts now use the
standardized environment variable [`SOURCE_DATE_EPOCH`][repr] to add the date
of the commit from which the binary was built ([#4221]). This should simplify
reproducible builds for package maintainers and those who compile their own
AdGuard Home.
- The setting `local_domain_name` is now in the `dhcp` block in the - The setting `local_domain_name` is now in the `dhcp` block in the
configuration file to avoid confusion ([#3367]). configuration file to avoid confusion ([#3367]).
- The `dns.bogus_nxdomain` configuration file parameter now supports CIDR - The `dns.bogus_nxdomain` configuration file parameter now supports CIDR
@ -74,6 +79,9 @@ In this release, the schema version has changed from 12 to 13.
[#2993]: https://github.com/AdguardTeam/AdGuardHome/issues/2993 [#2993]: https://github.com/AdguardTeam/AdGuardHome/issues/2993
[#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057 [#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057
[#3367]: https://github.com/AdguardTeam/AdGuardHome/issues/3367 [#3367]: https://github.com/AdguardTeam/AdGuardHome/issues/3367
[#4221]: https://github.com/AdguardTeam/AdGuardHome/issues/4221
[repr]: https://reproducible-builds.org/docs/source-date-epoch/

View File

@ -7,6 +7,7 @@ import (
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/AdguardTeam/golibs/stringutil" "github.com/AdguardTeam/golibs/stringutil"
) )
@ -26,11 +27,11 @@ const (
// TODO(a.garipov): Find out if we can get GOARM and GOMIPS values the same way // TODO(a.garipov): Find out if we can get GOARM and GOMIPS values the same way
// we can GOARCH and GOOS. // we can GOARCH and GOOS.
var ( var (
channel string = ChannelDevelopment channel string = ChannelDevelopment
goarm string goarm string
gomips string gomips string
version string version string
buildtime string committime string
) )
// Channel returns the current AdGuard Home release channel. // Channel returns the current AdGuard Home release channel.
@ -106,7 +107,7 @@ const (
vFmtVerHdr = "Version: " vFmtVerHdr = "Version: "
vFmtChanHdr = "Channel: " vFmtChanHdr = "Channel: "
vFmtGoHdr = "Go version: " vFmtGoHdr = "Go version: "
vFmtTimeHdr = "Build time: " vFmtTimeHdr = "Commit time: "
vFmtRaceHdr = "Race: " vFmtRaceHdr = "Race: "
vFmtGOOSHdr = "GOOS: " + runtime.GOOS vFmtGOOSHdr = "GOOS: " + runtime.GOOS
vFmtGOARCHHdr = "GOARCH: " + runtime.GOARCH vFmtGOARCHHdr = "GOARCH: " + runtime.GOARCH
@ -148,15 +149,23 @@ func Verbose() (v string) {
vFmtGoHdr, vFmtGoHdr,
runtime.Version(), runtime.Version(),
) )
if buildtime != "" {
stringutil.WriteToBuilder(b, nl, vFmtTimeHdr, buildtime) if committime != "" {
commitTimeUnix, err := strconv.ParseInt(committime, 10, 64)
if err != nil {
stringutil.WriteToBuilder(b, nl, vFmtTimeHdr, fmt.Sprintf("parse error: %s", err))
} else {
stringutil.WriteToBuilder(b, nl, vFmtTimeHdr, time.Unix(commitTimeUnix, 0).String())
}
} }
stringutil.WriteToBuilder(b, nl, vFmtGOOSHdr, nl, vFmtGOARCHHdr) stringutil.WriteToBuilder(b, nl, vFmtGOOSHdr, nl, vFmtGOARCHHdr)
if goarm != "" { if goarm != "" {
stringutil.WriteToBuilder(b, nl, vFmtGOARMHdr, "v", goarm) stringutil.WriteToBuilder(b, nl, vFmtGOARMHdr, "v", goarm)
} else if gomips != "" { } else if gomips != "" {
stringutil.WriteToBuilder(b, nl, vFmtGOMIPSHdr, gomips) stringutil.WriteToBuilder(b, nl, vFmtGOMIPSHdr, gomips)
} }
stringutil.WriteToBuilder(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace)) stringutil.WriteToBuilder(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace))
info, ok := debug.ReadBuildInfo() info, ok := debug.ReadBuildInfo()

View File

@ -90,14 +90,15 @@ Required environment:
### `go-build.sh`: Build The Backend ### `go-build.sh`: Build The Backend
Optional environment: Optional environment:
* `BUILD_TIME`: If set, overrides the build time information. Useful for
reproducible builds.
* `GOARM`: ARM processor options for the Go compiler. * `GOARM`: ARM processor options for the Go compiler.
* `GOMIPS`: ARM processor options for the Go compiler. * `GOMIPS`: ARM processor options for the Go compiler.
* `GO`: set an alternative name for the Go compiler. * `GO`: set an alternative name for the Go compiler.
* `OUT`: output binary name. * `OUT`: output binary name.
* `PARALLELISM`: set the maximum number of concurrently run build commands * `PARALLELISM`: set the maximum number of concurrently run build commands
(that is, compiler, linker, etc.). (that is, compiler, linker, etc.).
* `SOURCE_DATE_EPOCH`: the [standardized][repr] environment variable for the
Unix epoch time of the latest commit in the repository. If set, overrides
the default obtained from Git. Useful for reproducible builds.
* `VERBOSE`: verbosity level. `1` shows every command that is run and every * `VERBOSE`: verbosity level. `1` shows every command that is run and every
Go package that is processed. `2` also shows subcommands and environment. Go package that is processed. `2` also shows subcommands and environment.
The default value is `0`, don't be verbose. The default value is `0`, don't be verbose.
@ -107,6 +108,8 @@ Optional environment:
Required environment: Required environment:
* `CHANNEL`: release channel, see above. * `CHANNEL`: release channel, see above.
[repr]: https://reproducible-builds.org/docs/source-date-epoch/
### `go-deps.sh`: Install Backend Dependencies ### `go-deps.sh`: Install Backend Dependencies

View File

@ -5,22 +5,26 @@ FROM alpine:3.13
ARG BUILD_DATE ARG BUILD_DATE
ARG VERSION ARG VERSION
ARG VCS_REF ARG VCS_REF
LABEL maintainer="AdGuard Team <devteam@adguard.com>" \
org.opencontainers.image.created=$BUILD_DATE \ LABEL\
org.opencontainers.image.url="https://adguard.com/adguard-home.html" \ maintainer="AdGuard Team <devteam@adguard.com>" \
org.opencontainers.image.source="https://github.com/AdguardTeam/AdGuardHome" \ org.opencontainers.image.authors="AdGuard Team <devteam@adguard.com>" \
org.opencontainers.image.version=$VERSION \ org.opencontainers.image.created=$BUILD_DATE \
org.opencontainers.image.revision=$VCS_REF \ org.opencontainers.image.description="Network-wide ads & trackers blocking DNS server" \
org.opencontainers.image.vendor="AdGuard" \ org.opencontainers.image.documentation="https://github.com/AdguardTeam/AdGuardHome/wiki/" \
org.opencontainers.image.title="AdGuard Home" \ org.opencontainers.image.licenses="GPL-3.0" \
org.opencontainers.image.description="Network-wide ads & trackers blocking DNS server" \ org.opencontainers.image.revision=$VCS_REF \
org.opencontainers.image.licenses="GPL-3.0" org.opencontainers.image.source="https://github.com/AdguardTeam/AdGuardHome" \
org.opencontainers.image.title="AdGuard Home" \
org.opencontainers.image.url="https://adguard.com/en/adguard-home/overview.html" \
org.opencontainers.image.vendor="AdGuard" \
org.opencontainers.image.version=$VERSION
# Update certificates. # Update certificates.
RUN apk --no-cache --update add ca-certificates libcap tzdata && \ RUN apk --no-cache --update add ca-certificates libcap tzdata && \
rm -rf /var/cache/apk/* && \ rm -rf /var/cache/apk/* && \
mkdir -p /opt/adguardhome/conf /opt/adguardhome/work && \ mkdir -p /opt/adguardhome/conf /opt/adguardhome/work && \
chown -R nobody: /opt/adguardhome chown -R nobody: /opt/adguardhome
ARG DIST_DIR ARG DIST_DIR
ARG TARGETARCH ARG TARGETARCH

View File

@ -65,9 +65,9 @@ then
fi fi
readonly version readonly version
# Set date and time of the current build unless already set. # Set date and time of the latest commit unless already set.
buildtime="${BUILD_TIME:-$( date -u +%FT%TZ%z )}" committime="${SOURCE_DATE_EPOCH:-$( git log -1 --pretty=%ct )}"
readonly buildtime readonly committime
# Set the linker flags accordingly: set the release channel and the current # Set the linker flags accordingly: set the release channel and the current
# version as well as goarm and gomips variable values, if the variables are set # version as well as goarm and gomips variable values, if the variables are set
@ -78,7 +78,7 @@ readonly version_pkg
ldflags="-s -w" ldflags="-s -w"
ldflags="${ldflags} -X ${version_pkg}.version=${version}" ldflags="${ldflags} -X ${version_pkg}.version=${version}"
ldflags="${ldflags} -X ${version_pkg}.channel=${channel}" ldflags="${ldflags} -X ${version_pkg}.channel=${channel}"
ldflags="${ldflags} -X ${version_pkg}.buildtime=${buildtime}" ldflags="${ldflags} -X ${version_pkg}.committime=${committime}"
if [ "${GOARM:-}" != '' ] if [ "${GOARM:-}" != '' ]
then then
ldflags="${ldflags} -X ${version_pkg}.goarm=${GOARM}" ldflags="${ldflags} -X ${version_pkg}.goarm=${GOARM}"

View File

@ -86,6 +86,7 @@ in
# minor release. If the current commit is the new minor release, # minor release. If the current commit is the new minor release,
# num_commits_since_minor is zero. # num_commits_since_minor is zero.
num_commits_since_minor="$( git rev-list "${last_minor_zero}..HEAD" | wc -l )" num_commits_since_minor="$( git rev-list "${last_minor_zero}..HEAD" | wc -l )"
# The output of darwin's implementation of wc needs to be trimmed from # The output of darwin's implementation of wc needs to be trimmed from
# redundant spaces. # redundant spaces.
num_commits_since_minor="$( echo "$num_commits_since_minor" | tr -d '[:space:]' )" num_commits_since_minor="$( echo "$num_commits_since_minor" | tr -d '[:space:]' )"