Skip to content

Commit 3b71b43

Browse files
committed
add first app without compose
1 parent aa0ddba commit 3b71b43

17 files changed

Lines changed: 2262 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Deploy Node.js App with Ansible
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
environment: dev
12+
13+
env:
14+
IMAGE_NAME: docker.io/therealpad/my-node-app
15+
CONTAINER_NAME: my-node-app
16+
PORT: 3000
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Log in to Docker Hub
23+
uses: docker/login-action@v2
24+
with:
25+
username: ${{ secrets.DOCKERHUB_USERNAME }}
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
28+
- name: Build Docker image
29+
run: |
30+
docker build -t $IMAGE_NAME:latest ./server
31+
32+
- name: Push Docker image
33+
run: |
34+
docker push $IMAGE_NAME:latest
35+
36+
- name: Set up Python
37+
uses: actions/setup-python@v4
38+
with:
39+
python-version: "3.x"
40+
41+
- name: Install Ansible
42+
run: |
43+
python -m pip install --upgrade pip
44+
pip install ansible
45+
46+
- name: Set up SSH
47+
uses: webfactory/ssh-agent@v0.9.0
48+
with:
49+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
50+
51+
- name: Add target host to known_hosts
52+
run: |
53+
HOST_IP=$(grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' inventory.ini | head -n 1)
54+
ssh-keyscan $HOST_IP >> ~/.ssh/known_hosts
55+
56+
- name: Run Ansible Playbook
57+
run: |
58+
ansible-playbook -i inventory.ini setup.yml \
59+
--extra-vars "node_username=${{ secrets.NODE_USERNAME }} \
60+
node_password=${{ secrets.NODE_PASSWORD }} \
61+
secret_message='${{ secrets.SECRET_MESSAGE }}'"
62+
env:
63+
ANSIBLE_HOST_KEY_CHECKING: "False"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules/

inventory.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[webservers]
2+
164.92.164.107 ansible_user=root ansible_python_interpreter=/usr/bin/python3 ansible_ssh_private_key_file="/Users/pierre-alexandredelgado/.ssh/id_ed25519_test_1"

roles/app/tasks/main.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
- name: Clone the repository
3+
git:
4+
repo: "{{ app_repo }}"
5+
dest: "{{ app_path }}"
6+
version: main
7+
force: yes
8+
9+
- name: Dummy build step
10+
command: npm install
11+
args:
12+
chdir: "{{ app_path }}/server"
13+
14+
- name: Pull server image
15+
community.docker.docker_image:
16+
name: therealpad/my-node-app
17+
source: pull
18+
19+
- name: Build image and with build args
20+
community.docker.docker_image:
21+
name: "{{ app_name }}"
22+
build:
23+
path: "{{ app_path }}/server"
24+
source: build
25+
26+
- name: Create systemd service to run Node.js Docker container
27+
copy:
28+
dest: "/etc/systemd/system/{{ app_name }}.service"
29+
content: |
30+
[Unit]
31+
Description=Docker Container {{ app_name }}
32+
After=docker.service
33+
Requires=docker.service
34+
35+
[Service]
36+
Restart=always
37+
RestartSec=5
38+
ExecStartPre=-/usr/bin/docker stop {{ app_name }}
39+
ExecStartPre=-/usr/bin/docker rm {{ app_name }}
40+
ExecStart=/usr/bin/docker run \
41+
-p 3000:3000 \
42+
-e USERNAME={{ node_username }} \
43+
-e PASSWORD={{ node_password }} \
44+
-e SECRET_MESSAGE='{{ secret_message }}' \
45+
{{ app_name }}
46+
ExecStop=/usr/bin/docker stop {{ app_name }}
47+
48+
[Install]
49+
WantedBy=multi-user.target
50+
51+
- name: Reload systemd to pick up new service
52+
command: systemctl daemon-reload
53+
54+
- name: Enable and start the Node.js Docker container service
55+
systemd:
56+
name: "{{ app_name }}"
57+
enabled: yes
58+
state: started
59+
60+
- name: Restart Node.js Docker container service to load new code
61+
systemd:
62+
name: "{{ app_name }}"
63+
state: restarted

roles/app/vars/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
app_name: nodejs-service
2+
app_repo: "https://github.com/DevOps-PA-resume/Multi-Container-Application"
3+
app_path: "/var/www/nodejs-service"
4+
node_app_port: 3000
5+
node_username: "admin"
6+
node_password: "password"
7+
secret_message: "c kool ansible (multi container application)"

roles/base/tasks/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
- name: Update APT packages
3+
apt:
4+
update_cache: yes
5+
upgrade: dist
6+
7+
- name: Install useful packages
8+
apt:
9+
name:
10+
- curl
11+
- git
12+
- ufw
13+
- fail2ban
14+
- nodejs
15+
- npm
16+
- docker.io
17+
state: present
18+

roles/nginx/tasks/main.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
- name: Install Nginx
3+
apt:
4+
name: nginx
5+
state: present
6+
7+
- name: Copy Nginx configuration
8+
template:
9+
src: nginx.conf.j2
10+
dest: /etc/nginx/sites-available/mysite.conf
11+
mode: '0644'
12+
13+
- name: Enable Nginx site
14+
file:
15+
src: /etc/nginx/sites-available/mysite.conf
16+
dest: /etc/nginx/sites-enabled/mysite.conf
17+
state: link
18+
force: yes
19+
20+
- name: Remove default Nginx site
21+
file:
22+
path: /etc/nginx/sites-enabled/default
23+
state: absent
24+
25+
- name: Test Nginx configuration
26+
command: nginx -t
27+
register: nginx_test
28+
changed_when: false
29+
failed_when: nginx_test.rc != 0
30+
31+
- name: Reload Nginx
32+
systemd:
33+
name: nginx
34+
state: reloaded
35+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
server {
2+
listen 80;
3+
server_name yourdomain.com; # or your server's IP if you don't have a domain
4+
5+
location / {
6+
proxy_pass http://127.0.0.1:3000; # send requests to your Node.js app
7+
proxy_http_version 1.1;
8+
proxy_set_header Upgrade $http_upgrade;
9+
proxy_set_header Connection 'upgrade';
10+
proxy_set_header Host $host;
11+
proxy_cache_bypass $http_upgrade;
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICmOLYq0aniAjx1b9QQj7i9bK97k+rVktbhHbTD1MzPF delgadopierrealexandre@gmail.com

roles/ssh/tasks/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
- name: Ensure .ssh directory exists
3+
become: yes
4+
file:
5+
path: /home/ubuntu/.ssh
6+
state: directory
7+
owner: root
8+
group: root
9+
mode: '0700'
10+
11+
- name: Add public SSH key
12+
become: yes
13+
authorized_key:
14+
user: root
15+
state: present
16+
key: "{{ lookup('file', 'id_ed25519_test_1.pub') }}"
17+

0 commit comments

Comments
 (0)