Merge pull request #6247 from nalind/source-date-epoch-build-arg

Accept SOURCE_DATE_EPOCH as a build-arg
This commit is contained in:
openshift-merge-bot[bot] 2025-06-26 19:30:16 +00:00 committed by GitHub
commit f28c074787
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/containerd/platforms"
"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
for _, platform := range options.Platforms {
platformContext := *systemContext

View File

@ -8117,3 +8117,32 @@ _EOF
fi
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"
}