use containerd's epoch package for handling SOURCE_DATE_EPOCH

This allows us to validate the value before we're using it, and makes
sure we handle things in the same way.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2023-06-23 12:36:37 +02:00
parent bd672eaf5b
commit a272ef1942
5 changed files with 127 additions and 8 deletions

View File

@@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/containerd/console"
"github.com/containerd/containerd/pkg/epoch"
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
@@ -162,16 +164,18 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
return err
}
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
// TODO: extract env var parsing to a method easily usable by library consumers
if v, err := epoch.SourceDateEpoch(); err != nil {
return err
} else if v != nil {
esd := strconv.FormatInt(v.Unix(), 10)
for _, t := range tgts {
if _, ok := t.Args["SOURCE_DATE_EPOCH"]; ok {
if _, ok := t.Args[epoch.SourceDateEpochEnv]; ok {
continue
}
if t.Args == nil {
t.Args = map[string]*string{}
}
t.Args["SOURCE_DATE_EPOCH"] = &v
t.Args[epoch.SourceDateEpochEnv] = &esd
}
}

View File

@@ -15,6 +15,7 @@ import (
"strings"
"github.com/containerd/console"
"github.com/containerd/containerd/pkg/epoch"
"github.com/docker/buildx/build"
"github.com/docker/buildx/builder"
"github.com/docker/buildx/controller"
@@ -120,10 +121,13 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
ExportLoad: o.exportLoad,
}
// TODO: extract env var parsing to a method easily usable by library consumers
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
if _, ok := opts.BuildArgs["SOURCE_DATE_EPOCH"]; !ok {
opts.BuildArgs["SOURCE_DATE_EPOCH"] = v
if _, ok := opts.BuildArgs[epoch.SourceDateEpochEnv]; !ok {
v, err := epoch.SourceDateEpoch()
if err != nil {
return nil, err
}
if v != nil {
opts.BuildArgs[epoch.SourceDateEpochEnv] = strconv.FormatInt(v.Unix(), 10)
}
}