Skip to content

Commit

Permalink
Merge pull request #2807 from rhatdan/ignore
Browse files Browse the repository at this point in the history
Add --ignorefile flag to use alternate .dockerignore flags
  • Loading branch information
openshift-merge-robot authored Nov 25, 2020
2 parents 587e617 + fde2597 commit dd26b13
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 17 deletions.
37 changes: 36 additions & 1 deletion cmd/buildah/addcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"io/ioutil"
"strings"

"github.com/containers/buildah"
buildahcli "github.com/containers/buildah/pkg/cli"
Expand All @@ -13,6 +15,8 @@ type addCopyResults struct {
addHistory bool
chown string
quiet bool
ignoreFile string
contextdir string
}

func init() {
Expand Down Expand Up @@ -52,13 +56,17 @@ func init() {
addFlags.SetInterspersed(false)
addFlags.BoolVar(&addOpts.addHistory, "add-history", false, "add an entry for this operation to the image's history. Use BUILDAH_HISTORY environment variable to override. (default false)")
addFlags.StringVar(&addOpts.chown, "chown", "", "set the user and group ownership of the destination content")
addFlags.StringVar(&addOpts.contextdir, "contextdir", "", "context directory path")
addFlags.StringVar(&addOpts.ignoreFile, "ignorefile", "", "path to .dockerignore file")
addFlags.BoolVarP(&addOpts.quiet, "quiet", "q", false, "don't output a digest of the newly-added/copied content")

// TODO We could avoid some duplication here if need-be; given it is small, leaving as is
copyFlags := copyCommand.Flags()
copyFlags.SetInterspersed(false)
copyFlags.BoolVar(&copyOpts.addHistory, "add-history", false, "add an entry for this operation to the image's history. Use BUILDAH_HISTORY environment variable to override. (default false)")
copyFlags.StringVar(&copyOpts.chown, "chown", "", "set the user and group ownership of the destination content")
copyFlags.StringVar(&copyOpts.ignoreFile, "ignorefile", "", "path to .dockerignore file")
copyFlags.StringVar(&copyOpts.contextdir, "contextdir", "", "context directory path")
copyFlags.BoolVarP(&copyOpts.quiet, "quiet", "q", false, "don't output a digest of the newly-added/copied content")

rootCmd.AddCommand(addCommand)
Expand Down Expand Up @@ -100,7 +108,19 @@ func addAndCopyCmd(c *cobra.Command, args []string, verb string, extractLocalArc
builder.ContentDigester.Restart()

options := buildah.AddAndCopyOptions{
Chown: iopts.chown,
Chown: iopts.chown,
ContextDir: iopts.contextdir,
}
if iopts.ignoreFile != "" {
if iopts.contextdir == "" {
return errors.Errorf("--ignore options requires that you specify a context dir using --contextdir")
}

excludes, err := parseDockerignore(iopts.ignoreFile)
if err != nil {
return err
}
options.Excludes = excludes
}

err = builder.Add(dest, extractLocalArchives, options, args...)
Expand All @@ -126,3 +146,18 @@ func addCmd(c *cobra.Command, args []string, iopts addCopyResults) error {
func copyCmd(c *cobra.Command, args []string, iopts addCopyResults) error {
return addAndCopyCmd(c, args, "COPY", false, iopts)
}

func parseDockerignore(ignoreFile string) ([]string, error) {
var excludes []string
ignore, err := ioutil.ReadFile(ignoreFile)
if err != nil {
return excludes, err
}
for _, e := range strings.Split(string(ignore), "\n") {
if len(e) == 0 || e[0] == '#' {
continue
}
excludes = append(excludes, e)
}
return excludes, nil
}
7 changes: 7 additions & 0 deletions cmd/buildah/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {
Jobs: &iopts.Jobs,
LogRusage: iopts.LogRusage,
}
if iopts.IgnoreFile != "" {
excludes, err := parseDockerignore(iopts.IgnoreFile)
if err != nil {
return err
}
options.Excludes = excludes
}
if c.Flag("timestamp").Changed {
timestamp := time.Unix(iopts.Timestamp, 0).UTC()
options.Timestamp = &timestamp
Expand Down
5 changes: 5 additions & 0 deletions contrib/completions/bash/buildah
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ return 1
--file
--format
--http-proxy
--ignorefile
--iidfile
--isolation
--ipc
Expand Down Expand Up @@ -536,6 +537,8 @@ return 1

local options_with_args="
--chown
--contextdir
--ignorefile
"

local all_options="$options_with_args $boolean_options"
Expand All @@ -558,6 +561,8 @@ return 1

local options_with_args="
-chown
--contextdir
--ignorefile
"

local all_options="$options_with_args $boolean_options"
Expand Down
20 changes: 15 additions & 5 deletions docs/buildah-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ BUILDAH\_HISTORY environment variable. `export BUILDAH_HISTORY=true`

Sets the user and group ownership of the destination content.

**--contextdir**

Build context directory. Specifying a context directory causes Buildah to
chroot into that context directory. This means copying files pointed at
by symbolic links outside of the chroot will fail.

**--ignorefile**

Path to an alternative .dockerignore file. Requires --contextdir be specified.

**--quiet**, **-q**

Refrain from printing a digest of the added content.
Expand All @@ -49,13 +59,13 @@ buildah add containerID 'passwd' 'certs.d' /etc

## FILES

### `.dockerignore`
### .dockerignore

If the file .dockerignore exists in the context directory, `buildah copy` reads
its contents. Buildah uses the content to exclude files and directories from
the context directory, when copying content into the image.
When the \fB\fC\-\-ignorefile\fR option is specified Buildah reads the
content to exclude files and directories from the source directory, when
copying content into the image.

Users can specify a series of Unix shell globals in a .dockerignore file to
Users can specify a series of Unix shell globals in a inore file to
identify files/directories to exclude.

Buildah supports a special wildcard string `**` which matches any number of
Expand Down
11 changes: 8 additions & 3 deletions docs/buildah-bud.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ those.

Write the image ID to the file.

**--ignorefile**

Path to an alternative .dockerignore file.

**--ipc** *how*

Sets the configuration for IPC namespaces when handling `RUN` instructions.
Expand Down Expand Up @@ -733,9 +737,10 @@ are stored while pulling and pushing images. Defaults to '/var/tmp'.

### `.dockerignore`

If the file .dockerignore exists in the context directory, `buildah bud` reads
its contents. Buildah uses the content to exclude files and directories from
the context directory, when executing COPY and ADD directives in the
If the file .dockerignore exists in the context directory, `buildah copy` reads
its contents. Use `--ignorefile` flag to overide .dockerignore path location.
Buildah uses the content to exclude files and directories from the context
directory, when executing COPY and ADD directives in the
Containerfile/Dockerfile

Users can specify a series of Unix shell globals in a .dockerignore file to
Expand Down
20 changes: 15 additions & 5 deletions docs/buildah-copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ BUILDAH\_HISTORY environment variable. `export BUILDAH_HISTORY=true`

Sets the user and group ownership of the destination content.

**--contextdir**

Build context directory. Specifying a context directory causes Buildah to
chroot into a the context directory. This means copying files pointed at
by symbolic links outside of the chroot will fail.

**--ignorefile**

Path to an alternative .dockerignore file. Requires --contextdir be specified.

**--quiet**, **-q**

Refrain from printing a digest of the copied content.
Expand All @@ -47,13 +57,13 @@ buildah copy containerID 'passwd' 'certs.d' /etc

## FILES

### `.dockerignore`
### .dockerignore

If the file .dockerignore exists in the context directory, `buildah copy` reads
its contents. Buildah uses the content to exclude files and directories from
the context directory, when copying content into the image.
When the \fB\fC\-\-ignorefile\fR option is specified Buildah reads the
content to exclude files and directories from the source directory, when
copying content into the image.

Users can specify a series of Unix shell globals in a .dockerignore file to
Users can specify a series of Unix shell globals in a inore file to
identify files/directories to exclude.

Buildah supports a special wildcard string `**` which matches any number of
Expand Down
2 changes: 2 additions & 0 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ type BuildOptions struct {
Jobs *int
// LogRusage logs resource usage for each step.
LogRusage bool
// Excludes is a list of excludes to be used instead of the .dockerignore file.
Excludes []string
}

// BuildDockerfiles parses a set of one or more Dockerfiles (which may be
Expand Down
9 changes: 6 additions & 3 deletions imagebuildah/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func NewExecutor(store storage.Store, options BuildOptions, mainNode *parser.Nod
return nil, errors.Wrapf(err, "failed to get container config")
}

excludes, err := imagebuilder.ParseDockerignore(options.ContextDirectory)
if err != nil {
return nil, err
excludes := options.Excludes
if len(excludes) == 0 {
excludes, err = imagebuilder.ParseDockerignore(options.ContextDirectory)
if err != nil {
return nil, err
}
}
capabilities, err := defaultContainerConfig.Capabilities("", options.AddCapabilities, options.DropCapabilities)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type BudResults struct {
Creds string
DisableCompression bool
DisableContentTrust bool
IgnoreFile string
File []string
Format string
Iidfile string
Expand Down Expand Up @@ -185,6 +186,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
fs.BoolVarP(&flags.DisableCompression, "disable-compression", "D", true, "don't compress layers by default")
fs.BoolVar(&flags.DisableContentTrust, "disable-content-trust", false, "This is a Docker specific option and is a NOOP")
fs.StringVar(&flags.IgnoreFile, "ignorefile", "", "path to an alternate .dockerignore file")
fs.StringSliceVarP(&flags.File, "file", "f", []string{}, "`pathname or URL` of a Dockerfile")
fs.StringVar(&flags.Format, "format", DefaultFormat(), "`format` of the built image's manifest and metadata. Use BUILDAH_FORMAT environment variable to override.")
fs.StringVar(&flags.Iidfile, "iidfile", "", "`file` to write the image ID to")
Expand Down Expand Up @@ -231,6 +233,7 @@ func GetBudFlagsCompletions() commonComp.FlagCompletions {
flagCompletion["creds"] = commonComp.AutocompleteNone
flagCompletion["file"] = commonComp.AutocompleteDefault
flagCompletion["format"] = commonComp.AutocompleteNone
flagCompletion["ignorefile"] = commonComp.AutocompleteDefault
flagCompletion["iidfile"] = commonComp.AutocompleteDefault
flagCompletion["jobs"] = commonComp.AutocompleteNone
flagCompletion["label"] = commonComp.AutocompleteNone
Expand Down
33 changes: 33 additions & 0 deletions tests/add.bats
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,36 @@ load helpers
run_buildah add $cid tools/Makefile /
run_buildah run $cid ls /Makefile
}

@test "add --ignore" {
mytest=${TESTDIR}/mytest
mkdir -p ${mytest}
touch ${mytest}/mystuff
touch ${mytest}/source.go
mkdir -p ${mytest}/notmystuff
touch ${mytest}/notmystuff/notmystuff
cat > ${mytest}/.ignore << _EOF
*.go
.ignore
notmystuff
_EOF

expect="
stuff
stuff/mystuff"

run_buildah from --signature-policy ${TESTSDIR}/policy.json scratch
cid=$output

run_buildah 125 copy --ignorefile ${mytest}/.ignore $cid ${mytest} /stuff
expect_output -- "--ignore options requires that you specify a context dir using --contextdir" "container file list"

run_buildah add --contextdir=${mytest} --ignorefile ${mytest}/.ignore $cid ${mytest} /stuff

run_buildah mount $cid
mnt=$output
run find $mnt -printf "%P\n"
filelist=$(LC_ALL=C sort <<<"$output")
run_buildah umount $cid
expect_output --from="$filelist" "$expect" "container file list"
}
24 changes: 24 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2388,3 +2388,27 @@ EOF
@test "bud-terminal" {
run_buildah bud ${TESTSDIR}/bud/terminal
}

@test "bud --ignore containerignore" {
_prefetch alpine busybox

CONTEXTDIR=${TESTDIR}/dockerignore
cp -r ${TESTSDIR}/bud/dockerignore ${CONTEXTDIR}
mv ${CONTEXTDIR}/.dockerignore ${TESTDIR}/containerignore

run_buildah bud -t testbud --signature-policy ${TESTSDIR}/policy.json -f ${CONTEXTDIR}/Dockerfile.succeed --ignorefile ${TESTDIR}/containerignore ${CONTEXTDIR}

run_buildah from --name myctr testbud

run_buildah 1 run myctr ls -l test1.txt

run_buildah run myctr ls -l test2.txt

run_buildah 1 run myctr ls -l sub1.txt

run_buildah 1 run myctr ls -l sub2.txt

run_buildah run myctr ls -l subdir/sub1.txt

run_buildah 1 run myctr ls -l subdir/sub2.txt
}
33 changes: 33 additions & 0 deletions tests/copy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,36 @@ load helpers
run_buildah 125 build-using-dockerfile --signature-policy ${TESTSDIR}/policy.json ${TESTDIR}
expect_output --substring "no such file or directory"
}

@test "copy --ignore" {
mytest=${TESTDIR}/mytest
mkdir -p ${mytest}
touch ${mytest}/mystuff
touch ${mytest}/source.go
mkdir -p ${mytest}/notmystuff
touch ${mytest}/notmystuff/notmystuff
cat > ${mytest}/.ignore << _EOF
*.go
.ignore
notmystuff
_EOF

expect="
stuff
stuff/mystuff"

run_buildah from --signature-policy ${TESTSDIR}/policy.json scratch
cid=$output

run_buildah 125 copy --ignorefile ${mytest}/.ignore $cid ${mytest} /stuff
expect_output -- "--ignore options requires that you specify a context dir using --contextdir" "container file list"

run_buildah copy --contextdir=${mytest} --ignorefile ${mytest}/.ignore $cid ${mytest} /stuff

run_buildah mount $cid
mnt=$output
run find $mnt -printf "%P\n"
filelist=$(LC_ALL=C sort <<<"$output")
run_buildah umount $cid
expect_output --from="$filelist" "$expect" "container file list"
}

0 comments on commit dd26b13

Please sign in to comment.