-
Notifications
You must be signed in to change notification settings - Fork 114
123 lines (99 loc) · 3.35 KB
/
docker-image.yml
File metadata and controls
123 lines (99 loc) · 3.35 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
name: Build and Deploy
on:
push:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SERVER_IP: ${{ secrets.SERVER_IP }}
DEPLOY_PATH: ~/studentbar-pos-deploy
ENV_PRODUCTION_FILE: ${{ secrets.ENV_PRODUCTION_FILE }}
DOCKER_COMPOSE_FILE: docker-compose.prod.yaml
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
- name: Build backend
working-directory: backend
run: ./mvnw clean package -DskipTests
- name: Set up Node 20
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install frontend dependencies
working-directory: frontend
run: npm ci
- name: Build frontend
working-directory: frontend
run: npm run build
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan "$SERVER_IP" >> ~/.ssh/known_hosts
- name: Create .env.production from secret
run: |
printf '%s\n' "$ENV_PRODUCTION_FILE" > .env.production
- name: Create deployment package
run: |
tar czf deployment.tar.gz \
backend/target/*.jar \
frontend/ \
nginx/ \
"$DOCKER_COMPOSE_FILE" \
backend/Dockerfile \
frontend/Dockerfile \
.env.production
- name: Install sshpass and openssh-client
run: |
sudo apt-get update
sudo apt-get install -y sshpass openssh-client
- name: Copy deployment package to server
run: |
scp -i ~/.ssh/id_rsa \
-o StrictHostKeyChecking=yes \
deployment.tar.gz \
"$SSH_USER@$SERVER_IP:/tmp/"
- name: Deploy on server
run: |
ssh -i ~/.ssh/id_rsa \
-o StrictHostKeyChecking=yes \
"$SSH_USER@$SERVER_IP" << EOF
set -e
mkdir -p $DEPLOY_PATH
cd $DEPLOY_PATH
# Extract deployment package
tar xzf /tmp/deployment.tar.gz
rm /tmp/deployment.tar.gz
# Check if docker-compose file exists
if [ ! -f "$DOCKER_COMPOSE_FILE" ]; then
echo "ERROR: $DOCKER_COMPOSE_FILE not found!"
exit 1
fi
# Copy environment file if exists
if [ -f .env.production ]; then
cp .env.production .env
fi
# Clean/repair builder cache and avoid BuildKit flakiness
# sudo systemctl restart docker
# docker buildx prune -af || true
# docker builder prune -af || true
# docker image prune -af || true
# Stop existing containers
docker compose -f "$DOCKER_COMPOSE_FILE" down
# Rebuild and start containers
docker compose -f "$DOCKER_COMPOSE_FILE" build --no-cache
docker compose -f "$DOCKER_COMPOSE_FILE" up -d
# Clean up old images
# docker image prune -f
echo "Deployment complete!"
docker compose -f "$DOCKER_COMPOSE_FILE" ps
EOF