Skip to content

Commit d1ec772

Browse files
Merge pull request #176 from idealista/hotfix/175
#175 add error handling on deploy playbook
2 parents eb26529 + 2ab15d5 commit d1ec772

File tree

6 files changed

+65
-36
lines changed

6 files changed

+65
-36
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch
1010
- *[#160](https://github.com/idealista/java_role/issues/160) Remove packer for playbook for the task of pushing images to Dockerhub* @aren-pulid0
1111
- *[#165](https://github.com/idealista/java_role/issues/165) Add JAVA_HOME as ENV in docker image* @aren-pulid0
1212
- *[#167](https://github.com/idealista/java_role/issues/167) Support for multiarch images* @aren-pulid0
13+
- *[#175](https://github.com/idealista/java_role/issues/175) Error handling on deploy playbook @aren-pulid0
14+
1315

1416
## [7.0.2](https://github.com/idealista/java_role/tree/7.0.2) (2021-08-19)
1517
[Full Changelog](https://github.com/idealista/java_role/compare/7.0.1...7.0.2)

dockerhub/Dockerfile.j2

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
FROM {{ docker_image_base }}
22

3-
RUN rm -rf /var/lib/apt/lists/* && apt-get update \
4-
&& apt-get install -y sudo systemd init systemd-sysv \
5-
&& apt-get autoremove -y && apt-get clean
6-
7-
RUN mkdir -p /usr/share/man/man1
83

9-
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python3 sudo bash ca-certificates && apt-get clean; \
4+
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python3 sudo bash ca-certificates systemd init systemd-sysv && apt-get clean; \
105
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python36 sudo python3-dnf bash && dnf clean all; \
116
elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
127
elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \

dockerhub/deploy_images.yml

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
---
22

3-
- name: Get JAVA_HOME variable
4-
community.docker.docker_container_exec:
5-
container: "{{ 'jdk' + '-' + item }}"
6-
command: grep -i -o 'JAVA_HOME.*' /etc/profile.d/jdk.sh
7-
register: java_home
3+
- name: Commit environment variables into containers
4+
block:
5+
- name: Get JAVA_HOME variable
6+
community.docker.docker_container_exec:
7+
container: "{{ 'jdk' + '-' + item }}"
8+
command: grep -i -o 'JAVA_HOME.*' /etc/profile.d/jdk.sh
9+
register: java_home
810

9-
- set_fact: java_home="{{ java_home.stdout }}"
11+
- set_fact: java_home="{{ java_home.stdout }}"
1012

11-
- name: Commit container JAVA_HOME variable to image
12-
command: docker commit --change "ENV {{ java_home }}" {{ 'jdk' + '-' + item }} idealista/jdk:{{ docker_tag + '-' + item}}
13+
- name: Commit container JAVA_HOME variable to image
14+
command: docker commit --change "ENV {{ java_home }}" {{ 'jdk' + '-' + item }} idealista/jdk:{{ docker_tag + '-' + item}}
1315

14-
- name: Log into Docker Hub
15-
docker_login:
16-
email: "{{ docker_hub_email }}"
17-
username: "{{ docker_hub_username }}"
18-
password: "{{ docker_hub_password }}"
16+
- name: Log into Docker Hub
17+
docker_login:
18+
email: "{{ docker_hub_email }}"
19+
username: "{{ docker_hub_username }}"
20+
password: "{{ docker_hub_password }}"
1921

20-
- name: Tag and push to Docker Hub
21-
command: docker push idealista/jdk:{{ docker_tag + '-' + item }}
22+
- name: Tag and push to Docker Hub
23+
command: docker push idealista/jdk:{{ docker_tag + '-' + item }}
2224

23-
- name: Remove the container
24-
docker_container:
25-
name: "{{ 'jdk' + '-' + item }}"
26-
state: absent
25+
- name: Remove the container
26+
docker_container:
27+
name: "{{ 'jdk' + '-' + item }}"
28+
state: absent
29+
30+
when: "failed_architectures is not defined or failed_architectures | join('|') | regex_search(item) != item"

dockerhub/execute_role.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
3+
- name: Execute role
4+
block:
5+
- include_role:
6+
name: java_role
7+
rescue:
8+
- set_fact:
9+
failed_execution: True
10+
- debug:
11+
msg: "The container {{ ansible_nodename }} couldn't execute roles correctly."
12+

dockerhub/group_vars/all/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dockerhub_image_architectures: ['amd64', 'arm64']

dockerhub/main.yml

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Create Dockerfile, images and build containers
33
hosts: localhost
4-
connection: local
54
gather_facts: false
65
tasks:
76
- name: Create Dockerfile
@@ -24,29 +23,36 @@
2423

2524
- name: Build containers
2625
include_tasks: build_containers.yml
27-
with_items: ['amd64', 'arm64']
26+
with_items: "{{ dockerhub_image_architectures }}"
2827

2928

3029
- name: Execute Java role in Docker containers
3130
hosts: jdk_containers
32-
connection: local
31+
connection: localhost
32+
gather_facts: True
3333
vars:
3434
- java_jdk_vendor: "{{ jdk_vendor if jdk_vendor is defined and jdk_vendor is not sameas None and jdk_vendor != '' else 'openjdk' }}"
3535
- java_open_jdk_version: "{{ jdk_version if jdk_version is defined and jdk_version is not sameas None and jdk_version != '' }}"
3636
- java_open_jdk_version_major: "{{ jdk_major if jdk_major is defined and jdk_major is not sameas None and jdk_major != ''}}"
37-
roles:
38-
- java_role
39-
37+
tasks:
38+
- name: Execute role
39+
include_tasks: execute_role.yml
4040

4141
- name: Deploy images to DockerHub
4242
hosts: localhost
4343
connection: local
44-
gather_facts: false
44+
gather_facts: True
4545
tasks:
4646

47+
- name: Check failed containers
48+
set_fact:
49+
failed_architectures: "{{ failed_architectures + [ item ] }}"
50+
with_items: "{{ groups['jdk_containers'] }}"
51+
when: "{{ hostvars[item]['failed_execution'] is defined and hostvars[item]['failed_execution'] == True }}"
52+
4753
- name: Deploy images
4854
include_tasks: deploy_images.yml
49-
with_items: ['amd64', 'arm64']
55+
with_items: "{{ dockerhub_image_architectures }}"
5056

5157

5258
- name: Deploy multiarch manifest to DockerHub and cleanup
@@ -56,12 +62,21 @@
5662
tasks:
5763

5864
- name: Create manifest
59-
command: docker manifest create idealista/jdk:{{ docker_tag }}
60-
--amend idealista/jdk:{{ docker_tag + '-amd64' }}
61-
--amend idealista/jdk:{{ docker_tag + '-arm64' }}
65+
command: docker manifest create idealista/jdk:{{ docker_tag }}
66+
{% for architecture in dockerhub_image_architectures %}
67+
{% if failed_architectures is not defined or not failed_architectures | join('|') | regex_search(architecture) %}
68+
--amend idealista/jdk:{{ docker_tag + '-' + architecture }}
69+
{% endif %}
70+
{% endfor %}
6271

6372
- name: Push manifest
6473
command: docker manifest push idealista/jdk:{{ docker_tag }}
6574

6675
- name: Delete builder
6776
command: docker buildx rm multiarch
77+
78+
- name: Example using fail and when together
79+
fail:
80+
msg: Atleast one of the architectures could not be uploaded see logs for more information.
81+
when: failed_architectures is defined and failed_architectures | length != 0
82+

0 commit comments

Comments
 (0)