-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansible.yml
77 lines (76 loc) · 2.82 KB
/
ansible.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
- name: Spectator Continuous Delivery
user: "{{ lookup('env', 'SSH_USERNAME') }}"
hosts: servers
become: true
vars:
ssh_ip: "{{ lookup('env', 'SSH_IP') }}"
ssh_username: "{{ lookup('env', 'SSH_USERNAME') }}"
git_token: "{{ lookup('env', 'GITHUB_TOKEN') }}"
git_ref: "{{ lookup('env', 'GITHUB_REF') }}"
git_sha: "{{ lookup('env', 'GITHUB_SHA') }}"
tasks:
- name: ping to server
ansible.builtin.ping:
# We check if the server is up
- name: docker enabled and running
ansible.builtin.service:
name: docker
enabled: true
state: started
- name: check if the git directory exists
ansible.builtin.command:
cmd: ls /home/{{ ssh_username }}/spectator
changed_when: false
register: directory_exists
# We check if there's a running docker container
- name: check current running docker containers
ansible.builtin.command:
cmd: docker compose ps
chdir: /home/{{ lookup('env', 'SSH_USERNAME') }}/spectator
when: directory_exists.stdout.find('docker-compose.yml') != -1
register: docker_exists
# If there is one, we stop it
# See: https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html
- name: stop current running docker container
ansible.builtin.command:
cmd: docker compose down
chdir: /home/{{ lookup('env', 'SSH_USERNAME') }}/spectator
when:
- docker_exists is succeeded
- docker_exists.stdout is defined
- docker_exists.stdout.find('running') != -1
# After we stop the docker container, we check the directory that has the git repository
# If it's exists, we delete it
- name: remove the old spectator directory
ansible.builtin.file:
state: absent
path: /home/{{ lookup('env', 'SSH_USERNAME') }}/spectator
when:
- directory_exists.stdout.find('docker-compose.yml') != -1
- name: pull git repository
ansible.builtin.git:
clone: true
repo: "https://{{ git_token }}@github.com/teknologi-umum/spectator.git"
dest: /home/{{ ssh_username }}/spectator
refspec: "{{ git_ref }}"
version: "{{ git_sha }}"
# Run the docker container
- name: build the docker container
ansible.builtin.command:
cmd: docker compose up --no-start
chdir: /home/{{ ssh_username }}/spectator
changed_when: false
- name: start the docker container
ansible.builtin.command:
cmd: docker compose start
chdir: /home/{{ ssh_username }}/spectator
changed_when: false
- name: cleanup dangling images
ansible.builtin.command:
cmd: docker image prune -f
changed_when: false
- name: cleanup dangling build caches
ansible.builtin.command:
cmd: docker builder prune -f
changed_when: false