@@ -81,6 +81,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
8181 testBakeMultiPlatform ,
8282 testBakeCheckCallOutput ,
8383 testBakeExtraHosts ,
84+ testBakeFileFromEnvironment ,
8485}
8586
8687func testBakePrint (t * testing.T , sb integration.Sandbox ) {
@@ -2191,6 +2192,165 @@ target "default" {
21912192 require .NoError (t , err , out )
21922193}
21932194
2195+ func testBakeFileFromEnvironment (t * testing.T , sb integration.Sandbox ) {
2196+ bakeFileFirst := []byte (`
2197+ target "first" {
2198+ dockerfile-inline = "FROM scratch\nCOPY first /"
2199+ }
2200+ ` )
2201+ bakeFileSecond := []byte (`
2202+ target "second" {
2203+ dockerfile-inline = "FROM scratch\nCOPY second /"
2204+ }
2205+ ` )
2206+
2207+ t .Run ("single file" , func (t * testing.T ) {
2208+ dir := tmpdir (t ,
2209+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2210+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2211+ )
2212+ cmd := buildxCmd (sb ,
2213+ withDir (dir ),
2214+ withArgs ("bake" , "--progress=plain" , "first" ),
2215+ withEnv ("BUILDX_BAKE_FILE=first.hcl" ))
2216+
2217+ dt , err := cmd .CombinedOutput ()
2218+ require .NoError (t , err , string (dt ))
2219+ require .Contains (t , string (dt ), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env` )
2220+ require .Contains (t , string (dt ), `#1 reading first.hcl` )
2221+ })
2222+
2223+ t .Run ("single file, default ignored if present" , func (t * testing.T ) {
2224+ dir := tmpdir (t ,
2225+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2226+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2227+ fstest .CreateFile ("docker-bake.hcl" , []byte ("invalid bake file" ), 0600 ),
2228+ )
2229+ cmd := buildxCmd (sb ,
2230+ withDir (dir ),
2231+ withArgs ("bake" , "--progress=plain" , "first" ),
2232+ withEnv ("BUILDX_BAKE_FILE=first.hcl" ))
2233+
2234+ dt , err := cmd .CombinedOutput ()
2235+ require .NoError (t , err , string (dt ))
2236+ require .Contains (t , string (dt ), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env` )
2237+ require .Contains (t , string (dt ), `#1 reading first.hcl` )
2238+ require .NotContains (t , string (dt ), "docker-bake.hcl" )
2239+ })
2240+
2241+ t .Run ("multiple files" , func (t * testing.T ) {
2242+ dir := tmpdir (t ,
2243+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2244+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2245+ fstest .CreateFile ("second.hcl" , bakeFileSecond , 0600 ),
2246+ fstest .CreateFile ("second" , []byte ("second" ), 0600 ),
2247+ )
2248+
2249+ cmd := buildxCmd (sb ,
2250+ withDir (dir ),
2251+ withArgs ("bake" , "--progress=plain" , "second" , "first" ),
2252+ withEnv ("BUILDX_BAKE_FILE=first.hcl" + string (os .PathListSeparator )+ "second.hcl" ))
2253+ dt , err := cmd .CombinedOutput ()
2254+ require .NoError (t , err , string (dt ))
2255+ require .Contains (t , string (dt ), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env` )
2256+ require .Contains (t , string (dt ), `#1 reading first.hcl` )
2257+ require .Contains (t , string (dt ), `#1 reading second.hcl` )
2258+ })
2259+
2260+ t .Run ("multiple files, custom separator" , func (t * testing.T ) {
2261+ dir := tmpdir (t ,
2262+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2263+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2264+ fstest .CreateFile ("second.hcl" , bakeFileSecond , 0600 ),
2265+ fstest .CreateFile ("second" , []byte ("second" ), 0600 ),
2266+ )
2267+
2268+ cmd := buildxCmd (sb ,
2269+ withDir (dir ),
2270+ withArgs ("bake" , "--progress=plain" , "second" , "first" ),
2271+ withEnv ("BUILDX_BAKE_PATH_SEPARATOR=@" , "BUILDX_BAKE_FILE=first.hcl@second.hcl" ))
2272+
2273+ dt , err := cmd .CombinedOutput ()
2274+ require .NoError (t , err , string (dt ))
2275+ require .Contains (t , string (dt ), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env` )
2276+ require .Contains (t , string (dt ), `#1 reading first.hcl` )
2277+ require .Contains (t , string (dt ), `#1 reading second.hcl` )
2278+ })
2279+
2280+ t .Run ("multiple files, one STDIN" , func (t * testing.T ) {
2281+ dir := tmpdir (t ,
2282+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2283+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2284+ fstest .CreateFile ("second" , []byte ("second" ), 0600 ),
2285+ )
2286+
2287+ cmd := buildxCmd (sb ,
2288+ withDir (dir ),
2289+ withArgs ("bake" , "--progress=plain" , "second" , "first" ),
2290+ withEnv ("BUILDX_BAKE_FILE=first.hcl" + string (os .PathListSeparator )+ "-" ))
2291+ w , err := cmd .StdinPipe ()
2292+ require .NoError (t , err )
2293+ go func () {
2294+ defer w .Close ()
2295+ w .Write (bakeFileSecond )
2296+ }()
2297+
2298+ dt , err := cmd .CombinedOutput ()
2299+ require .NoError (t , err , string (dt ))
2300+ require .Contains (t , string (dt ), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env` )
2301+ require .Contains (t , string (dt ), `#1 reading first.hcl` )
2302+ require .Contains (t , string (dt ), `#1 reading from stdin` )
2303+ })
2304+
2305+ t .Run ("env ignored if file arg passed" , func (t * testing.T ) {
2306+ dir := tmpdir (t ,
2307+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2308+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2309+ fstest .CreateFile ("second.hcl" , bakeFileSecond , 0600 ),
2310+ )
2311+ cmd := buildxCmd (sb ,
2312+ withDir (dir ),
2313+ withArgs ("bake" , "--progress=plain" , "-f" , "first.hcl" , "first" , "second" ),
2314+ withEnv ("BUILDX_BAKE_FILE=second.hcl" ))
2315+
2316+ dt , err := cmd .CombinedOutput ()
2317+ require .Error (t , err , string (dt ))
2318+ require .Contains (t , string (dt ), "failed to find target second" )
2319+ })
2320+
2321+ t .Run ("file does not exist" , func (t * testing.T ) {
2322+ dir := tmpdir (t ,
2323+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2324+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2325+ )
2326+ cmd := buildxCmd (sb ,
2327+ withDir (dir ),
2328+ withArgs ("bake" , "--progress=plain" , "first" ),
2329+ withEnv ("BUILDX_BAKE_FILE=wrong.hcl" ))
2330+
2331+ dt , err := cmd .CombinedOutput ()
2332+ require .Error (t , err , string (dt ))
2333+ require .Contains (t , string (dt ), "wrong.hcl: no such file or directory" )
2334+ })
2335+
2336+ for kind , val := range map [string ]string {"missing" : "" , "whitespace" : " " } {
2337+ t .Run (kind + " value ignored" , func (t * testing.T ) {
2338+ dir := tmpdir (t ,
2339+ fstest .CreateFile ("first.hcl" , bakeFileFirst , 0600 ),
2340+ fstest .CreateFile ("first" , []byte ("first" ), 0600 ),
2341+ )
2342+ cmd := buildxCmd (sb ,
2343+ withDir (dir ),
2344+ withArgs ("bake" , "--progress=plain" , "first" ),
2345+ withEnv (fmt .Sprintf ("BUILDX_BAKE_FILE=%s" , val )))
2346+
2347+ dt , err := cmd .CombinedOutput ()
2348+ require .Error (t , err , string (dt ))
2349+ require .Contains (t , string (dt ), "couldn't find a bake definition" )
2350+ })
2351+ }
2352+ }
2353+
21942354func writeTempPrivateKey (fp string ) error {
21952355 privateKey , err := rsa .GenerateKey (rand .Reader , 2048 )
21962356 if err != nil {
0 commit comments