Skip to content

Commit 01828fe

Browse files
committed
Ansible integration test 1
1 parent 54295b7 commit 01828fe

2 files changed

Lines changed: 80 additions & 29 deletions

File tree

.github/workflows/backend.yml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ on:
44
paths:
55
- 'backend/**'
66
- '.github/workflows/backend.yml'
7+
- 'playbook.yml'
78
branches: [main]
89

910
jobs:
1011
deploy:
1112
runs-on: ubuntu-latest
1213
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v4
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup Ansible
17+
uses: ansible/ansible-action@master
1518
with:
16-
fetch-depth: 0
19+
requirements: ansible-core==2.12
1720

18-
- name: Deploy via SSH
19-
uses: appleboy/ssh-action@v1
21+
- name: Run Ansible Playbook
22+
uses: ansible/ansible-action@master
2023
with:
21-
host: ${{ secrets.SERVER_IP }}
22-
username: ${{ secrets.SERVER_USER }}
23-
key: ${{ secrets.SERVER_SSH_KEY }}
24-
script: |
25-
cd /opt/CookieLess/backend
26-
git pull origin main
27-
sudo docker-compose down
28-
sudo docker-compose up -d --build
24+
playbook: playbook.yml
25+
inventory: |
26+
[production]
27+
${{ secrets.SERVER_IP }}
28+
extra-vars: |
29+
MONGO_INITDB_ROOT_USERNAME: ${{ secrets.MONGO_INITDB_ROOT_USERNAME }}
30+
MONGO_INITDB_ROOT_PASSWORD: ${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}

backend/ansible/playbook.yml

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,80 @@
11
---
2-
- hosts: all
2+
- name: Configure CookieLess backend
3+
hosts: all
34
become: yes
5+
vars:
6+
app_dir: /opt/CookieLess
7+
backup_dir: /opt/backups
8+
cron_backup: "0 3 * * *" # Daily at 3 AM
9+
410
tasks:
11+
# Base system setup
12+
- name: Update apt cache
13+
apt:
14+
update_cache: yes
15+
16+
# Docker installation
517
- name: Install Docker
618
apt:
719
name: docker.io
820
state: present
921

1022
- name: Install Docker Compose
11-
get_url:
12-
url: https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-linux-x86_64
23+
ansible.builtin.get_url:
24+
url: https://github.com/docker/compose/releases/download/v2.28.1/docker-compose-linux-x86_64
1325
dest: /usr/local/bin/docker-compose
1426
mode: '0755'
1527

16-
- name: Copy backend code
17-
copy:
18-
src: ../backend
19-
dest: /opt/CookieLess
28+
# Application setup
29+
- name: Create application directory
30+
ansible.builtin.file:
31+
path: "{{ app_dir }}"
32+
state: directory
33+
mode: '0755'
34+
35+
- name: Synchronize backend code
36+
ansible.builtin.synchronize:
37+
src: ../backend/
38+
dest: "{{ app_dir }}"
39+
delete: yes
40+
41+
- name: Create .env file
42+
ansible.builtin.template:
43+
src: templates/env.j2
44+
dest: "{{ app_dir }}/.env"
45+
46+
# MongoDB backup setup
47+
- name: Create backup directory
48+
ansible.builtin.file:
49+
path: "{{ backup_dir }}"
50+
state: directory
51+
mode: '0755'
52+
53+
- name: Install MongoDB backup cron
54+
ansible.builtin.cron:
55+
name: "MongoDB daily backup"
56+
job: "docker exec cookieless-mongodb-1 mongodump --archive={{ backup_dir }}/backup-$(date +\\%F).gz --gzip"
57+
cron_file: mongodb_backup
58+
user: root
59+
minute: "{{ cron_backup.split()[0] }}"
60+
hour: "{{ cron_backup.split()[1] }}"
2061

21-
- name: Create .env file for docker-compose
22-
copy:
23-
dest: /opt/CookieLess/backend/.env
24-
content: |
25-
MONGO_INITDB_ROOT_USERNAME={{ lookup('env', 'MONGO_INITDB_ROOT_USERNAME') }}
26-
MONGO_INITDB_ROOT_PASSWORD={{ lookup('env', 'MONGO_INITDB_ROOT_PASSWORD') }}
27-
MONGO_HOST=mongodb
28-
MONGO_PORT=27017
62+
# Security setup
63+
- name: Configure UFW firewall
64+
ufw:
65+
rule: allow
66+
port: "{{ item }}"
67+
proto: tcp
68+
loop:
69+
- 7880
70+
- 7881
71+
- 9333
2972

73+
# Application deployment
3074
- name: Start containers
31-
command: docker-compose -f /opt/CookieLess/docker-compose.yml up -d
75+
community.docker.docker_compose:
76+
project_src: "{{ app_dir }}"
77+
build: yes
78+
restart: yes
79+
recreate: always
80+
remove_orphans: yes

0 commit comments

Comments
 (0)