Skip to content

Commit

Permalink
Parse build-args for empty values (#517)
Browse files Browse the repository at this point in the history
* Parse build-args for empty values
* Split for first equal sign in build-args
* Honor empty values in build-args

Fixes: #514

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed May 5, 2023
1 parent 9a221ca commit 43a5017
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
10 changes: 8 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ By default, the Containerfile / Dockerfile outputted by Ansible Builder contains
$ ansible-builder build --build-arg FOO=bar
To use different build arguments, you can specify ``--build-arg`` multiple times:

.. code::
$ ansible-builder build --build-arg FOO=bar --build-arg SIMPLE=sample
To use a custom base image:

.. code::
Expand Down Expand Up @@ -249,7 +255,7 @@ Examples

The example in ``test/data/pytz`` requires the ``awx.awx`` collection in
the execution environment definition. The lookup plugin
``awx.awx.tower_schedule_rrule`` requires the PyPI ``pytz`` and another
``awx.awx.schedule_rrule`` requires the PyPI ``pytz`` and another
library to work. If ``test/data/pytz/execution-environment.yml`` file is
given to the ``ansible-builder build`` command, then it will install the
collection inside the image, read ``requirements.txt`` inside of the
Expand All @@ -269,7 +275,7 @@ the private data directory.
The ``awx.awx`` collection is a subset of content included in the default
AWX execution environment. More details can be found at the
`awx-ee <https://github.com/ansible/awx-ee>`__ repository.
`awx-ee <https://github.com/ansible/awx-ee>`_ repository.


Deprecated Features
Expand Down
6 changes: 4 additions & 2 deletions src/ansible_builder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ def parse_args(args=sys.argv[1:]):

class BuildArgAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
key, *value = values.split('=')
key, sep, value = values.partition("=")
attr = getattr(namespace, self.dest)

# None signifies that the build-arg will come from the environment.
# This is currently only supported by Docker. Podman will treat any
# usage of the $VALUE as a literal string.
if value:
attr[key] = value[0]
attr[key] = value
elif sep == '=' and value == '':
attr[key] = ''
else:
attr[key] = None
30 changes: 30 additions & 0 deletions test/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ def test_custom_ansible_galaxy_cli_role_opts(exec_env_definition_file, tmp_path)
assert aee.build_args == {'ANSIBLE_GALAXY_CLI_ROLE_OPTS': '--ignore-errors'}


def test_build_args_empty_value(exec_env_definition_file, tmp_path):
content = {'version': 3}
path = str(exec_env_definition_file(content=content))

aee = prepare(['build', '-f', path, '--build-arg', 'ANSIBLE_GALAXY_CLI_ROLE_OPTS=', '-c', str(tmp_path)])
assert aee.build_args == {'ANSIBLE_GALAXY_CLI_ROLE_OPTS': ''}


def test_build_args_no_trailing_equal(exec_env_definition_file, tmp_path):
content = {'version': 3}
path = str(exec_env_definition_file(content=content))

aee = prepare(['build', '-f', path, '--build-arg', 'ANSIBLE_GALAXY_CLI_ROLE_OPTS', '-c', str(tmp_path)])
assert aee.build_args == {'ANSIBLE_GALAXY_CLI_ROLE_OPTS': None}


def test_build_args_multiple_equal_sign_value(exec_env_definition_file, tmp_path):
content = {'version': 3}
path = str(exec_env_definition_file(content=content))

aee = prepare(['build', '-f', path,
'--build-arg', 'ANSIBLE_GALAXY_CLI_ROLE_OPTS=',
'--build-arg', 'PYTHON_CONFIG_SETTINGS=--config-setting=--global-option=--tag-build=SUFFIX',
'-c', str(tmp_path)])
assert aee.build_args == {
'PYTHON_CONFIG_SETTINGS': '--config-setting=--global-option=--tag-build=SUFFIX',
'ANSIBLE_GALAXY_CLI_ROLE_OPTS': ''
}


def test_build_context(good_exec_env_definition_path, tmp_path):
path = str(good_exec_env_definition_path)
build_context = str(tmp_path)
Expand Down

0 comments on commit 43a5017

Please sign in to comment.