Skip to content

Commit c4b6cd5

Browse files
committed
fix(staged-dockerfile): fix docker ignore path matcher
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
1 parent 647700a commit c4b6cd5

File tree

2 files changed

+64
-107
lines changed

2 files changed

+64
-107
lines changed

pkg/build/image/build_context_archive.go

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package image
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"io/ioutil"
87
"os"
98
"path/filepath"
109

11-
"github.com/docker/docker/builder/dockerignore"
12-
1310
"github.com/werf/logboek"
1411
"github.com/werf/werf/pkg/container_backend"
1512
"github.com/werf/werf/pkg/context_manager"
@@ -36,17 +33,18 @@ type BuildContextArchive struct {
3633
func (a *BuildContextArchive) Create(ctx context.Context, opts container_backend.BuildContextArchiveCreateOptions) error {
3734
contextPathRelativeToGitWorkTree := filepath.Join(a.giterminismMgr.RelativeToGitProjectDir(), opts.ContextGitSubDir)
3835

39-
pathMatcher := path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{BasePath: contextPathRelativeToGitWorkTree})
40-
if dockerIgnorePathMatcher, err := createDockerIgnorePathMatcher(ctx, a.giterminismMgr, opts.ContextGitSubDir, opts.DockerfileRelToContextPath); err != nil {
36+
dockerIgnorePathMatcher, err := createDockerIgnorePathMatcher(ctx, a.giterminismMgr, opts.ContextGitSubDir, opts.DockerfileRelToContextPath)
37+
if err != nil {
4138
return fmt.Errorf("unable to create dockerignore path matcher: %w", err)
42-
} else if dockerIgnorePathMatcher != nil {
43-
pathMatcher = path_matcher.NewMultiPathMatcher(pathMatcher, dockerIgnorePathMatcher)
4439
}
4540

4641
archive, err := a.giterminismMgr.LocalGitRepo().GetOrCreateArchive(ctx, git_repo.ArchiveOptions{
47-
PathScope: contextPathRelativeToGitWorkTree,
48-
PathMatcher: pathMatcher,
49-
Commit: a.giterminismMgr.HeadCommit(),
42+
PathScope: contextPathRelativeToGitWorkTree,
43+
PathMatcher: path_matcher.NewMultiPathMatcher(path_matcher.NewPathMatcher(
44+
path_matcher.PathMatcherOptions{BasePath: contextPathRelativeToGitWorkTree}),
45+
dockerIgnorePathMatcher,
46+
),
47+
Commit: a.giterminismMgr.HeadCommit(),
5048
})
5149
if err != nil {
5250
return fmt.Errorf("unable to get or create archive: %w", err)
@@ -107,57 +105,3 @@ func (a *BuildContextArchive) CleanupExtractedDir(ctx context.Context) {
107105
logboek.Context(ctx).Warn().LogF("WARNING: unable to remove extracted context dir %q: %s", a.extractionDir, err)
108106
}
109107
}
110-
111-
// Might return nil.
112-
func createDockerIgnorePathMatcher(ctx context.Context, giterminismMgr giterminism_manager.Interface, contextGitSubDir, dockerfileRelToContextPath string) (path_matcher.PathMatcher, error) {
113-
dockerfileRelToGitPath := filepath.Join(contextGitSubDir, dockerfileRelToContextPath)
114-
115-
var dockerIgnorePatterns []string
116-
for _, dockerIgnoreRelToContextPath := range []string{
117-
dockerfileRelToContextPath + ".dockerignore",
118-
".dockerignore",
119-
} {
120-
dockerIgnoreRelToGitPath := filepath.Join(contextGitSubDir, dockerIgnoreRelToContextPath)
121-
if exist, err := giterminismMgr.FileReader().IsDockerignoreExistAnywhere(ctx, dockerIgnoreRelToGitPath); err != nil {
122-
return nil, err
123-
} else if !exist {
124-
continue
125-
}
126-
127-
dockerIgnore, err := giterminismMgr.FileReader().ReadDockerignore(ctx, dockerIgnoreRelToGitPath)
128-
if err != nil {
129-
return nil, err
130-
}
131-
132-
r := bytes.NewReader(dockerIgnore)
133-
dockerIgnorePatterns, err = dockerignore.ReadAll(r)
134-
if err != nil {
135-
return nil, fmt.Errorf("unable to read %q file: %w", dockerIgnoreRelToContextPath, err)
136-
}
137-
138-
break
139-
}
140-
141-
if dockerIgnorePatterns == nil {
142-
return nil, nil
143-
}
144-
145-
dockerIgnorePathMatcher := path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
146-
BasePath: filepath.Join(giterminismMgr.RelativeToGitProjectDir(), contextGitSubDir),
147-
DockerignorePatterns: dockerIgnorePatterns,
148-
})
149-
150-
if !dockerIgnorePathMatcher.IsPathMatched(dockerfileRelToGitPath) {
151-
logboek.Context(ctx).Warn().LogLn("WARNING: There is no way to ignore the Dockerfile due to docker limitation when building an image for a compressed context that reads from STDIN.")
152-
logboek.Context(ctx).Warn().LogF("WARNING: To hide this message, remove the Dockerfile ignore rule or add an exception rule.\n")
153-
154-
exceptionRule := "!" + dockerfileRelToContextPath
155-
dockerIgnorePatterns = append(dockerIgnorePatterns, exceptionRule)
156-
dockerIgnorePathMatcher = path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
157-
BasePath: filepath.Join(giterminismMgr.RelativeToGitProjectDir(), contextGitSubDir),
158-
DockerignorePatterns: dockerIgnorePatterns,
159-
})
160-
}
161-
162-
return dockerIgnorePathMatcher, nil
163-
}

pkg/build/image/dockerfile.go

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/werf/werf/pkg/dockerfile"
1919
"github.com/werf/werf/pkg/dockerfile/frontend"
2020
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
21+
"github.com/werf/werf/pkg/giterminism_manager"
2122
"github.com/werf/werf/pkg/path_matcher"
2223
"github.com/werf/werf/pkg/util"
2324
)
@@ -121,54 +122,17 @@ func mapLegacyDockerfileToImage(ctx context.Context, dockerfileImageConfig *conf
121122
}
122123
}
123124

125+
dockerIgnorePathMatcher, err := createDockerIgnorePathMatcher(ctx, opts.GiterminismManager, dockerfileImageConfig.Context, dockerfileImageConfig.Dockerfile)
126+
if err != nil {
127+
return nil, fmt.Errorf("unable to create dockerignore path matcher: %w", err)
128+
}
129+
124130
relDockerfilePath := filepath.Join(dockerfileImageConfig.Context, dockerfileImageConfig.Dockerfile)
125131
dockerfileData, err := opts.GiterminismManager.FileReader().ReadDockerfile(ctx, relDockerfilePath)
126132
if err != nil {
127133
return nil, err
128134
}
129135

130-
var relDockerignorePath string
131-
var dockerignorePatterns []string
132-
for _, relContextDockerignorePath := range []string{
133-
dockerfileImageConfig.Dockerfile + ".dockerignore",
134-
".dockerignore",
135-
} {
136-
relDockerignorePath = filepath.Join(dockerfileImageConfig.Context, relContextDockerignorePath)
137-
if exist, err := opts.GiterminismManager.FileReader().IsDockerignoreExistAnywhere(ctx, relDockerignorePath); err != nil {
138-
return nil, err
139-
} else if exist {
140-
dockerignoreData, err := opts.GiterminismManager.FileReader().ReadDockerignore(ctx, relDockerignorePath)
141-
if err != nil {
142-
return nil, err
143-
}
144-
145-
r := bytes.NewReader(dockerignoreData)
146-
dockerignorePatterns, err = dockerignore.ReadAll(r)
147-
if err != nil {
148-
return nil, fmt.Errorf("unable to read %q file: %w", relContextDockerignorePath, err)
149-
}
150-
151-
break
152-
}
153-
}
154-
155-
dockerignorePathMatcher := path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
156-
BasePath: filepath.Join(opts.GiterminismManager.RelativeToGitProjectDir(), dockerfileImageConfig.Context),
157-
DockerignorePatterns: dockerignorePatterns,
158-
})
159-
160-
if !dockerignorePathMatcher.IsPathMatched(relDockerfilePath) {
161-
exceptionRule := "!" + dockerfileImageConfig.Dockerfile
162-
logboek.Context(ctx).Warn().LogLn("WARNING: There is no way to ignore the Dockerfile due to docker limitation when building an image for a compressed context that reads from STDIN.")
163-
logboek.Context(ctx).Warn().LogF("WARNING: To hide this message, remove the Dockerfile ignore rule from the %q or add an exception rule %q.\n", relDockerignorePath, exceptionRule)
164-
165-
dockerignorePatterns = append(dockerignorePatterns, exceptionRule)
166-
dockerignorePathMatcher = path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
167-
BasePath: filepath.Join(opts.GiterminismManager.RelativeToGitProjectDir(), dockerfileImageConfig.Context),
168-
DockerignorePatterns: dockerignorePatterns,
169-
})
170-
}
171-
172136
p, err := parser.Parse(bytes.NewReader(dockerfileData))
173137
if err != nil {
174138
return nil, err
@@ -211,7 +175,7 @@ func mapLegacyDockerfileToImage(ctx context.Context, dockerfileImageConfig *conf
211175
dockerfileImageConfig.SSH,
212176
),
213177
ds,
214-
stage.NewContextChecksum(dockerignorePathMatcher),
178+
stage.NewContextChecksum(dockerIgnorePathMatcher),
215179
baseStageOptions,
216180
dockerfileImageConfig.Dependencies,
217181
)
@@ -222,3 +186,52 @@ func mapLegacyDockerfileToImage(ctx context.Context, dockerfileImageConfig *conf
222186

223187
return img, nil
224188
}
189+
190+
func createDockerIgnorePathMatcher(ctx context.Context, giterminismMgr giterminism_manager.Interface, contextGitSubDir, dockerfileRelToContextPath string) (path_matcher.PathMatcher, error) {
191+
dockerfileRelToGitPath := filepath.Join(contextGitSubDir, dockerfileRelToContextPath)
192+
193+
var dockerIgnorePatterns []string
194+
for _, dockerIgnoreRelToContextPath := range []string{
195+
dockerfileRelToContextPath + ".dockerignore",
196+
".dockerignore",
197+
} {
198+
dockerIgnoreRelToGitPath := filepath.Join(contextGitSubDir, dockerIgnoreRelToContextPath)
199+
if exist, err := giterminismMgr.FileReader().IsDockerignoreExistAnywhere(ctx, dockerIgnoreRelToGitPath); err != nil {
200+
return nil, err
201+
} else if !exist {
202+
continue
203+
}
204+
205+
dockerIgnore, err := giterminismMgr.FileReader().ReadDockerignore(ctx, dockerIgnoreRelToGitPath)
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
r := bytes.NewReader(dockerIgnore)
211+
dockerIgnorePatterns, err = dockerignore.ReadAll(r)
212+
if err != nil {
213+
return nil, fmt.Errorf("unable to read %q file: %w", dockerIgnoreRelToContextPath, err)
214+
}
215+
216+
break
217+
}
218+
219+
dockerIgnorePathMatcher := path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
220+
BasePath: filepath.Join(giterminismMgr.RelativeToGitProjectDir(), contextGitSubDir),
221+
DockerignorePatterns: dockerIgnorePatterns,
222+
})
223+
224+
if !dockerIgnorePathMatcher.IsPathMatched(dockerfileRelToGitPath) {
225+
logboek.Context(ctx).Warn().LogLn("WARNING: There is no way to ignore the Dockerfile due to docker limitation when building an image for a compressed context that reads from STDIN.")
226+
logboek.Context(ctx).Warn().LogF("WARNING: To hide this message, remove the Dockerfile ignore rule or add an exception rule.\n")
227+
228+
exceptionRule := "!" + dockerfileRelToContextPath
229+
dockerIgnorePatterns = append(dockerIgnorePatterns, exceptionRule)
230+
dockerIgnorePathMatcher = path_matcher.NewPathMatcher(path_matcher.PathMatcherOptions{
231+
BasePath: filepath.Join(giterminismMgr.RelativeToGitProjectDir(), contextGitSubDir),
232+
DockerignorePatterns: dockerIgnorePatterns,
233+
})
234+
}
235+
236+
return dockerIgnorePathMatcher, nil
237+
}

0 commit comments

Comments
 (0)