Browse Source

Handle multiple docker images with the same repository

So that users can specify two docker image builds for the same
repository, but with different tags, ensure that the temporary
change_ tag attached to the image also includes the final tag
name.

This allows this configuration to work:

docker_images:
  - repository: foo/image
    context: opensuse
    tags:
      - opensuse-latest
  - repository: foo/image
    context: ubuntu
    tags:
      - ubuntu-latest

Change-Id: I917dcf8a74fc864ea06dc70bdb3e212dc170eb48
changes/79/639779/1
James E. Blair 2 months ago
parent
commit
885f02e217

+ 1
- 1
roles/build-docker-image/tasks/main.yaml View File

@@ -13,8 +13,8 @@
13 13
     {% for build_arg in item.build_args | default([]) -%}
14 14
       --build-arg {{ build_arg }}
15 15
     {% endfor -%}
16
-    --tag {{ item.repository }}:change_{{ zuul.change }}
17 16
     {% for tag in item.tags | default(['latest']) -%}
17
+    --tag {{ item.repository }}:change_{{ zuul.change }}_{{ tag }}
18 18
     --tag {{ item.repository }}:{{ tag }}
19 19
     {% endfor -%}
20 20
   args:

+ 28
- 0
roles/promote-docker-image/tasks/promote-retag-inner.yaml View File

@@ -0,0 +1,28 @@
1
+- name: Get manifest
2
+  no_log: true
3
+  uri:
4
+    url: "https://registry.hub.docker.com/v2/{{ image.repository }}/manifests/change_{{ zuul.change }}_{{ image_tag }}"
5
+    status_code: 200
6
+    headers:
7
+      Accept: "application/vnd.docker.distribution.manifestv2+json"
8
+      Authorization: "Bearer {{ token.json.token }}"
9
+    return_content: true
10
+  register: manifest
11
+- name: Put manifest
12
+  no_log: true
13
+  uri:
14
+    url: "https://registry.hub.docker.com/v2/{{ image.repository }}/manifests/{{ image_tag }}"
15
+    method: PUT
16
+    status_code: 201
17
+    body: "{{ manifest.content | string }}"
18
+    headers:
19
+      Content-Type: "application/vnd.docker.distribution.manifestv2+json"
20
+      Authorization: "Bearer {{ token.json.token }}"
21
+- name: Delete the current change tag
22
+  no_log: true
23
+  uri:
24
+    url: "https://hub.docker.com/v2/repositories/{{ image.repository }}/tags/change_{{ zuul.change }}_{{ image_tag }}/"
25
+    method: DELETE
26
+    status_code: 204
27
+    headers:
28
+      Authorization: "JWT {{ jwt_token.json.token }}"

+ 3
- 29
roles/promote-docker-image/tasks/promote-retag.yaml View File

@@ -6,34 +6,8 @@
6 6
     password: "{{ docker_credentials.password }}"
7 7
     force_basic_auth: true
8 8
   register: token
9
-- name: Get manifest
10
-  no_log: true
11
-  uri:
12
-    url: "https://registry.hub.docker.com/v2/{{ image.repository }}/manifests/change_{{ zuul.change }}"
13
-    status_code: 200
14
-    headers:
15
-      Accept: "application/vnd.docker.distribution.manifestv2+json"
16
-      Authorization: "Bearer {{ token.json.token }}"
17
-    return_content: true
18
-  register: manifest
19
-- name: "Put manifest"
20
-  no_log: true
9
+- name: Retag image
21 10
   loop: "{{ image.tags | default(['latest']) }}"
22 11
   loop_control:
23
-    loop_var: new_tag
24
-  uri:
25
-    url: "https://registry.hub.docker.com/v2/{{ image.repository }}/manifests/{{ new_tag }}"
26
-    method: PUT
27
-    status_code: 201
28
-    body: "{{ manifest.content | string }}"
29
-    headers:
30
-      Content-Type: "application/vnd.docker.distribution.manifestv2+json"
31
-      Authorization: "Bearer {{ token.json.token }}"
32
-- name: Delete the current change tag
33
-  no_log: true
34
-  uri:
35
-    url: "https://hub.docker.com/v2/repositories/{{ image.repository }}/tags/change_{{ zuul.change }}/"
36
-    method: DELETE
37
-    status_code: 204
38
-    headers:
39
-      Authorization: "JWT {{ jwt_token.json.token }}"
12
+    loop_var: image_tag
13
+  include_tasks: promote-retag-inner.yaml

+ 4
- 2
roles/upload-docker-image/tasks/main.yaml View File

@@ -8,6 +8,8 @@
8 8
 - name: Log in to dockerhub
9 9
   command: "docker login -u {{ docker_credentials.username }} -p {{ docker_credentials.password }}"
10 10
   no_log: true
11
-- name: Upload to dockerhub
12
-  command: "docker push {{ item.repository }}:change_{{ zuul.change }}"
11
+- name: Upload image to dockerhub
13 12
   loop: "{{ docker_images }}"
13
+  loop_control:
14
+    loop_var: image
15
+  include_tasks: push.yaml

+ 5
- 0
roles/upload-docker-image/tasks/push.yaml View File

@@ -0,0 +1,5 @@
1
+- name: Upload tag to dockerhub
2
+  command: "docker push {{ item.repository }}:change_{{ zuul.change }}_{{ image_tag }}"
3
+  loop: "{{ image.tags | default(['latest']) }}"
4
+  loop_control:
5
+    loop_var: image_tag

Loading…
Cancel
Save