Skip to content

Commit 06dc483

Browse files
committedDec 14, 2024·
feat(AdsService): supercharge docker build
1 parent 6b7eba3 commit 06dc483

File tree

4 files changed

+128
-69
lines changed

4 files changed

+128
-69
lines changed
 

‎.github/workflows/docker.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Build and Deploy Microservices
2+
3+
on:
4+
push:
5+
branches: ["deploy"]
6+
7+
jobs:
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
13+
- name: Copy context to server
14+
uses: burnettk/rsync-deployments@5.2
15+
with:
16+
switches: -avz --delete
17+
path: .
18+
remote_destination: ubuntu@37.139.41.110:/opt/backend-deploy/
19+
remote_host: 37.139.41.110
20+
private_key: ${{ secrets.SSH_PRIVATE_KEY }}
21+
22+
- name: Build and deploy on server
23+
uses: appleboy/ssh-action@master
24+
with:
25+
host: 37.139.41.110
26+
username: ubuntu
27+
key: ${{ secrets.SSH_PRIVATE_KEY }}
28+
script: |
29+
cd /opt/backend-deploy/
30+
sudo docker-compose pull # Pull latest images if you are using a registry
31+
sudo docker-compose build --no-cache # Build services
32+
sudo docker-compose up -d --remove-orphans # Start services in detached mode

‎.github/workflows/simple-build.yml

+64-64
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
# name: Simple Build
1+
name: Simple Build
22

3-
# on:
4-
# push:
5-
# branches: ["deploy"]
3+
on:
4+
push:
5+
branches: ["deploy"]
66

7-
# jobs:
8-
# build:
9-
# runs-on: ubuntu-latest
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
1010

11-
# steps:
12-
# - name: Install Go
13-
# uses: actions/setup-go@v4
14-
# with:
15-
# go-version: 1.22.x
11+
steps:
12+
- name: Install Go
13+
uses: actions/setup-go@v4
14+
with:
15+
go-version: 1.22.x
1616

17-
# - name: Checkout code
18-
# uses: actions/checkout@v4
17+
- name: Checkout code
18+
uses: actions/checkout@v4
1919

20-
# - name: Build
21-
# run: make build
20+
- name: Build
21+
run: make build
2222

23-
# - name: Upload build result
24-
# uses: actions/upload-artifact@v4
25-
# with:
26-
# name: bin
27-
# path: ./bin
23+
- name: Upload build result
24+
uses: actions/upload-artifact@v4
25+
with:
26+
name: bin
27+
path: ./bin
2828

29-
# deploy:
30-
# runs-on: ubuntu-latest
31-
# needs: build
32-
# steps:
33-
# - name: Download bin file
34-
# uses: actions/download-artifact@v4
35-
# with:
36-
# name: bin
37-
# path: ./bin
29+
deploy:
30+
runs-on: ubuntu-latest
31+
needs: build
32+
steps:
33+
- name: Download bin file
34+
uses: actions/download-artifact@v4
35+
with:
36+
name: bin
37+
path: ./bin
3838

39-
# - name: Transfer bin files to server
40-
# uses: appleboy/scp-action@master
41-
# with:
42-
# host: 37.139.41.110
43-
# username: ubuntu
44-
# key: ${{ secrets.SSH_PRIVATE_KEY }}
45-
# debug: true
46-
# source: "bin/*" # <- Вот изменение: копируем все файлы из bin
47-
# target: "/home/ubuntu/bin"
39+
- name: Transfer bin files to server
40+
uses: appleboy/scp-action@master
41+
with:
42+
host: 37.139.41.110
43+
username: ubuntu
44+
key: ${{ secrets.SSH_PRIVATE_KEY }}
45+
debug: true
46+
source: "bin/*" # <- Вот изменение: копируем все файлы из bin
47+
target: "/home/ubuntu/bin"
4848

49-
# # - name: Transfer migrations files to server
50-
# # uses: appleboy/scp-action@master
51-
# # with:
52-
# # host: 37.139.41.110
53-
# # username: ubuntu
54-
# # key: ${{ secrets.SSH_PRIVATE_KEY }}
55-
# # debug: true
56-
# # source: "migrations"
57-
# # target: "/home/ubuntu/bin" # <- убедитесь, что путь верный
49+
# - name: Transfer migrations files to server
50+
# uses: appleboy/scp-action@master
51+
# with:
52+
# host: 37.139.41.110
53+
# username: ubuntu
54+
# key: ${{ secrets.SSH_PRIVATE_KEY }}
55+
# debug: true
56+
# source: "migrations"
57+
# target: "/home/ubuntu/bin" # <- убедитесь, что путь верный
5858

59-
# - name: chmod all binaries
60-
# uses: appleboy/ssh-action@master
61-
# with:
62-
# host: 37.139.41.110
63-
# username: ubuntu
64-
# key: ${{ secrets.SSH_PRIVATE_KEY }}
65-
# script: | # Многострочный скрипт для chmod
66-
# cd /home/ubuntu/bin
67-
# chmod 755 *
59+
- name: chmod all binaries
60+
uses: appleboy/ssh-action@master
61+
with:
62+
host: 37.139.41.110
63+
username: ubuntu
64+
key: ${{ secrets.SSH_PRIVATE_KEY }}
65+
script: | # Многострочный скрипт для chmod
66+
cd /home/ubuntu/bin
67+
chmod 755 *
6868
69-
# - name: restart service
70-
# uses: appleboy/ssh-action@master
71-
# with:
72-
# host: 37.139.41.110
73-
# username: ubuntu
74-
# key: ${{ secrets.SSH_PRIVATE_KEY }}
75-
# script: sudo systemctl restart api.service
69+
- name: restart service
70+
uses: appleboy/ssh-action@master
71+
with:
72+
host: 37.139.41.110
73+
username: ubuntu
74+
key: ${{ secrets.SSH_PRIVATE_KEY }}
75+
script: sudo systemctl restart api.service

‎docker-compose.yml

+7-5
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,18 @@ services:
138138

139139
# Ads service
140140
ads_service:
141-
image: golang:1.23.1
142-
working_dir: /app
141+
build:
142+
context: .
143+
dockerfile: microservices/ads_service/Dockerfile
144+
volumes:
145+
- ./.env:/app/.env
146+
143147
ports:
144148
- "50052:50052"
145-
volumes:
146-
- .:/app
147-
command: go run ./microservices/ads_service/cmd/main.go
148149
depends_on:
149150
- redis
150151
- postgres
152+
- minio
151153
environment:
152154
- DB_HOST=${DB_HOST}
153155
- DB_PORT=${DB_PORT}

‎microservices/ads_service/Dockerfile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 1. Build it
2+
3+
FROM golang:1.23.1 AS builder
4+
WORKDIR /app
5+
# Копируем go.mod и go.sum
6+
COPY go.mod go.sum ./
7+
RUN go mod download
8+
9+
COPY .env ./
10+
# This microservice uses other modules so we can't just copy only it
11+
# Therefore we need to copy the whole fucking project
12+
# I have wasted 3 hours of my life on this
13+
# COPY ./microservices/ads_service/ ./microservices/ads_service/
14+
COPY . .
15+
ENV CGO_ENABLED=0
16+
ENV GOOS=linux
17+
RUN go build -o /microservices/ads_service/cmd/ads_service ./microservices/ads_service/cmd/main.go
18+
19+
20+
# 2. Run it
21+
FROM alpine:latest
22+
# WORKDIR /microservices/ads_service
23+
COPY --from=builder ./microservices/ads_service/cmd/ads_service /app/ads_service
24+
COPY --from=builder ./app/.env /.env
25+
CMD ["/app/ads_service"]

0 commit comments

Comments
 (0)
Please sign in to comment.