@@ -29,6 +29,7 @@ const (
2929
3030 // Environment specific flags.
3131 containerFlagUseVolumeWD = "use-volume-wd"
32+ containerFlagRemoveImage = "remove-image"
3233)
3334
3435type containerEnv struct {
@@ -38,7 +39,8 @@ type containerEnv struct {
3839 nameprv ContainerNameProvider
3940
4041 // Runtime flags
41- useVolWD bool
42+ useVolWD bool
43+ removeImg bool
4244}
4345
4446// ContainerNameProvider provides an ability to generate a random container name
@@ -77,13 +79,25 @@ func (c *containerEnv) FlagsDefinition() OptionsList {
7779 Type : jsonschema .Boolean ,
7880 Default : false ,
7981 },
82+ & Option {
83+ Name : containerFlagRemoveImage ,
84+ Title : "Remove Image" ,
85+ Description : "Remove an image after execution of action" ,
86+ Type : jsonschema .Boolean ,
87+ Default : false ,
88+ },
8089 }
8190}
8291
8392func (c * containerEnv ) UseFlags (flags TypeOpts ) error {
8493 if v , ok := flags [containerFlagUseVolumeWD ]; ok {
8594 c .useVolWD = v .(bool )
8695 }
96+
97+ if v , ok := flags [containerFlagRemoveImage ]; ok {
98+ c .removeImg = v .(bool )
99+ }
100+
87101 return nil
88102}
89103
@@ -224,6 +238,16 @@ func (c *containerEnv) Execute(ctx context.Context, a *Action) (err error) {
224238 return err
225239 }
226240 }
241+
242+ if c .removeImg {
243+ err = c .imageRemove (ctx , a )
244+ if err != nil {
245+ log .Err ("Image remove returned an error: %v" , err )
246+ } else {
247+ cli .Println ("Image %q was successfully removed" , a .ActionDef ().Image )
248+ }
249+ }
250+
227251 return nil
228252}
229253
@@ -245,6 +269,15 @@ func (c *containerEnv) Close() error {
245269 return c .driver .Close ()
246270}
247271
272+ func (c * containerEnv ) imageRemove (ctx context.Context , a * Action ) error {
273+ _ , err := c .driver .ImageRemove (ctx , a .ActionDef ().Image , types.ImageRemoveOptions {
274+ Force : false ,
275+ PruneChildren : false ,
276+ })
277+
278+ return err
279+ }
280+
248281func (c * containerEnv ) imageEnsure (ctx context.Context , a * Action ) error {
249282 streams := a .GetInput ().IO
250283 image := a .ActionDef ().Image
0 commit comments