Skip to content

Commit 1bb4d9f

Browse files
committed
Add image squashing
1 parent 419f97f commit 1bb4d9f

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

ansible/group_vars/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pager_duty_key: testkey
77
registry_env: prod
88
registry_port: 80
99
registry_host: "quay.io"
10+
quay_api_token: QB9UzzNhwClqMgRyMgNGrSGLdUYZPJLJALdcpKRa
1011

1112
# for docker stop old container
1213
stop_time: 5
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
dependencies:
3+
- { role: docker_squash }

ansible/roles/builder/tasks/main.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
url: https://quay.io/api/v1/repository/runnable/{{ repository_name | default(name) }}/tag/?limit=1&specificTag={{ container_tag }}
2424
method: GET
2525
headers:
26-
Authorization: 'Bearer QB9UzzNhwClqMgRyMgNGrSGLdUYZPJLJALdcpKRa'
26+
Authorization: 'Bearer {{ quay_api_token }}'
2727
register: currently_built_tags
2828

2929
- name: set number of images built
@@ -134,6 +134,32 @@
134134
loop_control:
135135
loop_var: file_name_item
136136

137+
- name: get image id for newly created image
138+
tags: deploy
139+
become: true
140+
when: build_image and push_image
141+
shell: docker images -a | grep "{{ container_image }}" | grep "{{ container_tag }}" | awk '{print $3}'
142+
register: unsquahed_image_id
143+
144+
- name: squash newly created image (Expect this to take some time [5 min. approx])
145+
tags: deploy
146+
when: build_image and push_image
147+
become: true
148+
shell: docker-squash "{{ unsquahed_image_id.stdout_lines[0] }}" -t "{{ container_image }}:{{ container_tag }}"
149+
150+
- name: get number of layers
151+
tags: deploy
152+
when: build_image and push_image
153+
become: true
154+
shell: docker history "{{ container_image }}:{{ container_tag }}" | wc -l
155+
register: number_of_layers
156+
157+
- name: Ensure only squashed images are pushed
158+
tags: deploy
159+
# One line for layer and one line for headers
160+
when: build_image and push_image and (number_of_layers is defined and number_of_layers.stdout != "2")
161+
fail: msg="Only squashed layers can be pushed to registry"
162+
137163
- name: login to registry
138164
tags: deploy
139165
become: yes

ansible/roles/squash_image/tasks/main.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
---
2-
- name: squash newly created image
3-
become: true
4-
ignore_errors: true # Might already be squashed
5-
shell: docker-squash "{{ unsquahed_image_id.stdout_lines[0] }}" -t "{{ container_image }}:{{ container_tag }}"
6-
72
- name: get image id for newly created image
83
become: true
94
shell: docker images -a | grep "{{ container_image }}" | grep "{{ container_tag }}" | awk '{print $3}'

0 commit comments

Comments
 (0)