Skip to content

Commit

Permalink
Merge pull request #176 from idealista/hotfix/175
Browse files Browse the repository at this point in the history
 #175 add error handling on deploy playbook
  • Loading branch information
antonioarenzanapulido authored Feb 28, 2022
2 parents eb26529 + 2ab15d5 commit d1ec772
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch
- *[#160](https://github.com/idealista/java_role/issues/160) Remove packer for playbook for the task of pushing images to Dockerhub* @aren-pulid0
- *[#165](https://github.com/idealista/java_role/issues/165) Add JAVA_HOME as ENV in docker image* @aren-pulid0
- *[#167](https://github.com/idealista/java_role/issues/167) Support for multiarch images* @aren-pulid0
- *[#175](https://github.com/idealista/java_role/issues/175) Error handling on deploy playbook @aren-pulid0


## [7.0.2](https://github.com/idealista/java_role/tree/7.0.2) (2021-08-19)
[Full Changelog](https://github.com/idealista/java_role/compare/7.0.1...7.0.2)
Expand Down
7 changes: 1 addition & 6 deletions dockerhub/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
FROM {{ docker_image_base }}

RUN rm -rf /var/lib/apt/lists/* && apt-get update \
&& apt-get install -y sudo systemd init systemd-sysv \
&& apt-get autoremove -y && apt-get clean

RUN mkdir -p /usr/share/man/man1

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; \
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; \
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python36 sudo python3-dnf bash && dnf clean all; \
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; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \
Expand Down
42 changes: 23 additions & 19 deletions dockerhub/deploy_images.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
---

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

- set_fact: java_home="{{ java_home.stdout }}"
- set_fact: java_home="{{ java_home.stdout }}"

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

- name: Log into Docker Hub
docker_login:
email: "{{ docker_hub_email }}"
username: "{{ docker_hub_username }}"
password: "{{ docker_hub_password }}"
- name: Log into Docker Hub
docker_login:
email: "{{ docker_hub_email }}"
username: "{{ docker_hub_username }}"
password: "{{ docker_hub_password }}"

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

- name: Remove the container
docker_container:
name: "{{ 'jdk' + '-' + item }}"
state: absent
- name: Remove the container
docker_container:
name: "{{ 'jdk' + '-' + item }}"
state: absent

when: "failed_architectures is not defined or failed_architectures | join('|') | regex_search(item) != item"
12 changes: 12 additions & 0 deletions dockerhub/execute_role.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---

- name: Execute role
block:
- include_role:
name: java_role
rescue:
- set_fact:
failed_execution: True
- debug:
msg: "The container {{ ansible_nodename }} couldn't execute roles correctly."

1 change: 1 addition & 0 deletions dockerhub/group_vars/all/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dockerhub_image_architectures: ['amd64', 'arm64']
37 changes: 26 additions & 11 deletions dockerhub/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
- name: Create Dockerfile, images and build containers
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Create Dockerfile
Expand All @@ -24,29 +23,36 @@

- name: Build containers
include_tasks: build_containers.yml
with_items: ['amd64', 'arm64']
with_items: "{{ dockerhub_image_architectures }}"


- name: Execute Java role in Docker containers
hosts: jdk_containers
connection: local
connection: localhost
gather_facts: True
vars:
- java_jdk_vendor: "{{ jdk_vendor if jdk_vendor is defined and jdk_vendor is not sameas None and jdk_vendor != '' else 'openjdk' }}"
- java_open_jdk_version: "{{ jdk_version if jdk_version is defined and jdk_version is not sameas None and jdk_version != '' }}"
- java_open_jdk_version_major: "{{ jdk_major if jdk_major is defined and jdk_major is not sameas None and jdk_major != ''}}"
roles:
- java_role

tasks:
- name: Execute role
include_tasks: execute_role.yml

- name: Deploy images to DockerHub
hosts: localhost
connection: local
gather_facts: false
gather_facts: True
tasks:

- name: Check failed containers
set_fact:
failed_architectures: "{{ failed_architectures + [ item ] }}"
with_items: "{{ groups['jdk_containers'] }}"
when: "{{ hostvars[item]['failed_execution'] is defined and hostvars[item]['failed_execution'] == True }}"

- name: Deploy images
include_tasks: deploy_images.yml
with_items: ['amd64', 'arm64']
with_items: "{{ dockerhub_image_architectures }}"


- name: Deploy multiarch manifest to DockerHub and cleanup
Expand All @@ -56,12 +62,21 @@
tasks:

- name: Create manifest
command: docker manifest create idealista/jdk:{{ docker_tag }}
--amend idealista/jdk:{{ docker_tag + '-amd64' }}
--amend idealista/jdk:{{ docker_tag + '-arm64' }}
command: docker manifest create idealista/jdk:{{ docker_tag }}
{% for architecture in dockerhub_image_architectures %}
{% if failed_architectures is not defined or not failed_architectures | join('|') | regex_search(architecture) %}
--amend idealista/jdk:{{ docker_tag + '-' + architecture }}
{% endif %}
{% endfor %}

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

- name: Delete builder
command: docker buildx rm multiarch

- name: Example using fail and when together
fail:
msg: Atleast one of the architectures could not be uploaded see logs for more information.
when: failed_architectures is defined and failed_architectures | length != 0

0 comments on commit d1ec772

Please sign in to comment.