Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions define/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ type BuildOptions struct {
CommonBuildOpts *CommonBuildOptions
// CPPFlags are additional arguments to pass to the C Preprocessor (cpp).
CPPFlags []string
// Preprocess tells the builder to run the C Preprocessor (cpp) regardless of the file extension.
Preprocess types.OptionalBool
// DefaultMountsFilePath is the file path holding the mounts to be mounted for RUN
// instructions in "host-path:container-path" format
DefaultMountsFilePath string
Expand Down
9 changes: 8 additions & 1 deletion docs/buildah-build.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The build context directory can be specified as the http(s) URL of an archive, g

If no context directory is specified, then Buildah will assume the current working directory as build context, which should contain a Containerfile.

Containerfiles ending with a ".in" suffix will be preprocessed via cpp(1). This can be useful to decompose Containerfiles into several reusable parts that can be used via CPP's **#include** directive. Notice, a Containerfile.in file can still be used by other tools when manually preprocessing them via `cpp -E`. Any comments ( Lines beginning with `#` ) in included Containerfile(s) that are not preprocess commands, will be printed as warnings during builds.
Containerfiles ending with a ".in" suffix will be automatically preprocessed via cpp(1). This can be useful to decompose Containerfiles into several reusable parts that can be used via CPP's **#include** directive. Notice, a Containerfile.in file can still be used by other tools when manually preprocessing them via `cpp -E`. Any comments ( Lines beginning with `#` ) in included Containerfile(s) that are not preprocess commands, will be printed as warnings during builds.

When the URL is an archive, the contents of the URL is downloaded to a temporary location and extracted before execution.

Expand Down Expand Up @@ -829,6 +829,13 @@ The `buildah build` command allows building images for all Linux architectures,

**NOTE:** The `--platform` option may not be used in combination with the `--arch`, `--os`, or `--variant` options.

**--preprocess**

If specified, will always attempt to use the C Preprocessor cpp(1),
even if the Containerfile doesn't end with the ".in" suffix.
Note: You can also configure this behavior by setting the BUILDAH\_PREPROCESS
environment variable to `1`, `true`, or `yes`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solacelost nit: yes value isn't being checked in DefaultPreprocess.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhhh, it was checked in one early iteration before consolidating on the function for defaults. Apologies.

Up to you if I fix and force push and wait for CI or just small PR to update after. I'd kind of rather update the function than the docs for it, to align with some other env var behavior.


**--pull**

Pull image policy. If not specified, the default is **missing**. If an explicit
Expand Down
6 changes: 4 additions & 2 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
data = contents
}

// pre-process Dockerfiles with ".in" suffix
if strings.HasSuffix(dfile, ".in") {
// pre-process Dockerfiles with ".in" suffix, if --preprocess is specified, or if BUILDAH_PREPROCESS is set
preprocessSpecified := options.Preprocess == types.OptionalBoolTrue
preprocessInferred := (options.Preprocess == types.OptionalBoolUndefined) && strings.HasSuffix(dfile, ".in")
if preprocessSpecified || preprocessInferred {
pData, err := preprocessContainerfileContents(logger, dfile, data, options.ContextDirectory, options.CPPFlags)
if err != nil {
return "", nil, err
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
sbomScanOptions = append(sbomScanOptions, *sbomScanOption)
}

var compatVolumes, createdAnnotation, inheritAnnotations, inheritLabels, skipUnusedStages types.OptionalBool
var compatVolumes, createdAnnotation, inheritAnnotations, inheritLabels, skipUnusedStages, preprocess types.OptionalBool
if c.Flag("compat-volumes").Changed {
compatVolumes = types.NewOptionalBool(iopts.CompatVolumes)
}
Expand All @@ -415,6 +415,9 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
if c.Flag("skip-unused-stages").Changed {
skipUnusedStages = types.NewOptionalBool(iopts.SkipUnusedStages)
}
if c.Flag("preprocess").Changed || DefaultPreprocess() {
Comment thread
solacelost marked this conversation as resolved.
preprocess = types.NewOptionalBool(iopts.Preprocess)
}

options = define.BuildOptions{
AddCapabilities: iopts.CapAdd,
Expand All @@ -433,6 +436,7 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
CompatVolumes: compatVolumes,
ConfidentialWorkload: confidentialWorkloadOptions,
CPPFlags: iopts.CPPFlags,
Preprocess: preprocess,
CommonBuildOpts: commonOpts,
Compression: compression,
CompressionFormat: compressionFormat,
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type BudResults struct {
Timestamp int64
OmitHistory bool
OCIHooksDir []string
Preprocess bool
Pull string
PullAlways bool
PullNever bool
Expand Down Expand Up @@ -250,6 +251,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs.BoolVar(&flags.InheritLabels, "inherit-labels", true, "inherit the labels from the base image or base stages.")
fs.BoolVar(&flags.InheritAnnotations, "inherit-annotations", true, "inherit the annotations from the base image or base stages.")
fs.StringArrayVar(&flags.CPPFlags, "cpp-flag", []string{}, "set additional flag to pass to C preprocessor (cpp)")
fs.BoolVar(&flags.Preprocess, "preprocess", DefaultPreprocess(), "use the C preprocessor (cpp) on a Dockerfile, regardless of the file suffix. Use BUILDAH_PREPROCESS environment variable to change default.")
fs.BoolVar(&flags.CreatedAnnotation, "created-annotation", true, `set an "org.opencontainers.image.created" annotation in the image`)
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
fs.StringVarP(&flags.CWOptions, "cw", "", "", "confidential workload `options`")
Expand Down Expand Up @@ -544,6 +546,16 @@ func DefaultHistory() bool {
return false
}

// DefaultPreprocess returns true if BUILDAH_PREPROCESS is set to "1" or "true"
// otherwise it returns false
func DefaultPreprocess() bool {
preprocess := os.Getenv("BUILDAH_PREPROCESS")
if strings.ToLower(preprocess) == "true" || strings.ToLower(preprocess) == "yes" || preprocess == "1" {
return true
}
return false
}

func VerifyFlagsArgsOrder(args []string) error {
for _, arg := range args {
if strings.HasPrefix(arg, "-") {
Expand Down
60 changes: 60 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4126,6 +4126,66 @@ _EOF
expect_output --substring "Ignoring <stdin>:5:2: error: #error"
}

@test "bud with preprocessor, via --preprocess" {
_prefetch busybox
target=alpine-image
run_buildah build $WITH_POLICY_JSON -t ${target} --preprocess -f Decomposed.tpl $BUDFILES/preprocess
}

@test "bud with preprocessor-requiring containerfile, without --preprocess" {
_prefetch busybox
target=alpine-image
run_buildah 125 build $WITH_POLICY_JSON -t ${target} -f Decomposed.tpl $BUDFILES/preprocess
expect_output --substring 'Build error: Unknown instruction: "RUNHELLO"'
}

@test "bud with preprocessor error, via --preprocess" {
_prefetch busybox
target=alpine-image
run_buildah bud $WITH_POLICY_JSON -t ${target} --preprocess -f Error.tpl $BUDFILES/preprocess
expect_output --substring "Ignoring <stdin>:5:2: error: #error"
}

@test "bud with preprocessor, via env var 1" {
_prefetch busybox
target=alpine-image
BUILDAH_PREPROCESS=1 run_buildah build $WITH_POLICY_JSON -t ${target} -f Decomposed.tpl $BUDFILES/preprocess
}

@test "bud with preprocessor, via env var true" {
_prefetch busybox
target=alpine-image
BUILDAH_PREPROCESS=true run_buildah build $WITH_POLICY_JSON -t ${target} -f Decomposed.tpl $BUDFILES/preprocess
}

@test "bud with preprocessor, via env var yes" {
_prefetch busybox
target=alpine-image
BUILDAH_PREPROCESS=yes run_buildah build $WITH_POLICY_JSON -t ${target} -f Decomposed.tpl $BUDFILES/preprocess
}

@test "bud with preprocessor error, via env var" {
_prefetch busybox
target=alpine-image
BUILDAH_PREPROCESS=1 run_buildah bud $WITH_POLICY_JSON -t ${target} -f Error.tpl $BUDFILES/preprocess
expect_output --substring "Ignoring <stdin>:5:2: error: #error"
}

@test "bud with preprocessor-requiring containerfile.in, with preprocess force disabled" {
_prefetch busybox
target=alpine-image
starthttpd $BUDFILES/preprocess
run_buildah 125 build $WITH_POLICY_JSON -t ${target} --preprocess=false -f Decomposed.in $BUDFILES/preprocess
expect_output --substring 'Build error: Unknown instruction: "RUNHELLO"'
}

@test "bud with preprocessor-requiring containerfile, via explicit cli flag, defaulted on via env var" {
_prefetch busybox
target=alpine-image
BUILDAH_PREPROCESS=1 run_buildah 125 build $WITH_POLICY_JSON -t ${target} --preprocess=false -f Decomposed.tpl $BUDFILES/preprocess
expect_output --substring 'Build error: Unknown instruction: "RUNHELLO"'
}

@test "bud-with-rejected-name" {
target=ThisNameShouldBeRejected
run_buildah 125 build -q $WITH_POLICY_JSON -t ${target} $BUDFILES/from-scratch
Expand Down
5 changes: 5 additions & 0 deletions tests/bud/preprocess/Decomposed.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox

#include "common"

RUNHELLO
5 changes: 5 additions & 0 deletions tests/bud/preprocess/Error.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox

#include "common"

#error THISERROR