From a25a345a3036a7455628dc8be682ec6f78f9ccc1 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 24 Oct 2019 23:19:11 +0200 Subject: [PATCH 1/5] --force-build / --force-push --- chartpress.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/chartpress.py b/chartpress.py index 844e755..9f0985c 100755 --- a/chartpress.py +++ b/chartpress.py @@ -292,7 +292,7 @@ def _strip_identifiers_build_suffix(identifier): return re.sub(r'[-\.]\d{3,}\.\w{4,}\Z', "", identifier) -def build_images(prefix, images, tag=None, push=False, chart_version=None, skip_build=False, long=False): +def build_images(prefix, images, tag=None, push=False, force_push=False, chart_version=None, force_build=False, skip_build=False, long=False): """Build a collection of docker images Args: @@ -304,9 +304,14 @@ def build_images(prefix, images, tag=None, push=False, chart_version=None, skip_ to modify the image's files. push (bool): Whether to push the resulting images (default: False). + force_push (bool): + Whether to push the built images even if they already exist in the image + registry (default: False). chart_version (str): The latest chart version, trimmed from its build suffix, will be included as a prefix on image tags if `tag` is not specified. + force_build (bool): + To build even if the image is available locally or remotely already. skip_build (bool): Whether to skip the actual image build (only updates tags). long (bool): @@ -364,7 +369,7 @@ def build_images(prefix, images, tag=None, push=False, chart_version=None, skip_ if skip_build: continue - if tag or image_needs_building(image_spec): + if force_build or tag or image_needs_building(image_spec): build_args = render_build_args( options, { @@ -376,8 +381,8 @@ def build_images(prefix, images, tag=None, push=False, chart_version=None, skip_ else: print(f"Skipping build for {image_spec}, it already exists") - if push: - if tag or image_needs_pushing(image_spec): + if push or force_push: + if force_push or tag or image_needs_pushing(image_spec): check_call([ 'docker', 'push', image_spec ]) @@ -603,7 +608,12 @@ def main(args=None): argparser.add_argument( '--push', action='store_true', - help='Push built images to their docker image registry.', + help='Push built images to their image registries, but not if it would replace an existing image.', + ) + argparser.add_argument( + '--force-push', + action='store_true', + help='Push built images to their image registries, regardless if it would replace an existing image.', ) argparser.add_argument( '--publish-chart', @@ -636,11 +646,17 @@ def main(args=None): action='store_true', help="Skip image build step and reset Chart.yaml's version field and values.yaml's image tags. What it resets to can be configured in chartpress.yaml with the resetTag and resetVersion configurations.", ) - argparser.add_argument( + skip_or_force_build_group = argparser.add_mutually_exclusive_group() + skip_or_force_build_group.add_argument( '--skip-build', action='store_true', help='Skip the image build step.', ) + skip_or_force_build_group.add_argument( + '--force-build', + action='store_true', + help='Enforce the image build step, regardless of if the image already is available either locally or remotely.', + ) argparser.add_argument( '--version', action='store_true', @@ -695,7 +711,9 @@ def main(args=None): images=chart['images'], tag=args.tag if not args.reset else chart.get('resetTag', 'set-by-chartpress'), push=args.push, + force_push=args.force_push, chart_version=chart_version, + force_build=args.force_build, skip_build=args.skip_build or args.reset, long=args.long, ) From 420955f8bc4d88a67857a7bc54d82e364ef03990 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 24 Oct 2019 23:20:39 +0200 Subject: [PATCH 2/5] --tag no longer acts as --force-build and --force-push --- chartpress.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chartpress.py b/chartpress.py index 9f0985c..670a658 100755 --- a/chartpress.py +++ b/chartpress.py @@ -369,7 +369,7 @@ def build_images(prefix, images, tag=None, push=False, force_push=False, chart_v if skip_build: continue - if force_build or tag or image_needs_building(image_spec): + if force_build or image_needs_building(image_spec): build_args = render_build_args( options, { @@ -382,7 +382,7 @@ def build_images(prefix, images, tag=None, push=False, force_push=False, chart_v print(f"Skipping build for {image_spec}, it already exists") if push or force_push: - if force_push or tag or image_needs_pushing(image_spec): + if force_push or image_needs_pushing(image_spec): check_call([ 'docker', 'push', image_spec ]) From 8cca1e9ba4871c74a17c161d9edf59ff7d2af72a Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 24 Oct 2019 23:48:12 +0200 Subject: [PATCH 3/5] Update README with force flags --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 073fd86..4d29d80 100644 --- a/README.md +++ b/README.md @@ -42,17 +42,21 @@ to build your chart(s) and image(s). Add `--push` to publish images to docker hub and `--publish-chart` to publish the chart and index to gh-pages. ``` -usage: chartpress [-h] [--push] [--publish-chart] +usage: chartpress [-h] [--push] [--force-push] [--publish-chart] [--extra-message EXTRA_MESSAGE] [--tag TAG | --long] - [--image-prefix IMAGE_PREFIX] [--reset] [--skip-build] - [--version] [--commit-range COMMIT_RANGE] + [--image-prefix IMAGE_PREFIX] [--reset] + [--skip-build | --force-build] [--version] + [--commit-range COMMIT_RANGE] Automate building and publishing helm charts and associated images. This is used as part of the JupyterHub and Binder projects. optional arguments: -h, --help show this help message and exit - --push Push built images to their docker image registry. + --push Push built images to their image registries, but not + if it would replace an existing image. + --force-push Push built images to their image registries, + regardless if it would replace an existing image. --publish-chart Package a Helm chart and publish it to a Helm chart repository contructed with a GitHub git repository and GitHub pages. @@ -70,6 +74,8 @@ optional arguments: can be configured in chartpress.yaml with the resetTag and resetVersion configurations. --skip-build Skip the image build step. + --force-build Enforce the image build step, regardless of if the + image already is available either locally or remotely. --version Print current chartpress version and exit. --commit-range COMMIT_RANGE Deprecated: this flag will be ignored. The new logic From 8b48073e1d5ee0232290259670cef87b47c4552f Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 7 Nov 2019 14:21:26 +0100 Subject: [PATCH 4/5] Test 1+ image references in values.yaml only once --- tests/test_repo_interactions.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test_repo_interactions.py b/tests/test_repo_interactions.py index bb293ea..05567c3 100644 --- a/tests/test_repo_interactions.py +++ b/tests/test_repo_interactions.py @@ -45,8 +45,6 @@ def test_chartpress_run(git_repo, capfd): out = _capture_output(["--reset"], capfd) assert f"Updating testchart/Chart.yaml: version: 0.0.1-test.reset.version" in out assert f"Updating testchart/values.yaml: image: testchart/testimage:test-reset-tag" in out - assert f"Updating testchart/values.yaml: list.0: testchart/testimage:test-reset-tag" in out - assert f"Updating testchart/values.yaml: list.1.image: testchart/testimage:test-reset-tag" in out # verify that we don't need to rebuild the image out = _capture_output([], capfd) @@ -59,8 +57,6 @@ def test_chartpress_run(git_repo, capfd): assert f"Successfully tagged" not in out assert f"Updating testchart/Chart.yaml: version: {tag}" in out assert f"Updating testchart/values.yaml: image: testchart/testimage:{tag}" in out - assert f"Updating testchart/values.yaml: list.0: testchart/testimage:{tag}" in out - assert f"Updating testchart/values.yaml: list.1.image: testchart/testimage:{tag}" in out # verify a real git tag is detected @@ -77,16 +73,12 @@ def test_chartpress_run(git_repo, capfd): out = _capture_output(["--skip-build", "--long"], capfd) assert f"Updating testchart/Chart.yaml: version: {tag}.000.{sha}" in out assert f"Updating testchart/values.yaml: image: testchart/testimage:{tag}.000.{sha}" in out - assert f"Updating testchart/values.yaml: list.0: testchart/testimage:{tag}.000.{sha}" in out - assert f"Updating testchart/values.yaml: list.1.image: testchart/testimage:{tag}.000.{sha}" in out # verify usage of --image-prefix out = _capture_output(["--skip-build", "--image-prefix", "test-prefix/"], capfd) assert f"Updating testchart/Chart.yaml: version: {tag}" in out assert f"Updating testchart/values.yaml: image: test-prefix/testimage:{tag}" in out - assert f"Updating testchart/values.yaml: list.0: test-prefix/testimage:{tag}" in out - assert f"Updating testchart/values.yaml: list.1.image: test-prefix/testimage:{tag}" in out # verify usage of --publish-chart and --extra-message From 69ceb827c46033ec765ea2be069f52b9d4130eba Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 7 Nov 2019 14:30:40 +0100 Subject: [PATCH 5/5] Add test for --force-build --- tests/README.md | 2 +- tests/test_repo_interactions.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/README.md b/tests/README.md index a655b2f..4659141 100644 --- a/tests/README.md +++ b/tests/README.md @@ -17,7 +17,7 @@ pytest -vx --flakes ``` ## Not yet tested -- `--push` +- `--push` or `--force-push` ## References - [pytest](https://docs.pytest.org) diff --git a/tests/test_repo_interactions.py b/tests/test_repo_interactions.py index 05567c3..66404c9 100644 --- a/tests/test_repo_interactions.py +++ b/tests/test_repo_interactions.py @@ -10,6 +10,8 @@ def test_git_repo_fixture(git_repo): assert os.path.isfile("chartpress.yaml") assert os.path.isfile("testchart/Chart.yaml") assert os.path.isfile("testchart/values.yaml") + assert os.path.isfile("testchart/templates/configmap.yaml") + assert os.path.isfile("image/Dockerfile") # assert there is another branch to contain published content as well git_repo.git.checkout("gh-pages") @@ -46,11 +48,17 @@ def test_chartpress_run(git_repo, capfd): assert f"Updating testchart/Chart.yaml: version: 0.0.1-test.reset.version" in out assert f"Updating testchart/values.yaml: image: testchart/testimage:test-reset-tag" in out + # verify that we don't need to rebuild the image out = _capture_output([], capfd) assert f"Skipping build" in out + # verify usage of --force-build + out = _capture_output(["--force-build"], capfd) + assert f"Successfully tagged" in out + + # verify usage --skip-build and --tag tag = "1.2.3-test.tag" out = _capture_output(["--skip-build", "--tag", tag], capfd) @@ -90,11 +98,13 @@ def test_chartpress_run(git_repo, capfd): ], capfd, ) + # verify output of --publish-chart assert "Branch 'gh-pages' set up to track remote branch 'gh-pages' from 'origin'." in out assert "Successfully packaged chart and saved it to:" in out assert f"/testchart-{tag}.tgz" in out + # checkout gh-pages git_repo.git.stash() git_repo.git.checkout("gh-pages") @@ -110,6 +120,7 @@ def test_chartpress_run(git_repo, capfd): automatic_helm_chart_repo_commit = git_repo.commit("HEAD") assert "test added --extra-message" in automatic_helm_chart_repo_commit.message + # return to master git_repo.git.checkout("master") git_repo.git.stash("pop") @@ -126,11 +137,13 @@ def test_chartpress_run(git_repo, capfd): ], capfd, ) + # verify output of --publish-chart assert "Branch 'gh-pages' set up to track remote branch 'gh-pages' from 'origin'." in out assert "Successfully packaged chart and saved it to:" in out assert f"/testchart-{tag}.001.{sha}.tgz" in out + # checkout gh-pages git_repo.git.stash() git_repo.git.checkout("gh-pages") @@ -145,6 +158,7 @@ def test_chartpress_run(git_repo, capfd): assert f"version: {tag}" in index_yaml assert f"version: {tag}.001.{sha}" in index_yaml + # return to master git_repo.git.checkout("master") git_repo.git.stash("pop")