@@ -127,6 +127,7 @@ var allTests = integration.TestFuncs(
127
127
testEnvEmptyFormatting ,
128
128
testCacheMultiPlatformImportExport ,
129
129
testOnBuildCleared ,
130
+ testOnBuildWithChildStage ,
130
131
testOnBuildInheritedStageRun ,
131
132
testOnBuildInheritedStageWithFrom ,
132
133
testOnBuildNewDeps ,
@@ -5013,6 +5014,96 @@ ONBUILD RUN mkdir \out && echo 11>> \out\foo
5013
5014
require .Equal (t , integration .UnixOrWindows ("11" , "11\r \n " ), string (dt ))
5014
5015
}
5015
5016
5017
+ // testOnBuildWithChildStage tests that ONBUILD rules from the parent image do
5018
+ // not run again if another stage inherits from current stage.
5019
+ // moby/buildkit#5578
5020
+ func testOnBuildWithChildStage (t * testing.T , sb integration.Sandbox ) {
5021
+ integration .SkipOnPlatform (t , "windows" )
5022
+ workers .CheckFeatureCompat (t , sb , workers .FeatureDirectPush )
5023
+ f := getFrontend (t , sb )
5024
+
5025
+ registry , err := sb .NewRegistry ()
5026
+ if errors .Is (err , integration .ErrRequirements ) {
5027
+ t .Skip (err .Error ())
5028
+ }
5029
+ require .NoError (t , err )
5030
+
5031
+ dockerfile := []byte (`
5032
+ FROM busybox
5033
+ ONBUILD RUN mkdir -p /out && echo -n yes >> /out/didrun
5034
+ ` )
5035
+
5036
+ dir := integration .Tmpdir (
5037
+ t ,
5038
+ fstest .CreateFile ("Dockerfile" , dockerfile , 0600 ),
5039
+ )
5040
+
5041
+ c , err := client .New (sb .Context (), sb .Address ())
5042
+ require .NoError (t , err )
5043
+ defer c .Close ()
5044
+
5045
+ target := registry + "/buildkit/testonbuildstage:base"
5046
+
5047
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
5048
+ Exports : []client.ExportEntry {
5049
+ {
5050
+ Type : client .ExporterImage ,
5051
+ Attrs : map [string ]string {
5052
+ "push" : "true" ,
5053
+ "name" : target ,
5054
+ },
5055
+ },
5056
+ },
5057
+ LocalMounts : map [string ]fsutil.FS {
5058
+ dockerui .DefaultLocalNameDockerfile : dir ,
5059
+ dockerui .DefaultLocalNameContext : dir ,
5060
+ },
5061
+ }, nil )
5062
+ require .NoError (t , err )
5063
+
5064
+ dockerfile = []byte (fmt .Sprintf (`
5065
+ FROM %s AS base
5066
+ RUN [ -f /out/didrun ] && touch /step1
5067
+ RUN rm /out/didrun
5068
+ RUN [ ! -f /out/didrun ] && touch /step2
5069
+
5070
+ FROM base AS child
5071
+ RUN [ ! -f /out/didrun ] && touch /step3
5072
+
5073
+ FROM scratch
5074
+ COPY --from=child /step* /
5075
+ ` , target ))
5076
+
5077
+ dir = integration .Tmpdir (
5078
+ t ,
5079
+ fstest .CreateFile ("Dockerfile" , dockerfile , 0600 ),
5080
+ )
5081
+ destDir := t .TempDir ()
5082
+
5083
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
5084
+ Exports : []client.ExportEntry {
5085
+ {
5086
+ Type : client .ExporterLocal ,
5087
+ OutputDir : destDir ,
5088
+ },
5089
+ },
5090
+ LocalMounts : map [string ]fsutil.FS {
5091
+ dockerui .DefaultLocalNameDockerfile : dir ,
5092
+ dockerui .DefaultLocalNameContext : dir ,
5093
+ },
5094
+ }, nil )
5095
+ require .NoError (t , err )
5096
+
5097
+ _ , err = os .Stat (filepath .Join (destDir , "step1" ))
5098
+ require .NoError (t , err )
5099
+
5100
+ _ , err = os .Stat (filepath .Join (destDir , "step2" ))
5101
+ require .NoError (t , err )
5102
+
5103
+ _ , err = os .Stat (filepath .Join (destDir , "step3" ))
5104
+ require .NoError (t , err )
5105
+ }
5106
+
5016
5107
func testOnBuildNamedContext (t * testing.T , sb integration.Sandbox ) {
5017
5108
integration .SkipOnPlatform (t , "windows" )
5018
5109
workers .CheckFeatureCompat (t , sb , workers .FeatureOCIExporter , workers .FeatureOCILayout )
0 commit comments