summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--roles/build-docker-image/README.rst3
-rw-r--r--roles/build-docker-image/common.rst98
-rw-r--r--roles/build-docker-image/defaults/main.yaml1
-rw-r--r--roles/build-docker-image/tasks/main.yaml13
-rw-r--r--roles/merge-output-to-logs/README.rst14
-rw-r--r--roles/merge-output-to-logs/tasks/main.yaml15
-rw-r--r--roles/promote-docker-image/README.rst3
-rw-r--r--roles/promote-docker-image/defaults/main.yaml1
-rw-r--r--roles/promote-docker-image/tasks/main.yaml20
-rw-r--r--roles/promote-docker-image/tasks/promote-cleanup.yaml20
-rw-r--r--roles/promote-docker-image/tasks/promote-retag.yaml39
-rw-r--r--roles/upload-docker-image/README.rst3
-rw-r--r--roles/upload-docker-image/defaults/main.yaml1
-rw-r--r--roles/upload-docker-image/tasks/main.yaml6
-rw-r--r--roles/upload-puppetforge/README.rst22
-rw-r--r--roles/upload-puppetforge/defaults/main.yaml3
-rw-r--r--roles/upload-puppetforge/tasks/main.yaml21
17 files changed, 283 insertions, 0 deletions
diff --git a/roles/build-docker-image/README.rst b/roles/build-docker-image/README.rst
new file mode 100644
index 0000000..f533afa
--- /dev/null
+++ b/roles/build-docker-image/README.rst
@@ -0,0 +1,3 @@
1Build one or more docker images.
2
3.. include:: ../../roles/build-docker-image/common.rst
diff --git a/roles/build-docker-image/common.rst b/roles/build-docker-image/common.rst
new file mode 100644
index 0000000..c2c65df
--- /dev/null
+++ b/roles/build-docker-image/common.rst
@@ -0,0 +1,98 @@
1This is one of a collection of roles which are designed to work
2together to build, upload, and promote docker images in a gating
3context:
4
5* :zuul:role:`build-docker-image`: Build the images.
6* :zuul:role:`upload-docker-image`: Stage the images on dockerhub.
7* :zuul:role:`promote-docker-image`: Promote previously uploaded images.
8
9The :zuul:role:`build-docker-image` role is designed to be used in
10`check` and `gate` pipelines and simply builds the images. It can be
11used to verify that the build functions, or it can be followed by the
12use of subsequent roles to upload the images to Docker Hub.
13
14The :zuul:role:`upload-docker-image` role uploads the images to Docker
15Hub, but only with a single tag corresponding to the change ID. This
16role is designed to be used in a job in a `gate` pipeline so that the
17build produced by the gate is staged and can later be promoted to
18production if the change is successful.
19
20The :zuul:role:`promote-docker-image` role is designed to be used in a
21`promote` pipeline. It requires no nodes and runs very quickly on the
22Zuul executor. It simply re-tags a previously uploaded image for a
23change with whatever tags are supplied by the
24:zuul:rolevar:`build-docker-image.docker_images.context`. It also
25removes the change ID tag from the repository in Docker Hub, and
26removes any similar change ID tags more than 24 hours old. This keeps
27the repository tidy in the case that gated changes fail to merge after
28uploading their staged images.
29
30They all accept the same input data, principally a list of
31dictionaries representing the images to build. YAML anchors_ can be
32used to supply the same data to all three jobs.
33
34Use the :zuul:role:`install-docker` role to install Docker before
35using this role.
36
37**Role Variables**
38
39.. zuul:rolevar:: zuul_work_dir
40 :default: {{ zuul.project.src_dir }}
41
42 The project directory. Serves as the base for
43 :zuul:rolevar:`build-docker-image.docker_images.context`.
44
45.. zuul:rolevar:: credentials
46 :type: dict
47
48 This is only required for the upload and promote roles. This is
49 expected to be a Zuul Secret with two keys:
50
51 .. zuul:rolevar:: username
52
53 The Docker Hub username.
54
55 .. zuul:rolevar:: password
56
57 The Docker Hub password
58
59.. zuul:rolevar:: docker_images
60 :type: list
61
62 A list of images to build. Each item in the list should have:
63
64 .. zuul:rolevar:: context
65
66 The docker build context; this should be a directory underneath
67 :zuul:rolevar:`build-docker-image.zuul_work_dir`.
68
69 .. zuul:rolevar:: repository
70
71 The name of the target repository in dockerhub for the
72 image. Supply this even if the image is not going to be
73 uploaded (it will be tagged with this in the local
74 registry).
75
76 .. zuul:rolevar:: path
77
78 Optional: the directory that should be passed to docker build.
79 Useful for building images with a Dockerfile in the context
80 directory but a source repository elsewhere.
81
82 .. zuul:jobvar:: build_args
83 :type: list
84
85 Optional: a list of values to pass to the docker ``--build-arg``
86 parameter.
87
88 .. zuul:rolevar:: target
89
90 Optional: the target for a multi-stage build.
91
92 .. zuul:jobvar:: tags
93 :type: list
94 :default: ['latest']
95
96 A list of tags to be added to the image when promoted.
97
98.. _anchors: https://yaml.org/spec/1.2/spec.html#&%20anchor//
diff --git a/roles/build-docker-image/defaults/main.yaml b/roles/build-docker-image/defaults/main.yaml
new file mode 100644
index 0000000..9739eb1
--- /dev/null
+++ b/roles/build-docker-image/defaults/main.yaml
@@ -0,0 +1 @@
zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/build-docker-image/tasks/main.yaml b/roles/build-docker-image/tasks/main.yaml
new file mode 100644
index 0000000..5db9050
--- /dev/null
+++ b/roles/build-docker-image/tasks/main.yaml
@@ -0,0 +1,13 @@
1- name: Build a docker image
2 command: >-
3 docker build {{ item.path | default('.') }} -f Dockerfile
4 {% if target | default(false) -%}
5 --target {{ target }}
6 {% endif -%}
7 {% for build_arg in item.build_args | default([]) -%}
8 --build-arg {{ build_arg }}
9 {% endfor -%}
10 --tag {{ item.repository }}:change_{{ zuul.change }}
11 args:
12 chdir: "{{ zuul_work_dir }}/{{ item.context }}"
13 loop: "{{ images }}"
diff --git a/roles/merge-output-to-logs/README.rst b/roles/merge-output-to-logs/README.rst
new file mode 100644
index 0000000..3b0b643
--- /dev/null
+++ b/roles/merge-output-to-logs/README.rst
@@ -0,0 +1,14 @@
1Put artifacts and docs into the executor log dir
2
3.. note::
4
5 This role only works in a trusted context. It is intended to
6 be used in the post playbook of a base job.
7
8This role moves artifacts and docs into the logs dir when
9``zuul.change`` is defined so that they can be uploaded to the
10log server for developer preview and validation.
11
12Artifacts and docs are left in place when ``zuul.change`` is
13not defined so that normal publication jobs can publish them
14to final locations.
diff --git a/roles/merge-output-to-logs/tasks/main.yaml b/roles/merge-output-to-logs/tasks/main.yaml
new file mode 100644
index 0000000..d1fa111
--- /dev/null
+++ b/roles/merge-output-to-logs/tasks/main.yaml
@@ -0,0 +1,15 @@
1- name: Move artifacts and docs to logs dir
2 when: zuul.change is defined
3 delegate_to: localhost
4 shell: |
5 if ! $(ls {{ zuul.executor.work_root }}/{{ item }}) ; then
6 # Only create target directory if it is needed.
7 # Do not fail if it is already there.
8 mkdir -p {{ zuul.executor.log_root }}/{{ item }}
9 # Leave the original directory behind so that other roles
10 # operating on the interface directories can simply no-op.
11 mv -f {{ zuul.executor.work_root }}/{{ item }}/* {{ zuul.executor.log_root }}/{{ item }}
12 fi
13 loop:
14 - artifacts
15 - docs
diff --git a/roles/promote-docker-image/README.rst b/roles/promote-docker-image/README.rst
new file mode 100644
index 0000000..abce78f
--- /dev/null
+++ b/roles/promote-docker-image/README.rst
@@ -0,0 +1,3 @@
1Promote one or more previously uploaded docker images.
2
3.. include:: ../../roles/build-docker-image/common.rst
diff --git a/roles/promote-docker-image/defaults/main.yaml b/roles/promote-docker-image/defaults/main.yaml
new file mode 100644
index 0000000..9739eb1
--- /dev/null
+++ b/roles/promote-docker-image/defaults/main.yaml
@@ -0,0 +1 @@
zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/promote-docker-image/tasks/main.yaml b/roles/promote-docker-image/tasks/main.yaml
new file mode 100644
index 0000000..025303a
--- /dev/null
+++ b/roles/promote-docker-image/tasks/main.yaml
@@ -0,0 +1,20 @@
1# This is used by the delete tasks
2- name: Get dockerhub JWT token
3 no_log: true
4 uri:
5 url: "https://hub.docker.com/v2/users/login/"
6 body_format: json
7 body:
8 username: "{{ credentials.username }}"
9 password: "{{ credentials.password }}"
10 register: jwt_token
11- name: Promote image
12 loop: "{{ images }}"
13 loop_control:
14 loop_var: image
15 include_tasks: promote-retag.yaml
16- name: Delete obsolete tags
17 loop: "{{ images }}"
18 loop_control:
19 loop_var: image
20 include_tasks: promote-cleanup.yaml
diff --git a/roles/promote-docker-image/tasks/promote-cleanup.yaml b/roles/promote-docker-image/tasks/promote-cleanup.yaml
new file mode 100644
index 0000000..d8435b4
--- /dev/null
+++ b/roles/promote-docker-image/tasks/promote-cleanup.yaml
@@ -0,0 +1,20 @@
1- name: List tags
2 uri:
3 url: "https://hub.docker.com/v2/repositories/{{ image.repository }}/tags?page_size=1000"
4 status_code: 200
5 register: tags
6- name: Set cutoff timestamp to 24 hours ago
7 command: "python3 -c \"import datetime; print((datetime.datetime.utcnow()-datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%fZ'))\""
8 register: cutoff
9- name: Delete all change tags older than the cutoff
10 no_log: true
11 loop: "{{ tags.json.results }}"
12 loop_control:
13 loop_var: docker_tag
14 when: docker_tag.last_updated < cutoff.stdout and docker_tag.name.startswith('change_')
15 uri:
16 url: "https://hub.docker.com/v2/repositories/{{ image.repository }}/tags/{{ docker_tag.name }}/"
17 method: DELETE
18 status_code: 204
19 headers:
20 Authorization: "JWT {{ jwt_token.json.token }}"
diff --git a/roles/promote-docker-image/tasks/promote-retag.yaml b/roles/promote-docker-image/tasks/promote-retag.yaml
new file mode 100644
index 0000000..77b611a
--- /dev/null
+++ b/roles/promote-docker-image/tasks/promote-retag.yaml
@@ -0,0 +1,39 @@
1- name: Get dockerhub token
2 no_log: true
3 uri:
4 url: "https://auth.docker.io/token?service=registry.docker.io&scope=repository:{{ image.repository }}:pull,push"
5 user: "{{ credentials.username }}"
6 password: "{{ credentials.password }}"
7 force_basic_auth: true
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
21 loop: "{{ image.tags | default(['latest']) }}"
22 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 }}"
diff --git a/roles/upload-docker-image/README.rst b/roles/upload-docker-image/README.rst
new file mode 100644
index 0000000..2b04c2e
--- /dev/null
+++ b/roles/upload-docker-image/README.rst
@@ -0,0 +1,3 @@
1Upload one or more docker images.
2
3.. include:: ../../roles/build-docker-image/common.rst
diff --git a/roles/upload-docker-image/defaults/main.yaml b/roles/upload-docker-image/defaults/main.yaml
new file mode 100644
index 0000000..9739eb1
--- /dev/null
+++ b/roles/upload-docker-image/defaults/main.yaml
@@ -0,0 +1 @@
zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/upload-docker-image/tasks/main.yaml b/roles/upload-docker-image/tasks/main.yaml
new file mode 100644
index 0000000..ff49915
--- /dev/null
+++ b/roles/upload-docker-image/tasks/main.yaml
@@ -0,0 +1,6 @@
1- name: Log in to dockerhub
2 command: "docker login -u {{ credentials.username }} -p {{ credentials.password }}"
3 no_log: true
4- name: Upload to dockerhub
5 command: "docker push {{ item.repository }}:change_{{ zuul.change }}"
6 loop: "{{ images }}"
diff --git a/roles/upload-puppetforge/README.rst b/roles/upload-puppetforge/README.rst
new file mode 100644
index 0000000..5eae36a
--- /dev/null
+++ b/roles/upload-puppetforge/README.rst
@@ -0,0 +1,22 @@
1Upload puppet module to Puppet Forge
2
3**Role Variables**
4
5 .. zuul:rolevar:: puppet_module_dir
6 :default: {{ zuul.project.src_dir }}
7
8 The folder where the puppet module code is that it can
9 switch folder to.
10
11 .. zuul:rolevar:: blacksmith_forge_url
12 :default: https://forgeapi.puppetlabs.com
13
14 The URL to the Puppet Forge API.
15
16 .. zuul:rolevar:: blacksmith_forge_username
17
18 Username to use to log in to Puppet Forge.
19
20 .. zuul:rolevar:: blacksmith_forge_password
21
22 Password to use to log in to Puppet Forge.
diff --git a/roles/upload-puppetforge/defaults/main.yaml b/roles/upload-puppetforge/defaults/main.yaml
new file mode 100644
index 0000000..c93ad60
--- /dev/null
+++ b/roles/upload-puppetforge/defaults/main.yaml
@@ -0,0 +1,3 @@
1---
2puppet_module_dir: "{{ zuul.project.src_dir }}"
3blacksmith_forge_url: "https://forgeapi.puppetlabs.com"
diff --git a/roles/upload-puppetforge/tasks/main.yaml b/roles/upload-puppetforge/tasks/main.yaml
new file mode 100644
index 0000000..52fa288
--- /dev/null
+++ b/roles/upload-puppetforge/tasks/main.yaml
@@ -0,0 +1,21 @@
1- name: Install required gems
2 gem:
3 name: "{{ item }}"
4 with_items:
5 - rake
6 - puppetlabs_spec_helper
7 - puppet-blacksmith
8
9- name: Install new Rakefile
10 copy:
11 content: "require 'puppet_blacksmith/rake_tasks'"
12 dest: "{{ puppet_module_dir }}/Rakefile"
13
14- name: Publish puppet module
15 command: "rake module:push"
16 args:
17 chdir: "{{ puppet_module_dir }}"
18 environment:
19 BLACKSMITH_FORGE_URL: "{{ blacksmith_forge_url }}"
20 BLACKSMITH_FORGE_USERNAME: "{{ blacksmith_forge_username }}"
21 BLACKSMITH_FORGE_PASSWORD: "{{ blacksmith_forge_password }}"