@@ -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