From 0ee34534c664530211f6014c73c48f17d486086a Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Tue, 1 Feb 2022 21:44:01 +0300 Subject: [PATCH] Pull request: all: switch to SOURCE_DATE_EPOCH for source date Closes #4221. Squashed commit of the following: commit c84a5699280cf4c0b1c2ed034a44f05ffc74d30d Author: Ainar Garipov Date: Tue Feb 1 21:13:30 2022 +0300 all: switch to SOURCE_DATE_EPOCH for source date --- CHANGELOG.md | 8 ++++++++ internal/version/version.go | 25 +++++++++++++++++-------- scripts/README.md | 7 +++++-- scripts/make/Dockerfile | 30 +++++++++++++++++------------- scripts/make/go-build.sh | 8 ++++---- scripts/make/version.sh | 1 + 6 files changed, 52 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20aafe62..5ad22454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,11 @@ and this project adheres to ### 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 configuration file to avoid confusion ([#3367]). - 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 [#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057 [#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/ diff --git a/internal/version/version.go b/internal/version/version.go index 4218d99e..eec8c4d0 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -7,6 +7,7 @@ import ( "runtime/debug" "strconv" "strings" + "time" "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 // we can GOARCH and GOOS. var ( - channel string = ChannelDevelopment - goarm string - gomips string - version string - buildtime string + channel string = ChannelDevelopment + goarm string + gomips string + version string + committime string ) // Channel returns the current AdGuard Home release channel. @@ -106,7 +107,7 @@ const ( vFmtVerHdr = "Version: " vFmtChanHdr = "Channel: " vFmtGoHdr = "Go version: " - vFmtTimeHdr = "Build time: " + vFmtTimeHdr = "Commit time: " vFmtRaceHdr = "Race: " vFmtGOOSHdr = "GOOS: " + runtime.GOOS vFmtGOARCHHdr = "GOARCH: " + runtime.GOARCH @@ -148,15 +149,23 @@ func Verbose() (v string) { vFmtGoHdr, 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) if goarm != "" { stringutil.WriteToBuilder(b, nl, vFmtGOARMHdr, "v", goarm) } else if gomips != "" { stringutil.WriteToBuilder(b, nl, vFmtGOMIPSHdr, gomips) } + stringutil.WriteToBuilder(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace)) info, ok := debug.ReadBuildInfo() diff --git a/scripts/README.md b/scripts/README.md index 1ca2e471..3178d7ee 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -90,14 +90,15 @@ Required environment: ### `go-build.sh`: Build The Backend Optional environment: - * `BUILD_TIME`: If set, overrides the build time information. Useful for - reproducible builds. * `GOARM`: ARM processor options for the Go compiler. * `GOMIPS`: ARM processor options for the Go compiler. * `GO`: set an alternative name for the Go compiler. * `OUT`: output binary name. * `PARALLELISM`: set the maximum number of concurrently run build commands (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 Go package that is processed. `2` also shows subcommands and environment. The default value is `0`, don't be verbose. @@ -107,6 +108,8 @@ Optional environment: Required environment: * `CHANNEL`: release channel, see above. +[repr]: https://reproducible-builds.org/docs/source-date-epoch/ + ### `go-deps.sh`: Install Backend Dependencies diff --git a/scripts/make/Dockerfile b/scripts/make/Dockerfile index 44649721..f5a543a2 100644 --- a/scripts/make/Dockerfile +++ b/scripts/make/Dockerfile @@ -5,22 +5,26 @@ FROM alpine:3.13 ARG BUILD_DATE ARG VERSION ARG VCS_REF -LABEL maintainer="AdGuard Team " \ - org.opencontainers.image.created=$BUILD_DATE \ - org.opencontainers.image.url="https://adguard.com/adguard-home.html" \ - org.opencontainers.image.source="https://github.com/AdguardTeam/AdGuardHome" \ - org.opencontainers.image.version=$VERSION \ - org.opencontainers.image.revision=$VCS_REF \ - org.opencontainers.image.vendor="AdGuard" \ - org.opencontainers.image.title="AdGuard Home" \ - org.opencontainers.image.description="Network-wide ads & trackers blocking DNS server" \ - org.opencontainers.image.licenses="GPL-3.0" + +LABEL\ + maintainer="AdGuard Team " \ + org.opencontainers.image.authors="AdGuard Team " \ + org.opencontainers.image.created=$BUILD_DATE \ + org.opencontainers.image.description="Network-wide ads & trackers blocking DNS server" \ + org.opencontainers.image.documentation="https://github.com/AdguardTeam/AdGuardHome/wiki/" \ + org.opencontainers.image.licenses="GPL-3.0" \ + org.opencontainers.image.revision=$VCS_REF \ + 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. RUN apk --no-cache --update add ca-certificates libcap tzdata && \ - rm -rf /var/cache/apk/* && \ - mkdir -p /opt/adguardhome/conf /opt/adguardhome/work && \ - chown -R nobody: /opt/adguardhome + rm -rf /var/cache/apk/* && \ + mkdir -p /opt/adguardhome/conf /opt/adguardhome/work && \ + chown -R nobody: /opt/adguardhome ARG DIST_DIR ARG TARGETARCH diff --git a/scripts/make/go-build.sh b/scripts/make/go-build.sh index d6b86f51..7854166c 100644 --- a/scripts/make/go-build.sh +++ b/scripts/make/go-build.sh @@ -65,9 +65,9 @@ then fi readonly version -# Set date and time of the current build unless already set. -buildtime="${BUILD_TIME:-$( date -u +%FT%TZ%z )}" -readonly buildtime +# Set date and time of the latest commit unless already set. +committime="${SOURCE_DATE_EPOCH:-$( git log -1 --pretty=%ct )}" +readonly committime # 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 @@ -78,7 +78,7 @@ readonly version_pkg ldflags="-s -w" ldflags="${ldflags} -X ${version_pkg}.version=${version}" ldflags="${ldflags} -X ${version_pkg}.channel=${channel}" -ldflags="${ldflags} -X ${version_pkg}.buildtime=${buildtime}" +ldflags="${ldflags} -X ${version_pkg}.committime=${committime}" if [ "${GOARM:-}" != '' ] then ldflags="${ldflags} -X ${version_pkg}.goarm=${GOARM}" diff --git a/scripts/make/version.sh b/scripts/make/version.sh index 45cfd353..903be7bf 100644 --- a/scripts/make/version.sh +++ b/scripts/make/version.sh @@ -86,6 +86,7 @@ in # minor release. If the current commit is the new minor release, # num_commits_since_minor is zero. 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 # redundant spaces. num_commits_since_minor="$( echo "$num_commits_since_minor" | tr -d '[:space:]' )"