Skip to content

Commit 149bf96

Browse files
committed
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 <[email protected]>
1 parent b9c485c commit 149bf96

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

imagebuildah/build.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strconv"
1818
"strings"
1919
"sync"
20+
"time"
2021

2122
"github.com/containerd/platforms"
2223
"github.com/containers/buildah"
@@ -219,6 +220,15 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
219220
}
220221
}
221222

223+
if sourceDateEpoch, ok := options.Args[internal.SourceDateEpochName]; ok && options.SourceDateEpoch == nil {
224+
sde, err := strconv.ParseInt(sourceDateEpoch, 10, 64)
225+
if err != nil {
226+
return "", nil, fmt.Errorf("parsing SOURCE_DATE_EPOCH build-arg %q: %w", sourceDateEpoch, err)
227+
}
228+
sdeTime := time.Unix(sde, 0)
229+
options.SourceDateEpoch = &sdeTime
230+
}
231+
222232
systemContext := options.SystemContext
223233
for _, platform := range options.Platforms {
224234
platformContext := *systemContext

tests/bud.bats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8117,3 +8117,32 @@ _EOF
81178117
fi
81188118
done
81198119
}
8120+
8121+
@test "bud-with-source-date-epoch-arg" {
8122+
_prefetch busybox
8123+
local timestamp=60
8124+
local datestamp="1970-01-01T00:01:00Z"
8125+
mkdir -p $TEST_SCRATCH_DIR/buildcontext
8126+
cat > $TEST_SCRATCH_DIR/buildcontext/Dockerfile <<EOF
8127+
FROM busybox
8128+
RUN echo \$SOURCE_DATE_EPOCH | tee /SOURCE_DATE_EPOCH_BEFORE
8129+
ARG SOURCE_DATE_EPOCH
8130+
RUN echo \$SOURCE_DATE_EPOCH | tee /SOURCE_DATE_EPOCH_AFTER
8131+
EOF
8132+
run_buildah build --layers --no-cache --build-arg=SOURCE_DATE_EPOCH=$timestamp --rewrite-timestamp -t target $TEST_SCRATCH_DIR/buildcontext
8133+
run_buildah from target
8134+
local cid="$output"
8135+
run_buildah run "$cid" cat /SOURCE_DATE_EPOCH_BEFORE
8136+
assert "$output" = "" "SOURCE_DATE_EPOCH wasn't empty before it was declared"
8137+
run_buildah run "$cid" cat /SOURCE_DATE_EPOCH_AFTER
8138+
assert "$output" = "$timestamp" "SOURCE_DATE_EPOCH was wrong after it was declared"
8139+
run_buildah build --layers --no-cache --build-arg=SOURCE_DATE_EPOCH=$timestamp --rewrite-timestamp -t oci:$TEST_SCRATCH_DIR/layout $TEST_SCRATCH_DIR/buildcontext
8140+
local manifest=${TEST_SCRATCH_DIR}/layout/$(oci_image_manifest ${TEST_SCRATCH_DIR}/layout)
8141+
run jq -r '.annotations."org.opencontainers.image.created"' $manifest
8142+
assert $status = 0 "error running jq"
8143+
assert "$output" = "$datestamp" "SOURCE_DATE_EPOCH build arg didn't affect image creation date annotation"
8144+
local config=${TEST_SCRATCH_DIR}/layout/$(oci_image_config ${TEST_SCRATCH_DIR}/layout)
8145+
run jq -r .created $config
8146+
assert $status = 0 "error running jq"
8147+
assert "$output" = "$datestamp" "SOURCE_DATE_EPOCH build arg didn't affect image config creation date"
8148+
}

0 commit comments

Comments
 (0)