ansible-collection-atmosphere/zuul.d/playbooks/ansible-collection-atmosphe.../run.yml

164 lines
7.2 KiB
YAML

# Copyright (c) 2022 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- hosts: all
tasks:
- name: Include manifest file with all the image information
ansible.builtin.include_vars:
file: "../../../images/{{ openstack_release }}.yml"
name: image_manifest
- name: Check if the image is already built
ansible.builtin.command:
docker manifest inspect {{ image_manifest.registry }}/{{ item.key }}:{{ item.value.tag }}
register: _docker_manifest_inspect
with_dict: "{{ image_manifest.projects }}"
loop_control:
label: "{{ item.key }}"
# NOTE(mnaser): We want to mark the task as "changed" if we have to do
# any work (i.e if the image is not already built).
failed_when: false
changed_when: _docker_manifest_inspect.rc != 0
- name: Fail the job if all the images are already built
ansible.builtin.fail:
msg: All the images are already built, did you forget to bump the tag?
when: _docker_manifest_inspect is not changed
- name: Generate a fact with the list of projects to be built
ansible.builtin.set_fact:
images_to_build: "{{ _docker_manifest_inspect.results | select('changed') | list | map(attribute='item.key') | list }}"
- name: Clone projects that need to be built
ansible.builtin.git:
repo: "https://opendev.org/openstack/{{ item }}"
dest: "/tmp/{{ item }}"
version: "{{ image_manifest['projects'][item]['revision'] }}"
loop: "{{ images_to_build }}"
- name: Generate the PBR version for the projects
ansible.builtin.shell:
/usr/bin/python3 setup.py --version
args:
chdir: "/tmp/{{ item }}"
environment:
PYTHONWARNINGS: "ignore:Unverified HTTPS request"
register: _pbr_version
loop: "{{ images_to_build }}"
- name: Assert that the PBR version exists in the tag
ansible.builtin.assert:
that: item.stdout.strip() in image_manifest['projects'][item.item].tag
loop: "{{ _pbr_version.results }}"
loop_control:
label: "{{ item.item }}"
- name: Set fact for wheels tarball from artifacts
ansible.builtin.set_fact:
wheels_path: "{{ item.url }}"
loop: "{{ (zuul.artifacts | default([])) | selectattr('metadata.type', 'defined') | selectattr('metadata.type', 'equalto', 'wheels') | list }}"
- name: Set the fact for wheels path to default if none is detected
ansible.builtin.set_fact:
wheels_path: "https://tarballs.opendev.org/vexxhost/ansible-collection-atmosphere/ansible-collection-atmosphere-wheels-{{ openstack_release }}-master.tar.gz"
when: wheels_path is not defined
- name: Build the images
ansible.builtin.include_role:
name: build-docker-image
register: _build_docker_image
loop: "{{ images_to_build }}"
vars:
zuul_work_dir: "{{ zuul.projects['opendev.org/openstack/loci'].src_dir }}"
docker_registry: "{{ image_manifest.registry }}"
docker_images:
- context: .
repository: "{{ item }}"
tags:
- "{{ image_manifest['projects'][item]['tag'] }}-{{ ansible_architecture }}"
build_args:
# TODO(mnaser): build base image
- FROM="ubuntu:focal"
- PROJECT="{{ item }}"
- PROJECT_REF="{{ image_manifest['projects'][item]['revision'] }}"
- PROJECT_RELEASE="{{ openstack_release }}"
- WHEELS="{{ wheels_path }}"
- PROFILES="{{ image_manifest['projects'][item].get('profies', []) | join(' ') }}"
- PIP_PACKAGES="{{ image_manifest['projects'][item].get('pip_packages', []) | join(' ') }}"
- DIST_PACKAGES="{{ image_manifest['projects'][item].get('dist_packages', []) | join(' ') }}"
- name: Upload the images
ansible.builtin.include_role:
name: upload-docker-image
register: _build_docker_image
when: zuul.job is search("upload")
loop: "{{ images_to_build }}"
vars:
zuul_work_dir: "{{ zuul.projects['opendev.org/openstack/loci'].src_dir }}"
docker_registry: "{{ image_manifest.registry }}"
docker_images:
- context: .
repository: "{{ item }}"
tags:
- "{{ image_manifest['projects'][item]['tag'] }}-{{ ansible_architecture }}"
- name: Return artifacts to Zuul
block:
- name: Return artifacts to Zuul for build
when: zuul.job is search("build")
zuul_return:
data:
zuul:
artifacts:
- name: "{{ item }} ({{ ansible_architecture }})"
url: "docker://{{ _docker_registry }}/{{ item }}:{{ image_manifest['projects'][item]['tag'] }}-{{ ansible_architecture }}"
metadata:
type: image
repository: "docker://{{ _docker_registry }}/{{ item }}"
project: "{{ item }}"
arch: "{{ ansible_architecture }}"
vars:
_docker_registry: "zuul-jobs.buildset-registry:5000"
loop: "{{ _build_docker_image.results | map(attribute='item') | list }}"
- name: Return artifacts to Zuul for upload
when: zuul.job is search("upload")
zuul_return:
data:
zuul:
artifacts:
- name: "{{ item }} ({{ ansible_architecture }})"
url: "docker://{{ image_manifest.registry }}/{{ item }}:{{ ('change_' + zuul.change) if (zuul.change is defined) else zuul.pipeline }}_{{ image_manifest['projects'][item]['tag'] }}-{{ ansible_architecture }}"
metadata:
type: image
repository: "docker://{{ image_manifest.registry }}/{{ item }}"
project: "{{ item }}"
arch: "{{ ansible_architecture }}"
loop: "{{ _build_docker_image.results | map(attribute='item') | list }}"
- name: Return artifacts to Zuul for promote
when: zuul.job is search("promote")
zuul_return:
data:
zuul:
artifacts:
- name: "{{ item }} ({{ ansible_architecture }})"
url: "docker://{{ image_manifest.registry }}/{{ item }}:{{ image_manifest['projects'][item]['tag'] }}-{{ ansible_architecture }}"
metadata:
type: image
repository: "docker://{{ image_manifest.registry }}/{{ item }}"
project: "{{ item }}"
arch: "{{ ansible_architecture }}"
loop: "{{ _build_docker_image.results | map(attribute='item') | list }}"