--- # Copyright (c) Ansible Project # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) # SPDX-License-Identifier: GPL-3.0-or-later - name: Registering image name ansible.builtin.set_fact: iname: "{{ name_prefix ~ '-options' }}" - name: Registering image name ansible.builtin.set_fact: inames: "{{ inames + [iname] }}" #################################################################### ## args ############################################################ #################################################################### - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - name: buildargs community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "ArgsDockerfile" args: IMAGE: "{{ docker_test_image_busybox }}" TEST1: val1 TEST2: val2 TEST3: "True" pull: false register: buildargs_1 - name: buildargs (idempotency) community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "ArgsDockerfile" args: IMAGE: "{{ docker_test_image_busybox }}" TEST1: val1 TEST2: val2 TEST3: "True" pull: false register: buildargs_2 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - buildargs_1 is changed - buildargs_2 is not changed #################################################################### ## dockerfile ###################################################### #################################################################### - name: dockerfile community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "MyDockerfile" pull: false register: dockerfile_1 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - dockerfile_1 is changed - "('FROM ' ~ docker_test_image_alpine) in dockerfile_1.stderr" - dockerfile_1['image']['Config']['WorkingDir'] == '/newdata' #################################################################### ## platform ######################################################## #################################################################### - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - name: platform community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" platform: linux pull: false register: platform_1 - name: platform (idempotency) community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" platform: linux pull: false register: platform_2 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - platform_1 is changed - platform_2 is not changed #################################################################### ## target ########################################################## #################################################################### - name: Build multi-stage image community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "StagedDockerfile" target: first pull: false register: dockerfile_2 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - dockerfile_2 is changed - dockerfile_2.image.Config.WorkingDir == '/first' #################################################################### ## etc_hosts ####################################################### #################################################################### - name: Build image with custom etc_hosts community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "EtcHostsDockerfile" pull: false etc_hosts: some-custom-host: "127.0.0.1" register: path_1 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - path_1 is changed #################################################################### ## shm_size ######################################################## #################################################################### - name: Build image with custom shm_size community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "MyDockerfile" pull: false shm_size: 128MB register: path_1 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - ansible.builtin.assert: that: - path_1 is changed #################################################################### ## labels ########################################################## #################################################################### - name: Build image with labels community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "MyDockerfile" pull: false labels: FOO: BAR this is a label: this is the label's value register: labels_1 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - name: Show image information ansible.builtin.debug: var: labels_1.image - ansible.builtin.assert: that: - labels_1 is changed - labels_1.image.Config.Labels.FOO == 'BAR' - labels_1.image.Config.Labels["this is a label"] == "this is the label's value" #################################################################### ## secrets ######################################################### #################################################################### - name: Generate secret ansible.builtin.set_fact: docker_image_build_secret_value: this is my secret {{ '%0x' % ((2**32) | random) }} - when: buildx_version is version('0.6.0', '>=') block: - name: Build image with secrets via environment variables community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "SecretsDockerfile" pull: false secrets: - id: my-awesome-secret type: value value: '{{ docker_image_build_secret_value }}' nocache: true # using a cache can result in the output step being CACHED register: secrets_1 - name: cleanup community.docker.docker_image_remove: name: "{{ iname }}" - name: Show image information ansible.builtin.debug: var: secrets_1.stderr_lines - ansible.builtin.assert: that: - secrets_1 is changed - (docker_image_build_secret_value | b64encode) in secrets_1.stderr #################################################################### ## outputs ######################################################### #################################################################### - when: buildx_version is version('0.13.0', '>=') block: - name: Make sure the image is not there community.docker.docker_image_remove: name: "{{ iname }}" - name: Make sure the image tarball is not there ansible.builtin.file: path: "{{ remote_tmp_dir }}/container.tar" state: absent - name: Build image with outputs community.docker.docker_image_build: name: "{{ iname }}" path: "{{ remote_tmp_dir }}/files" dockerfile: "Dockerfile" pull: false outputs: - type: tar dest: "{{ remote_tmp_dir }}/container.tar" ignore_errors: true register: outputs_1 - when: outputs_1 is not failed block: - name: cleanup (should be changed) community.docker.docker_image_remove: name: "{{ iname }}" register: outputs_1_cleanup - name: Gather information on tarball ansible.builtin.stat: path: "{{ remote_tmp_dir }}/container.tar" register: outputs_1_stat - name: Show image information ansible.builtin.debug: var: outputs_1.image - name: Show tarball information ansible.builtin.debug: var: outputs_1_stat.stat - ansible.builtin.assert: that: - outputs_1 is changed - outputs_1.image | length > 0 - outputs_1_cleanup is changed - outputs_1_stat.stat.exists - when: outputs_1 is failed ansible.builtin.assert: that: - >- 'ERROR: multiple outputs currently unsupported by the current BuildKit daemon' in outputs_1.stderr