diff --git a/roles/build-docker-image/common.rst b/roles/build-docker-image/common.rst index 95d37adcf..8a5cc977d 100644 --- a/roles/build-docker-image/common.rst +++ b/roles/build-docker-image/common.rst @@ -119,4 +119,15 @@ using this role. A list of tags to be added to the image when promoted. + .. zuul:rolevar:: siblings + :type: list + :default: [] + + A list of sibling projects to be copied into + ``{{zuul_work_dir}}/.zuul-siblings``. This can be useful to + collect multiple projects to be installed within the same Docker + context. A ``-build-arg`` called ``ZUUL_SIBLINGS`` will be + added with each sibling project. Note that projects here must + be listed in ``required-projects``. + .. _anchors: https://yaml.org/spec/1.2/spec.html#&%20anchor// diff --git a/roles/build-docker-image/tasks/build.yaml b/roles/build-docker-image/tasks/build.yaml new file mode 100644 index 000000000..b50d00d33 --- /dev/null +++ b/roles/build-docker-image/tasks/build.yaml @@ -0,0 +1,41 @@ +# assert sibling dir not present + +- name: Create sibling source directory + file: + path: '{{ zuul_work_dir }}/.zuul-siblings' + state: directory + mode: 0755 + when: item.siblings is defined + +# use recursive copy with remote_src? ansible 2.8 only +- name: Copy sibling source directories + command: 'cp -r ~/src/{{ sibling }} {{ zuul_work_dir}}/.zuul-siblings/' + loop: '{{ item.siblings }}' + loop_control: + loop_var: sibling + when: item.siblings is defined + +- name: Build a docker image + command: >- + docker build {{ item.path | default('.') }} -f {{ item.dockerfile | default(docker_dockerfile) }} + {% if item.target | default(false) -%} + --target {{ item.target }} + {% endif -%} + {% for build_arg in item.build_args | default([]) -%} + --build-arg {{ build_arg }} + {% endfor -%} + {% if item.siblings | default(false) -%} + --build-arg "ZUUL_SIBLINGS={{ item.siblings | join(' ')}}" + {% endif -%} + {% for tag in item.tags | default(['latest']) -%} + --tag {{ item.repository }}:change_{{ zuul.change }}_{{ tag }} + --tag {{ item.repository }}:{{ tag }} + {% endfor -%} + args: + chdir: "{{ zuul_work_dir }}/{{ item.context }}" + +- name: Cleanup sibling source directory + file: + path: '{{ zuul_work_dir }}/.zuul-siblings' + state: absent + diff --git a/roles/build-docker-image/tasks/main.yaml b/roles/build-docker-image/tasks/main.yaml index 4dceac00a..c5d089880 100644 --- a/roles/build-docker-image/tasks/main.yaml +++ b/roles/build-docker-image/tasks/main.yaml @@ -4,22 +4,11 @@ set_fact: buildset_registry: "{{ (lookup('file', zuul.executor.work_root + '/results.json') | from_json)['buildset_registry'] }}" ignore_errors: true -- name: Build a docker image - command: >- - docker build {{ item.path | default('.') }} -f {{ item.dockerfile | default(docker_dockerfile) }} - {% if item.target | default(false) -%} - --target {{ item.target }} - {% endif -%} - {% for build_arg in item.build_args | default([]) -%} - --build-arg {{ build_arg }} - {% endfor -%} - {% for tag in item.tags | default(['latest']) -%} - --tag {{ item.repository }}:change_{{ zuul.change }}_{{ tag }} - --tag {{ item.repository }}:{{ tag }} - {% endfor -%} - args: - chdir: "{{ zuul_work_dir }}/{{ item.context }}" + +- name: Build docker images + include_tasks: build.yaml loop: "{{ docker_images }}" + # Docker doesn't understand docker push [1234:5678::]:5000/image/path:tag # so we set up /etc/hosts with a registry alias name to support ipv6 and 4. - name: Configure /etc/hosts for buildset_registry to workaround docker not understanding ipv6 addresses