Accept SOURCE_DATE_EPOCH as a build-arg

When SOURCE_DATE_EPOCH is passed in as a build-arg, treat it as we would
if it was passed in via the environment or its own CLI flag.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2025-06-23 11:19:06 -04:00
parent b9c485c123
commit 149bf968f5
2 changed files with 39 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"github.com/containerd/platforms" "github.com/containerd/platforms"
"github.com/containers/buildah" "github.com/containers/buildah"
@ -219,6 +220,15 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
} }
} }
if sourceDateEpoch, ok := options.Args[internal.SourceDateEpochName]; ok && options.SourceDateEpoch == nil {
sde, err := strconv.ParseInt(sourceDateEpoch, 10, 64)
if err != nil {
return "", nil, fmt.Errorf("parsing SOURCE_DATE_EPOCH build-arg %q: %w", sourceDateEpoch, err)
}
sdeTime := time.Unix(sde, 0)
options.SourceDateEpoch = &sdeTime
}
systemContext := options.SystemContext systemContext := options.SystemContext
for _, platform := range options.Platforms { for _, platform := range options.Platforms {
platformContext := *systemContext platformContext := *systemContext

View File

@ -8117,3 +8117,32 @@ _EOF
fi fi
done done
} }
@test "bud-with-source-date-epoch-arg" {
_prefetch busybox
local timestamp=60
local datestamp="1970-01-01T00:01:00Z"
mkdir -p $TEST_SCRATCH_DIR/buildcontext
cat > $TEST_SCRATCH_DIR/buildcontext/Dockerfile <<EOF
FROM busybox
RUN echo \$SOURCE_DATE_EPOCH | tee /SOURCE_DATE_EPOCH_BEFORE
ARG SOURCE_DATE_EPOCH
RUN echo \$SOURCE_DATE_EPOCH | tee /SOURCE_DATE_EPOCH_AFTER
EOF
run_buildah build --layers --no-cache --build-arg=SOURCE_DATE_EPOCH=$timestamp --rewrite-timestamp -t target $TEST_SCRATCH_DIR/buildcontext
run_buildah from target
local cid="$output"
run_buildah run "$cid" cat /SOURCE_DATE_EPOCH_BEFORE
assert "$output" = "" "SOURCE_DATE_EPOCH wasn't empty before it was declared"
run_buildah run "$cid" cat /SOURCE_DATE_EPOCH_AFTER
assert "$output" = "$timestamp" "SOURCE_DATE_EPOCH was wrong after it was declared"
run_buildah build --layers --no-cache --build-arg=SOURCE_DATE_EPOCH=$timestamp --rewrite-timestamp -t oci:$TEST_SCRATCH_DIR/layout $TEST_SCRATCH_DIR/buildcontext
local manifest=${TEST_SCRATCH_DIR}/layout/$(oci_image_manifest ${TEST_SCRATCH_DIR}/layout)
run jq -r '.annotations."org.opencontainers.image.created"' $manifest
assert $status = 0 "error running jq"
assert "$output" = "$datestamp" "SOURCE_DATE_EPOCH build arg didn't affect image creation date annotation"
local config=${TEST_SCRATCH_DIR}/layout/$(oci_image_config ${TEST_SCRATCH_DIR}/layout)
run jq -r .created $config
assert $status = 0 "error running jq"
assert "$output" = "$datestamp" "SOURCE_DATE_EPOCH build arg didn't affect image config creation date"
}