@@ -93,17 +93,26 @@ var PushContainerfileParamsConfig = map[string]common.Parameter{
9393 Usage : "Write digested image reference of the pushed Containerfile image into this file." ,
9494 Required : false ,
9595 },
96+ "alternative-filename" : {
97+ Name : "alternative-filename" ,
98+ ShortName : "n" ,
99+ EnvVarName : "KBC_PUSH_CONTAINERFILE_ALTERNATIVE_FILENAME" ,
100+ TypeKind : reflect .String ,
101+ Usage : "Alternative file name in the artifact image, e.g. Dockerfile." ,
102+ Required : false ,
103+ },
96104}
97105
98106type PushContainerfileParams struct {
99- ImageUrl string `paramName:"image-url"`
100- ImageDigest string `paramName:"image-digest"`
101- Containerfile string `paramName:"containerfile"`
102- Context string `paramName:"context"`
103- TagSuffix string `paramName:"tag-suffix"`
104- ArtifactType string `paramName:"artifact-type"`
105- Source string `paramName:"source"`
106- ResultPathImageRef string `paramName:"result-path-image-ref"`
107+ ImageUrl string `paramName:"image-url"`
108+ ImageDigest string `paramName:"image-digest"`
109+ Containerfile string `paramName:"containerfile"`
110+ Context string `paramName:"context"`
111+ TagSuffix string `paramName:"tag-suffix"`
112+ ArtifactType string `paramName:"artifact-type"`
113+ Source string `paramName:"source"`
114+ ResultPathImageRef string `paramName:"result-path-image-ref"`
115+ AlternativeFilename string `paramName:"alternative-filename"`
107116}
108117
109118type PushContainerfileResults struct {
@@ -207,7 +216,31 @@ func (c *PushContainerfile) Run() error {
207216 return fmt .Errorf ("Error on getting absolute path of %s: %w" , containerfilePath , err )
208217 }
209218
210- os .Chdir (filepath .Dir (absContainerfilePath ))
219+ var pushFilename string
220+ var workDir string
221+
222+ if c .Params .AlternativeFilename != "" {
223+ pushFilename = filepath .Base (c .Params .AlternativeFilename )
224+ workDir , err = os .MkdirTemp ("" , "push-containerfile-" )
225+ if err != nil {
226+ return fmt .Errorf ("Error on creating temporary directory: %w" , err )
227+ }
228+ defer os .RemoveAll (workDir )
229+ content , err := os .ReadFile (absContainerfilePath )
230+ if err != nil {
231+ return fmt .Errorf ("Error on reading file %s: %w" , absContainerfilePath , err )
232+ }
233+ if err := os .WriteFile (filepath .Join (workDir , pushFilename ), content , 0644 ); err != nil {
234+ return fmt .Errorf ("Error on writing file: %w" , err )
235+ }
236+ } else {
237+ pushFilename = filepath .Base (absContainerfilePath )
238+ workDir = filepath .Dir (absContainerfilePath )
239+ }
240+
241+ if err := os .Chdir (workDir ); err != nil {
242+ return fmt .Errorf ("Error on changing directory to %s: %w" , workDir , err )
243+ }
211244 defer os .Chdir (curDir )
212245
213246 stdout , _ , err := c .CliWrappers .OrasCli .Push (& cliwrappers.OrasPushArgs {
@@ -216,7 +249,7 @@ func (c *PushContainerfile) Run() error {
216249 Format : "go-template" ,
217250 Template : "{{.reference}}" ,
218251 DestinationImage : fmt .Sprintf ("%s:%s" , c .imageName , tag ),
219- FileName : filepath . Base ( absContainerfilePath ) ,
252+ FileName : pushFilename ,
220253 })
221254 if err != nil {
222255 return fmt .Errorf ("Error on pushing Containerfile %s: %w" , containerfilePath , err )
@@ -262,6 +295,14 @@ func (c *PushContainerfile) validateParams() error {
262295 return fmt .Errorf ("Tag suffix includes invalid characters or exceeds the max length of 57 characters." )
263296 }
264297
298+ altFilename := c .Params .AlternativeFilename
299+ if strings .Contains (altFilename , "/" ) {
300+ return fmt .Errorf ("Path is included in alternative file name '%s'" , altFilename )
301+ }
302+ if len (altFilename ) > 100 {
303+ return fmt .Errorf ("Alternative file name exceeds 100 characters." )
304+ }
305+
265306 return nil
266307}
267308
@@ -276,4 +317,7 @@ func (c *PushContainerfile) logParams() {
276317 if c .Params .ResultPathImageRef != "" {
277318 l .Logger .Infof ("[param] Image Reference result file: %s" , c .Params .ResultPathImageRef )
278319 }
320+ if c .Params .AlternativeFilename != "" {
321+ l .Logger .Infof ("[param] Alternative file name: %s" , c .Params .AlternativeFilename )
322+ }
279323}
0 commit comments