From dfb38844400e0320ffb04033d8f87ab3f8b675ea Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 6 May 2020 21:48:57 -0500 Subject: [PATCH] Upload images to dockerhub with buildx when using buildx If we build multi-arch images with buildx we also need to push to dockerhub using buildx, because otherwise we're just pushing the single-arch image we fetched back from the buildset registry for the local cache. Change-Id: If8b95a708e4f0d24e959317b803f5c9379a8b62b --- roles/upload-docker-image/tasks/buildx.yaml | 36 +++++++++++++++++++++ roles/upload-docker-image/tasks/main.yaml | 14 +++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 roles/upload-docker-image/tasks/buildx.yaml diff --git a/roles/upload-docker-image/tasks/buildx.yaml b/roles/upload-docker-image/tasks/buildx.yaml new file mode 100644 index 000000000..da6e2bd15 --- /dev/null +++ b/roles/upload-docker-image/tasks/buildx.yaml @@ -0,0 +1,36 @@ +- name: Upload tag to dockerhub + # docker buildx doesn't have a separate push command, only the + # ability to push at the end of a build. We run build here with + # all the same arguments as in the buildx build tasks, so they + # should result in a noop build and just a push. + command: >- + docker buildx build {{ zj_image.path | default('.') }} -f {{ zj_image.dockerfile | default(docker_dockerfile) }} + {% if zj_image.target | default(false) -%} + --target {{ zj_image.target }} + {% endif -%} + {% for build_arg in zj_image.build_args | default([]) -%} + --build-arg {{ build_arg }} + {% endfor -%} + {% if zj_image.siblings | default(false) -%} + --build-arg "ZUUL_SIBLINGS={{ zj_image.siblings | join(' ') }}" + {% endif -%} + {% for tag in zj_image.tags | default(['latest']) -%} + {% if zuul.change | default(false) -%} + --tag {{ zj_image.repository }}:change_{{ zuul.change }}_{{ tag }} + {% endif -%} + --tag {{ zj_image.repository }}:{{ tag }} + {% endfor -%} + {% for label in zj_image.labels | default([]) -%} + --label "{{ label }}" + {% endfor %} + --label "org.zuul-ci.change={{ zuul.change }}" + --label "org.zuul-ci.change_url={{ zuul.change_url }}" + --push + args: + chdir: "{{ zuul_work_dir }}/{{ zj_image.context }}" + environment: + DOCKER_CLI_EXPERIMENTAL: enabled + register: result + until: result.rc == 0 + retries: 3 + delay: 30 diff --git a/roles/upload-docker-image/tasks/main.yaml b/roles/upload-docker-image/tasks/main.yaml index c06b12fe1..e84d725d3 100644 --- a/roles/upload-docker-image/tasks/main.yaml +++ b/roles/upload-docker-image/tasks/main.yaml @@ -12,8 +12,20 @@ command: "docker login -u {{ docker_credentials.username }} -p {{ docker_credentials.password }}" no_log: true -- name: Upload image to dockerhub +- name: Determine if we need to use buildx + set_fact: + use_buildx: "{{ docker_images | selectattr('arch', 'defined') | list }}" + +- name: Upload image to dockerhub normally loop: "{{ docker_images }}" loop_control: loop_var: zj_image include_tasks: push.yaml + when: not use_buildx + +- name: Upload image to dockerhub using buildx + loop: "{{ docker_images }}" + loop_control: + loop_var: zj_image + include_tasks: buildx.yaml + when: use_buildx