Skip to content

Commit d6459d5

Browse files
rlandydanpawlik
authored andcommitted
Add a containers tag role
1 parent e9bba52 commit d6459d5

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

roles/build_containers/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ become - Required to install and execute tcib
3535
* `cifmw_build_containers_hotfix_tag`: (String) The tag of the container image.
3636
* `cifmw_build_containers_run_hotfix`: (boolean) conditional variable for executing build_containers.
3737
* `cifmw_build_containers_install_from_source`: (boolean) Install tcib from RPM.
38+
* `cifmw_build_containers_tag_string`: (String) Human readable string to tag containers
39+
* `cifmw_build_containers_retag_images`: (Boolean) Whether to tag images again after pushing with hash tag. Default to `false`
40+
* `cifmw_build_containers_retag_string`: (String) Human readable string to re-tag containers
3841

3942
### Parameters used in meta-content-provider
4043

roles/build_containers/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ cifmw_build_containers_repo_dir: "{{ cifmw_build_containers_basedir }}/artifacts
4141
cifmw_build_containers_image_tag: current-podified
4242
cifmw_build_containers_containers_base_image: quay.io/centos/centos:stream9
4343
cifmw_build_containers_cleanup: false
44+
cifmw_build_containers_tag_string: current
45+
cifmw_build_containers_retag_images: false
46+
cifmw_build_containers_retag_string: current
4447

4548
# Install tcib from source
4649
cifmw_build_containers_install_from_source: false

roles/build_containers/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@
9090
- cifmw_build_containers_buildah_push | default ('false') | bool
9191
- not cifmw_build_containers_push_containers | bool
9292

93+
- name: "Retag each image and push to registry: {{ item }}"
94+
become: true
95+
ansible.builtin.command: >
96+
buildah push --format v2s2 --all {{ item }}:{{ cifmw_build_containers_image_tag }} docker://{{ item }}:{{ cifmw_build_containers_retag_string }}
97+
loop: "{{ built_images.stdout_lines }}"
98+
when:
99+
- cifmw_build_containers_retag_images | default(false) | bool
100+
- cifmw_build_containers_tag_string != cifmw_build_containers_retag_string
101+
93102
- name: Cleanup tcib directories after container build
94103
ansible.builtin.import_tasks: cleanup.yml
95104
when: cifmw_build_containers_cleanup | bool
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
# Copyright Red Hat, Inc.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
- name: Ensure directories are present
18+
ansible.builtin.file:
19+
path: "{{ cifmw_build_containers_basedir }}/{{ item }}"
20+
state: directory
21+
mode: "0755"
22+
loop:
23+
- tmp
24+
- artifacts
25+
- logs
26+
27+
- name: Make sure authfile exists
28+
when:
29+
- cifmw_build_containers_authfile_path != None
30+
- cifmw_build_containers_push_containers | bool
31+
block:
32+
- name: Check for authfile
33+
ansible.builtin.stat:
34+
path: '{{ cifmw_build_containers_authfile_path }}'
35+
register: authfile_exist
36+
37+
- name: Make sure authfile exists
38+
ansible.builtin.assert:
39+
that:
40+
- authfile_exist.stat.exists | bool
41+
42+
- name: Retrieve the log file from container build job
43+
ansible.builtin.get_url:
44+
url: "{{ containers_built_artifacts_url }}/ci-framework-data/logs/containers-built.log"
45+
dest: "{{ cifmw_build_containers_basedir }}/logs/containers-built.log"
46+
mode: "0644"
47+
force: true
48+
register: result
49+
until:
50+
- result.status_code is defined
51+
- result.status_code == 200
52+
retries: 6
53+
delay: 50
54+
55+
- name: Get built_images from the log file
56+
ansible.builtin.shell:
57+
cmd: >-
58+
set -o pipefail;
59+
cat {{ cifmw_build_containers_basedir }}/logs/containers-built.log |
60+
grep {{ cifmw_build_containers_container_name_prefix }} |
61+
awk '{ print $1 }'
62+
register: built_images_from_file
63+
64+
- name: Get the hash tag from the log file
65+
ansible.builtin.shell:
66+
cmd: >-
67+
set -o pipefail;
68+
cat {{ cifmw_build_containers_basedir }}/logs/containers-built.log |
69+
grep {{ cifmw_build_containers_container_name_prefix }} |
70+
awk '{ print $2 }' | head -n 1
71+
register: images_tag_from_file
72+
73+
- name: Set variables for looping
74+
ansible.builtin.set_fact:
75+
built_images: "{{ built_images_from_file.stdout_lines }}"
76+
images_tag: "{{ images_tag_from_file.stdout_lines[0] }}"
77+
78+
- name: Pull images returned in built_images
79+
containers.podman.podman_image:
80+
name: "{{ item }}"
81+
tag: "{{ images_tag }}"
82+
loop: "{{ built_images }}"
83+
84+
- name: Retag the images with new tag
85+
containers.podman.podman_tag:
86+
image: "{{ item }}:{{ images_tag }}"
87+
target_names:
88+
- "{{ item }}:{{ cifmw_build_containers_tag_string }}"
89+
loop: "{{ built_images }}"
90+
91+
- name: Push images to registry with new tag
92+
containers.podman.podman_image:
93+
name: "{{ item }}"
94+
push_args:
95+
dest: "{{ cifmw_build_containers_push_registry }}/{{ cifmw_build_containers_registry_namespace }}"
96+
tag: "{{ cifmw_build_containers_tag_string }}"
97+
pull: false
98+
push: true
99+
loop: "{{ built_images }}"

0 commit comments

Comments
 (0)