66import tempfile
77from pathlib import Path
88import time
9- import timeit
109import datetime
1110from collections import deque , namedtuple
1211import shutil
@@ -73,15 +72,6 @@ def _is_image_pushed(msg: dict[str, Any]):
7372 "total" ]
7473
7574
76- def _pull_image_timing (repo : str , tag : str ) -> tuple [str , str , str , float ]:
77- client = docker .from_env ()
78- logger .info ("Pulling the Docker image {}:{} ..." , repo , tag )
79- seconds = timeit .timeit (
80- lambda : client .images .pull (repo , tag ), timer = time .perf_counter_ns , number = 1
81- ) / 1E9
82- return repo , tag , "pull" , seconds
83-
84-
8575def _ignore_socket (dir_ , files ):
8676 dir_ = Path (dir_ )
8777 return [file for file in files if (dir_ / file ).is_socket ()]
@@ -244,7 +234,12 @@ def _copy_ssh(self, copy_ssh_to: str):
244234 shutil .copytree (ssh_src , ssh_dst , ignore = _ignore_socket )
245235 logger .info ("~/.ssh has been copied to {}" , ssh_dst )
246236
247- def build (self , tag_build : str = None , copy_ssh_to : str = "" ) -> DockerAction :
237+ def build (
238+ self ,
239+ tag_build : str = None ,
240+ copy_ssh_to : str = "" ,
241+ builder : str = "Docker"
242+ ) -> DockerAction :
248243 """Build the Docker image.
249244
250245 :param tag_build: The tag of the Docker image to build.
@@ -266,19 +261,23 @@ def build(self, tag_build: str = None, copy_ssh_to: str = "") -> DockerAction:
266261 tag_build = "latest"
267262 logger .info ("Building the Docker image {}:{} ..." , self ._name , tag_build )
268263 self ._update_base_tag (tag_build )
269- client = docker .APIClient (base_url = "unix://var/run/docker.sock" )
270264 try :
271- for msg in client .build (
272- path = str (self ._path ),
273- tag = f"{ self ._name } :{ tag_build } " ,
274- rm = True ,
275- pull = self .is_root (),
276- cache_from = None ,
277- decode = True
278- ):
279- if "stream" in msg :
280- print (msg ["stream" ], end = "" )
281- docker .from_env ().images .get (f"{ self ._name } :{ tag_build } " )
265+ if builder == "Docker" :
266+ for msg in docker .APIClient (base_url = "unix://var/run/docker.sock"
267+ ).build (
268+ path = str (self ._path ),
269+ tag = f"{ self ._name } :{ tag_build } " ,
270+ rm = True ,
271+ pull = self .is_root (),
272+ cache_from = None ,
273+ decode = True
274+ ):
275+ if "stream" in msg :
276+ print (msg ["stream" ], end = "" )
277+ docker .from_env ().images .get (f"{ self ._name } :{ tag_build } " )
278+ elif builder == "Kaniko" :
279+ cmd = f"/kaniko/executor -c { self ._path } -d { self ._name } :{ tag_build } "
280+ sp .run (cmd , shell = True , check = True )
282281 except docker .errors .BuildError as err :
283282 return DockerAction (
284283 succeed = False ,
@@ -380,7 +379,8 @@ class DockerImageBuilder:
380379 def __init__ (
381380 self ,
382381 branch_urls : Union [dict [str , list [str ]], str , Path ],
383- branch_fallback : str = "dev"
382+ branch_fallback : str = "dev" ,
383+ builder : str = "Docker" ,
384384 ):
385385 if isinstance (branch_urls , (str , Path )):
386386 with open (branch_urls , "r" ) as fin :
@@ -393,6 +393,7 @@ def __init__(
393393 self ._roots = set ()
394394 self .failures = []
395395 self ._servers = set ()
396+ self ._builder = builder
396397
397398 def _record_docker_servers (self , deps : deque [DockerImage ]):
398399 for dep in deps :
@@ -614,7 +615,7 @@ def _build_image_node(
614615 branch = node .branch ,
615616 branch_fallback = self ._branch_fallback ,
616617 repo_path = self ._repo_path
617- ).build (tag_build = tag_build , copy_ssh_to = copy_ssh_to )
618+ ).build (tag_build = tag_build , copy_ssh_to = copy_ssh_to , builder = self . _builder )
618619 attr = self ._graph .nodes [node ]
619620 attr ["build_succeed" ] = succeed
620621 attr ["build_err_msg" ] = err_msg
@@ -623,7 +624,7 @@ def _build_image_node(
623624 if not succeed :
624625 return
625626 self ._tag_image (name , tag , attr )
626- if push :
627+ if self . _builder == "Docker" and push :
627628 self ._push_images (name , attr ["action_time" ])
628629
629630 @staticmethod
0 commit comments