Repository: ansible/ansible-builder

Issue:
RFE: Add no-cache option to the builder
When doing some build automatically with the same name it would be nice to have the no-cache option.

Discussion:

Patch:
diff --git a/ansible_builder/cli.py b/ansible_builder/cli.py
index 1d647e2..5e67cea 100644
--- a/ansible_builder/cli.py
+++ b/ansible_builder/cli.py
@@ -124,6 +124,12 @@ def add_container_options(parser):
              'Internally ansible-builder makes use of {0}.'.format(
              ', '.join(constants.build_arg_defaults.keys())))
 
+    build_command_parser.add_argument(
+        '--no-cache',
+        action='store_true',
+        help='Do not use cache when building the image',
+    )
+
     for p in [create_command_parser, build_command_parser]:
 
         p.add_argument('-f', '--file',
diff --git a/ansible_builder/main.py b/ansible_builder/main.py
index 758b3ac..0b4dd6b 100644
--- a/ansible_builder/main.py
+++ b/ansible_builder/main.py
@@ -39,6 +39,7 @@ class AnsibleBuilder:
                  tag=constants.default_tag,
                  container_runtime=constants.default_container_runtime,
                  output_filename=None,
+                 no_cache=False,
                  verbosity=constants.default_verbosity):
         self.action = action
         self.definition = UserDefinition(filename=filename)
@@ -49,6 +50,7 @@ class AnsibleBuilder:
             build_context, constants.user_content_subfolder)
         self.container_runtime = container_runtime
         self.build_args = build_args or {}
+        self.no_cache = no_cache
         self.containerfile = Containerfile(
             definition=self.definition,
             build_context=self.build_context,
@@ -111,6 +113,9 @@ class AnsibleBuilder:
 
         command.append(self.build_context)
 
+        if self.no_cache:
+            command.append('--no-cache')
+
         return command
 
     def build(self):


Test patch:
diff --git a/test/unit/test_cli.py b/test/unit/test_cli.py
index 2685037..d252526 100644
--- a/test/unit/test_cli.py
+++ b/test/unit/test_cli.py
@@ -44,3 +44,14 @@ def test_build_context(good_exec_env_definition_path, tmp_path):
     # test without 'container' sub-command (defaulting to 'container')
     aee = prepare(['build', '-f', path, '-c', build_context])
     assert aee.build_context == build_context
+
+
+def test_build_no_cache(good_exec_env_definition_path, tmp_path):
+    path = str(good_exec_env_definition_path)
+    build_context = str(tmp_path)
+
+    aee = prepare(['container', 'build', '-f', path, '-c', build_context])
+    aee_no_cache = prepare(['container', 'build', '-f', path, '-c', build_context, '--no-cache'])
+
+    assert '--no-cache' not in aee.build_command
+    assert '--no-cache' in aee_no_cache.build_command