Skip to content

Commit

Permalink
Merge pull request #3576 from rhatdan/errors
Browse files Browse the repository at this point in the history
Report ignorefile location when no content added
  • Loading branch information
openshift-merge-robot committed Oct 15, 2021
2 parents e4a4f2c + a8b15f8 commit 762cf6f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
10 changes: 8 additions & 2 deletions add.go
Expand Up @@ -47,8 +47,10 @@ type AddAndCopyOptions struct {
// If the sources include directory trees, Hasher will be passed
// tar-format archives of the directory trees.
Hasher io.Writer
// Excludes is the contents of the .dockerignore file.
// Excludes is the contents of the .containerignore file.
Excludes []string
// IgnoreFile is the path to the .containerignore file.
IgnoreFile string
// ContextDir is the base directory for content being copied and
// Excludes patterns.
ContextDir string
Expand Down Expand Up @@ -564,7 +566,11 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
}
}
if itemsCopied == 0 {
return errors.Wrapf(syscall.ENOENT, "no items matching glob %q copied (%d filtered out)", localSourceStat.Glob, len(localSourceStat.Globbed))
excludesFile := ""
if options.IgnoreFile != "" {
excludesFile = " using " + options.IgnoreFile
}
return errors.Wrapf(syscall.ENOENT, "no items matching glob %q copied (%d filtered out%s)", localSourceStat.Glob, len(localSourceStat.Globbed), excludesFile)
}
}
return nil
Expand Down
24 changes: 2 additions & 22 deletions cmd/buildah/addcopy.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/containers/buildah/pkg/parse"
"github.com/containers/common/pkg/auth"
"github.com/containers/storage"
"github.com/openshift/imagebuilder"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -231,11 +229,8 @@ func addAndCopyCmd(c *cobra.Command, args []string, verb string, iopts addCopyRe
}
if iopts.contextdir != "" {
var excludes []string
if iopts.ignoreFile != "" {
excludes, err = parseIgnore(iopts.ignoreFile)
} else {
excludes, err = imagebuilder.ParseDockerignore(contextdir)
}

excludes, options.IgnoreFile, err = parse.ContainerIgnoreFile(options.ContextDir, iopts.ignoreFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -273,18 +268,3 @@ func addAndCopyCmd(c *cobra.Command, args []string, verb string, iopts addCopyRe
conditionallyAddHistory(builder, c, "/bin/sh -c #(nop) %s %s%s", verb, contentType, digest.Hex())
return builder.Save()
}

func parseIgnore(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
}
3 changes: 2 additions & 1 deletion cmd/buildah/build.go
Expand Up @@ -317,7 +317,7 @@ func buildCmd(c *cobra.Command, inputArgs []string, iopts buildOptions) error {

var excludes []string
if iopts.IgnoreFile != "" {
if excludes, err = parseIgnore(iopts.IgnoreFile); err != nil {
if excludes, _, err = parse.ContainerIgnoreFile(contextDir, iopts.IgnoreFile); err != nil {
return err
}
}
Expand Down Expand Up @@ -350,6 +350,7 @@ func buildCmd(c *cobra.Command, inputArgs []string, iopts buildOptions) error {
IIDFile: iopts.Iidfile,
In: stdin,
Isolation: isolation,
IgnoreFile: iopts.IgnoreFile,
Labels: iopts.Label,
Layers: layers,
LogRusage: iopts.LogRusage,
Expand Down
2 changes: 2 additions & 0 deletions define/build.go
Expand Up @@ -227,6 +227,8 @@ type BuildOptions struct {
RusageLogFile string
// Excludes is a list of excludes to be used instead of the .dockerignore file.
Excludes []string
// IgnoreFile is a name of the .containerignore file
IgnoreFile string
// From is the image name to use to replace the value specified in the first
// FROM instruction in the Containerfile
From string
Expand Down
4 changes: 3 additions & 1 deletion imagebuildah/executor.go
Expand Up @@ -101,6 +101,7 @@ type Executor struct {
rootfsMap map[string]bool // Holds the names of every stage whose rootfs is referenced in a COPY or ADD instruction.
blobDirectory string
excludes []string
ignoreFile string
unusedArgs map[string]struct{}
capabilities []string
devices define.ContainerDevices
Expand Down Expand Up @@ -143,7 +144,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o

excludes := options.Excludes
if len(excludes) == 0 {
excludes, err = imagebuilder.ParseDockerignore(options.ContextDirectory)
excludes, options.IgnoreFile, err = parse.ContainerIgnoreFile(options.ContextDirectory, options.IgnoreFile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,6 +209,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
store: store,
contextDir: options.ContextDirectory,
excludes: excludes,
ignoreFile: options.IgnoreFile,
pullPolicy: options.PullPolicy,
registry: options.Registry,
ignoreUnrecognizedInstructions: options.IgnoreUnrecognizedInstructions,
Expand Down
1 change: 1 addition & 0 deletions imagebuildah/stage_executor.go
Expand Up @@ -401,6 +401,7 @@ func (s *StageExecutor) Copy(excludes []string, copies ...imagebuilder.Copy) err
PreserveOwnership: preserveOwnership,
ContextDir: contextDir,
Excludes: copyExcludes,
IgnoreFile: s.executor.ignoreFile,
IDMappingOptions: idMappingOptions,
StripSetuidBit: stripSetuid,
StripSetgidBit: stripSetgid,
Expand Down
18 changes: 18 additions & 0 deletions pkg/parse/parse.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/containers/storage/pkg/unshare"
units "github.com/docker/go-units"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/openshift/imagebuilder"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -1231,3 +1232,20 @@ func SSH(sshSources []string) (map[string]*sshagent.Source, error) {
}
return parsed, nil
}

func ContainerIgnoreFile(contextDir, path string) ([]string, string, error) {
if path != "" {
excludes, err := imagebuilder.ParseIgnore(path)
return excludes, path, err
}
path = filepath.Join(contextDir, ".containerignore")
excludes, err := imagebuilder.ParseIgnore(path)
if os.IsNotExist(err) {
path = filepath.Join(contextDir, ".dockerignore")
excludes, err = imagebuilder.ParseIgnore(path)
}
if os.IsNotExist(err) {
return excludes, "", nil
}
return excludes, path, err
}
1 change: 1 addition & 0 deletions tests/bud.bats
Expand Up @@ -110,6 +110,7 @@ symlink(subdir)"
@test "bud with .dockerignore #2" {
run_buildah 125 build -t testbud3 --signature-policy ${TESTSDIR}/policy.json ${TESTSDIR}/bud/dockerignore3
expect_output --substring 'error building.*"COPY test1.txt /upload/test1.txt".*no such file or directory'
expect_output --substring $(realpath "${TESTSDIR}/bud/dockerignore3/.dockerignore")
}

@test "bud-flags-order-verification" {
Expand Down

0 comments on commit 762cf6f

Please sign in to comment.