From 3a1e660e628df627086edb4762d8e6ee368b7ac4 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Mon, 4 May 2020 11:50:54 -0500
Subject: [PATCH] Add zuul labels to images and ability to user-define more

We can attach some metadata to images we build indicating where
the image was built from. We can also allow users to add additional
labels, should they choose, although honestly for users doing it in
the Dockerfile makes more sense.

Change-Id: I01de15279a69026a09633eb488dac62910b324f5
---
 roles/build-docker-image/common.rst       | 6 ++++++
 roles/build-docker-image/tasks/build.yaml | 5 +++++
 2 files changed, 11 insertions(+)

diff --git a/roles/build-docker-image/common.rst b/roles/build-docker-image/common.rst
index 87a9a30d1..7b04e0441 100644
--- a/roles/build-docker-image/common.rst
+++ b/roles/build-docker-image/common.rst
@@ -130,4 +130,10 @@ using this role.
       added with each sibling project.  Note that projects here must
       be listed in ``required-projects``.
 
+   .. zuul:rolevar:: labels
+      :type: list
+      :default: []
+
+      A list of labels to attach to the built image, in the form of "key=value".
+
 .. _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
index 76b21e1ca..5ccea855b 100644
--- a/roles/build-docker-image/tasks/build.yaml
+++ b/roles/build-docker-image/tasks/build.yaml
@@ -45,6 +45,11 @@
       {% endif -%}
       --tag {{ item.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 }}"
   args:
     chdir: "{{ zuul_work_dir }}/{{ item.context }}"