Skip to content

Commit d258fad

Browse files
author
Kroner
committed
add ansible | fix pipeline for ansible
1 parent 903d190 commit d258fad

File tree

9 files changed

+124
-4
lines changed

9 files changed

+124
-4
lines changed

.github/workflows/main.yaml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ name: ci-cd
33
on:
44
push:
55
branches:
6-
- main # Запускать пайплайн при пуше в ветку main
7-
- v0.3
6+
#- main # Запускать пайплайн при пуше в ветку main
7+
- v0.4
88
workflow_dispatch:
99

1010
jobs:
11-
test:
11+
tests:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v4
@@ -59,4 +59,32 @@ jobs:
5959
6060
- name: Cleanup (containers down)
6161
if: always()
62-
run: docker compose down
62+
run: docker compose down
63+
64+
deploy:
65+
runs-on: ubuntu-latest
66+
#needs: tests
67+
steps:
68+
- name: Checkout code
69+
uses: actions/checkout@v3
70+
71+
- name: Resolve DNS
72+
run: |
73+
echo "Проверка DNS:"
74+
nslookup ${{ secrets.SERVER_DNS }}
75+
76+
- name: Set up SSH key
77+
run: |
78+
mkdir -p ~/.ssh
79+
echo "${{ secrets.SSH_PRIVATE_KEY }}" | base 64 > ~/.ssh/webserver
80+
chmod 600 ~/.ssh/webserver
81+
# Добавляем сервер в known_hosts (замените your-server-ip)
82+
ssh-keyscan -T 10 ${{ secrets.SERVER_DNS }} >> ~/.ssh/known_hosts
83+
84+
- name: Install ansible on runner
85+
run: pip install ansible
86+
87+
- name: Starting ansible
88+
run: |
89+
cd ansible/
90+
ansible-playbook playbook.yaml

ansible/ansible.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[defaults]
2+
inventory = inventory/hosts # (опционально) автоматически подхватывает инвентарь
3+
private_key_file = ~/.ssh/webserver # SSH-ключ
4+
remote_user = deployer # пользователь для подключения
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# Настройки специфичные для production
3+
app_dirr: /opt/goserv
4+
docker_users:
5+
- deploy_user
6+
- www-data
7+
max_connections: 100

ansible/inventory/hosts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[localhost]
2+
127.0.0.1 ansible_connection=local # для локального запуска
3+
4+
[webserver]
5+
goserv.duckdns.org

ansible/playbook.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: Deploy webserver via Docker Compose
3+
hosts: "{{ target | default('webserver') }}" # переменная для выбора цели
4+
become: yes
5+
6+
roles:
7+
- role: docker
8+
- role: app
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
app_repo_url: "https://github.com/Kron-x/web_go.git"
3+
app_dir: "{{ ansible_user_dir }}/server"
4+
app_branch: "main"

ansible/roles/app/tasks/main.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: Check if repo exists
3+
stat:
4+
path: "{{ app_dir }}/.git"
5+
register: repo_exists
6+
7+
- name: Check app_dir
8+
run: echo {{ app_dir }}
9+
10+
- name: Clone or update repo
11+
git:
12+
repo: "{{ app_repo_url }}"
13+
dest: "{{ app_dir }}"
14+
update: "{{ repo_exists.stat.exists }}"
15+
force: yes
16+
version: "{{ app_branch }}"
17+
18+
- name: Run Docker Compose
19+
command: docker compose up -d --force-recreate
20+
args:
21+
chdir: "{{ app_dir }}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
docker_key_dir: /usr/share/keyrings
3+
docker_repo_url: "https://download.docker.com/linux/ubuntu"
4+
docker_repo_distribution: noble
5+
docker_repo_component: stable
6+
docker_packages:
7+
- docker-ce
8+
- docker-ce-cli
9+
- containerd.io
10+
- docker-compose-plugin
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: Ensure keyring directory exists
3+
file:
4+
path: "{{ docker_key_dir }}"
5+
state: directory
6+
mode: 0755
7+
8+
- name: Download Docker GPG key (proper way)
9+
ansible.builtin.get_url:
10+
url: "{{ docker_repo_url }}/gpg"
11+
dest: "{{ docker_key_dir }}/docker-archive-keyring.gpg"
12+
mode: "0644"
13+
14+
- name: Add Docker repository (secure way)
15+
ansible.builtin.apt_repository:
16+
repo: "deb [arch=amd64 signed-by={{ docker_key_dir }}/docker-archive-keyring.gpg] \
17+
{{ docker_repo_url }} \
18+
{{ docker_repo_distribution }} \
19+
{{ docker_repo_component }}"
20+
state: present
21+
filename: "docker"
22+
23+
- name: Install Docker & Docker Compose (v2)
24+
apt:
25+
name: "{{ docker_packages }}"
26+
state: present
27+
update_cache: yes
28+
29+
- name: Ensure Docker is running
30+
service:
31+
name: docker
32+
state: started
33+
enabled: yes

0 commit comments

Comments
 (0)