@@ -40,6 +40,7 @@ import (
40
40
"github.com/moby/buildkit/util/suggest"
41
41
"github.com/moby/buildkit/util/system"
42
42
dockerspec "github.com/moby/docker-image-spec/specs-go/v1"
43
+ "github.com/moby/patternmatcher"
43
44
"github.com/moby/sys/signal"
44
45
digest "github.com/opencontainers/go-digest"
45
46
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -594,6 +595,20 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
594
595
buildContext := & mutableOutput {}
595
596
ctxPaths := map [string ]struct {}{}
596
597
598
+ var dockerIgnoreMatcher * patternmatcher.PatternMatcher
599
+ if opt .Client != nil && opt .Client .CopyIgnoredCheckEnabled {
600
+ dockerIgnorePatterns , err := opt .Client .DockerIgnorePatterns (ctx )
601
+ if err != nil {
602
+ return nil , err
603
+ }
604
+ if len (dockerIgnorePatterns ) > 0 {
605
+ dockerIgnoreMatcher , err = patternmatcher .New (dockerIgnorePatterns )
606
+ if err != nil {
607
+ return nil , err
608
+ }
609
+ }
610
+ }
611
+
597
612
for _ , d := range allDispatchStates .states {
598
613
if ! opt .AllStages {
599
614
if _ , ok := allReachable [d ]; ! ok || d .noinit {
@@ -634,24 +649,27 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
634
649
return nil , parser .WithLocation (err , d .stage .Location )
635
650
}
636
651
}
652
+
637
653
d .state = d .state .Network (opt .NetworkMode )
654
+
638
655
opt := dispatchOpt {
639
- allDispatchStates : allDispatchStates ,
640
- metaArgs : optMetaArgs ,
641
- buildArgValues : opt .BuildArgs ,
642
- shlex : shlex ,
643
- buildContext : llb .NewState (buildContext ),
644
- proxyEnv : proxyEnv ,
645
- cacheIDNamespace : opt .CacheIDNamespace ,
646
- buildPlatforms : platformOpt .buildPlatforms ,
647
- targetPlatform : platformOpt .targetPlatform ,
648
- extraHosts : opt .ExtraHosts ,
649
- shmSize : opt .ShmSize ,
650
- ulimit : opt .Ulimits ,
651
- cgroupParent : opt .CgroupParent ,
652
- llbCaps : opt .LLBCaps ,
653
- sourceMap : opt .SourceMap ,
654
- lint : lint ,
656
+ allDispatchStates : allDispatchStates ,
657
+ metaArgs : optMetaArgs ,
658
+ buildArgValues : opt .BuildArgs ,
659
+ shlex : shlex ,
660
+ buildContext : llb .NewState (buildContext ),
661
+ proxyEnv : proxyEnv ,
662
+ cacheIDNamespace : opt .CacheIDNamespace ,
663
+ buildPlatforms : platformOpt .buildPlatforms ,
664
+ targetPlatform : platformOpt .targetPlatform ,
665
+ extraHosts : opt .ExtraHosts ,
666
+ shmSize : opt .ShmSize ,
667
+ ulimit : opt .Ulimits ,
668
+ cgroupParent : opt .CgroupParent ,
669
+ llbCaps : opt .LLBCaps ,
670
+ sourceMap : opt .SourceMap ,
671
+ lint : lint ,
672
+ dockerIgnoreMatcher : dockerIgnoreMatcher ,
655
673
}
656
674
657
675
if err = dispatchOnBuildTriggers (d , d .image .Config .OnBuild , opt ); err != nil {
@@ -810,22 +828,23 @@ func toCommand(ic instructions.Command, allDispatchStates *dispatchStates) (comm
810
828
}
811
829
812
830
type dispatchOpt struct {
813
- allDispatchStates * dispatchStates
814
- metaArgs []instructions.KeyValuePairOptional
815
- buildArgValues map [string ]string
816
- shlex * shell.Lex
817
- buildContext llb.State
818
- proxyEnv * llb.ProxyEnv
819
- cacheIDNamespace string
820
- targetPlatform ocispecs.Platform
821
- buildPlatforms []ocispecs.Platform
822
- extraHosts []llb.HostIP
823
- shmSize int64
824
- ulimit []pb.Ulimit
825
- cgroupParent string
826
- llbCaps * apicaps.CapSet
827
- sourceMap * llb.SourceMap
828
- lint * linter.Linter
831
+ allDispatchStates * dispatchStates
832
+ metaArgs []instructions.KeyValuePairOptional
833
+ buildArgValues map [string ]string
834
+ shlex * shell.Lex
835
+ buildContext llb.State
836
+ proxyEnv * llb.ProxyEnv
837
+ cacheIDNamespace string
838
+ targetPlatform ocispecs.Platform
839
+ buildPlatforms []ocispecs.Platform
840
+ extraHosts []llb.HostIP
841
+ shmSize int64
842
+ ulimit []pb.Ulimit
843
+ cgroupParent string
844
+ llbCaps * apicaps.CapSet
845
+ sourceMap * llb.SourceMap
846
+ lint * linter.Linter
847
+ dockerIgnoreMatcher * patternmatcher.PatternMatcher
829
848
}
830
849
831
850
func getEnv (state llb.State ) shell.EnvGetter {
@@ -912,6 +931,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
912
931
keepGitDir : c .KeepGitDir ,
913
932
checksum : checksum ,
914
933
location : c .Location (),
934
+ ignoreMatcher : opt .dockerIgnoreMatcher ,
915
935
opt : opt ,
916
936
})
917
937
}
@@ -946,12 +966,15 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
946
966
err = dispatchArg (d , c , & opt )
947
967
case * instructions.CopyCommand :
948
968
l := opt .buildContext
969
+ var ignoreMatcher * patternmatcher.PatternMatcher
949
970
if len (cmd .sources ) != 0 {
950
971
src := cmd .sources [0 ]
951
972
if ! src .noinit {
952
973
return errors .Errorf ("cannot copy from stage %q, it needs to be defined before current stage %q" , c .From , d .stageName )
953
974
}
954
975
l = src .state
976
+ } else {
977
+ ignoreMatcher = opt .dockerIgnoreMatcher
955
978
}
956
979
err = dispatchCopy (d , copyConfig {
957
980
params : c .SourcesAndDest ,
@@ -964,6 +987,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
964
987
link : c .Link ,
965
988
parents : c .Parents ,
966
989
location : c .Location (),
990
+ ignoreMatcher : ignoreMatcher ,
967
991
opt : opt ,
968
992
})
969
993
if err == nil {
@@ -1442,6 +1466,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
1442
1466
a = a .Copy (st , f , dest , opts ... )
1443
1467
}
1444
1468
} else {
1469
+ validateCopySourcePath (src , & cfg )
1445
1470
var patterns []string
1446
1471
if cfg .parents {
1447
1472
// detect optional pivot point
@@ -1558,6 +1583,7 @@ type copyConfig struct {
1558
1583
checksum digest.Digest
1559
1584
parents bool
1560
1585
location []parser.Range
1586
+ ignoreMatcher * patternmatcher.PatternMatcher
1561
1587
opt dispatchOpt
1562
1588
}
1563
1589
@@ -1871,6 +1897,27 @@ func addReachableStages(s *dispatchState, stages map[*dispatchState]struct{}) {
1871
1897
}
1872
1898
}
1873
1899
1900
+ func validateCopySourcePath (src string , cfg * copyConfig ) error {
1901
+ if cfg .ignoreMatcher == nil {
1902
+ return nil
1903
+ }
1904
+ cmd := "Copy"
1905
+ if cfg .isAddCommand {
1906
+ cmd = "Add"
1907
+ }
1908
+
1909
+ ok , err := cfg .ignoreMatcher .MatchesOrParentMatches (src )
1910
+ if err != nil {
1911
+ return err
1912
+ }
1913
+ if ok {
1914
+ msg := linter .RuleCopyIgnoredFile .Format (cmd , src )
1915
+ cfg .opt .lint .Run (& linter .RuleCopyIgnoredFile , cfg .location , msg )
1916
+ }
1917
+
1918
+ return nil
1919
+ }
1920
+
1874
1921
func validateCircularDependency (states []* dispatchState ) error {
1875
1922
var visit func (* dispatchState , []instructions.Command ) []instructions.Command
1876
1923
if states == nil {
0 commit comments