20
20
from typing import Any , Dict , List , Optional , Tuple , Union
21
21
from pathlib import Path
22
22
from string import Template
23
+ from re import search as re_search , IGNORECASE as re_IGNORECASE
23
24
24
25
from dataclasses import dataclass
25
26
from yamldataclassconfig .config import YamlDataClassConfig
@@ -424,23 +425,41 @@ def BuildImage(
424
425
if target not in [None , '' ]:
425
426
cmd += [f"--target={ target } " ]
426
427
427
- cpath = Path (collection .replace ("/" , "-" ))
428
- dpath = cpath / dockerfile
428
+ def _getCollectionAndDockerfilePaths (collection , dockerfile ):
429
+ collectionPath = Path (collection .replace ("/" , "-" ))
430
+ dockerfilePath = collectionPath / dockerfile
429
431
430
- if dpath .is_dir ():
431
- cpath = dpath
432
- dpath = dpath / 'Dockerfile'
433
- else :
434
- dpath = cpath / f"{ dockerfile } .dockerfile"
435
- cmd += ["-f" , str (dpath )]
432
+ if dockerfilePath .is_dir ():
433
+ contextPath = dockerfilePath
434
+ dockerfilePath = contextPath / 'Dockerfile'
435
+ else :
436
+ contextPath = collectionPath
437
+ dockerfilePath = contextPath / f"{ dockerfile } .dockerfile"
438
+
439
+ if not dockerfilePath .exists ():
440
+ raise Exception (f"Dockerfile <{ dockerfilePath } > does not exist!" )
441
+
442
+ return (contextPath , dockerfilePath )
443
+
444
+ (contextPath , dockerfilePath ) = _getCollectionAndDockerfilePaths (collection , dockerfile )
436
445
437
- if not dpath . exists () :
438
- raise Exception ( f"Dockerfile < { dpath } > does not exist!" )
446
+ if len ( dockerfilePath . suffix ) != 0 :
447
+ cmd += [ "-f" , str ( dockerfilePath )]
439
448
440
- cmd += [str (cpath )]
449
+ cmd += [str (contextPath )]
441
450
442
451
_exec (args = cmd , dry = dry , collapse = f"[Build] Build { imageName } " )
443
452
453
+ with dockerfilePath .open ('r' ) as rfptr :
454
+ for line in rfptr :
455
+ if re_search ('FROM scratch AS version' , line , re_IGNORECASE ):
456
+ _exec (
457
+ args = ['docker' , 'build' , '--target' , 'version' , '-o' , 'dist' , str (contextPath )],
458
+ dry = dry ,
459
+ collapse = f"[Build] Version { imageName } "
460
+ )
461
+ break
462
+
444
463
if test :
445
464
TestImage (
446
465
f"{ img } { (f'#{ withDir } ' if withDir is not None else '' )} " ,
@@ -578,8 +597,16 @@ def PushImage(
578
597
* ``#A``: architecture
579
598
* ``#C``: collection
580
599
"""
581
- def dpush (imgName ):
582
- _exec (args = ["docker" , "push" , imgName ], dry = dry , collapse = f"Push { imgName } " )
600
+ def dpush (args : List [str ]):
601
+ _exec (args = ["docker" , "push" ]+ args , dry = dry , collapse = f"Push { ' ' .join (args )} " )
602
+
603
+ def dtag (imgName : str , tags : List [str ]):
604
+ for tag in tags :
605
+ _exec (
606
+ args = ["docker" , "tag" , imgName , tag ],
607
+ dry = dry ,
608
+ collapse = f"Tag { tag } " ,
609
+ )
583
610
584
611
mirrors = [] if mirror is None else [mirror ] if isinstance (mirror , str ) else mirror
585
612
@@ -589,17 +616,23 @@ def dpush(imgName):
589
616
# There, it denotes keywords for replacement.
590
617
img = rimg .split ('#' )[0 ]
591
618
imageName = f"{ registry } /{ architecture } /{ collection } /{ img } "
592
- dpush (imageName )
619
+ dpush ([imageName ])
620
+
621
+ print ('\n Checking version file...' )
622
+ versionString = None
623
+ versionFile = Path ('dist' ) / f'hdlc.{ rimg if rimg [0 :4 ] != "pkg/" else rimg [4 :]} .version'
624
+ if versionFile .exists ():
625
+ with versionFile .open ('r' ) as rfptr :
626
+ versionString = rfptr .read ().strip ()
627
+ print (f'{ rimg } : { versionString } ' )
628
+
593
629
for mirror in mirrors :
594
- mimg = (
595
- img .replace ("/" , ":" , 1 ).replace ("/" , "--" )
596
- if mirror .startswith ("docker.io" )
597
- else img
598
- )
630
+ isDocker = mirror .startswith ("docker.io" )
631
+ mimg = img .replace ("/" , ":" , 1 ).replace ("/" , "--" ) if isDocker else img
599
632
mirrorName = f"{ mirror .replace ('#A' , architecture ).replace ('#C' , collection )} /{ mimg } "
600
- _exec (
601
- args = [ "docker" , "tag" , imageName , mirrorName ],
602
- dry = dry ,
603
- collapse = f"Tag { imageName } { mirrorName } " ,
604
- )
605
- dpush (mirrorName )
633
+ if isDocker or ( versionString is None ):
634
+ dtag ( imageName , [ mirrorName ])
635
+ dpush ([ mirrorName ])
636
+ continue
637
+ dtag ( imageName , [ mirrorName , f' { mirrorName } : { versionString } ' ] )
638
+ dpush ([ '--all-tags' , mirrorName ] )
0 commit comments