Files
playbooks/roles/koodiklinikka.fi-api/tasks/main.yml
2015-03-30 19:33:22 +03:00

57 lines
1.9 KiB
YAML

---
- name: Deploy client from git
remote_user: "{{ koodiklinikka_api_user }}"
action: >
git
repo="{{ koodiklinikka_api_repository_url }}"
dest="{{ koodiklinikka_api_app_path }}"
accept_hostkey=True
sudo: no
register: gitclone
- name: get previous container
shell: docker images|grep {{ koodiklinikka_api_container_name }}|grep latest| awk '{print $3}'
register: current_image_sha
- name: Build {{ koodiklinikka_api_container_name }}
shell: ./build_docker /opt
args:
chdir: "{{ koodiklinikka_api_app_path}}"
sudo: yes
when: gitclone.changed
register: container
- name: tag previous version
shell: docker tag -f {{ current_image_sha.stdout }} {{ koodiklinikka_api_container_name }}:old
when: container is defined and container.changed and current_image_sha.stdout != ""
- name: get running container
shell: docker ps |grep {{ koodiklinikka_api_container_name }}| awk '{print $1}'
register: current_container_id
- name: kill old version
shell: docker stop {{ koodiklinikka_api_container_name }}
when: container is defined and container.changed and current_container_id.stdout != ""
- name: get old containers
shell: docker ps -a|grep Exited| awk '{print $1}'
register: old_containers
- name: clean old containers
shell: docker rm $(docker ps -a|grep Exited| awk '{print $1}')
when: old_containers.stdout != ""
- name: Start the container
shell: docker run --restart=always -p 127.0.0.1:{{ koodiklinikka_api_port }}:{{ koodiklinikka_api_port }} -d --name {{ koodiklinikka_api_container_name }} {{ koodiklinikka_api_container_name }}:latest
when: container is defined and container.changed
- name: get old images
shell: docker images | grep "^<none>" | awk '{print $3}'
register: old_images
- name: clean untagged (old) images
shell: docker rmi $(docker images -a | grep "^<none>" | awk '{print $3}')
when: old_images.stdout != ""
ignore_errors: yes