-
Notifications
You must be signed in to change notification settings - Fork 0
78 lines (64 loc) · 2.23 KB
/
deploy.yaml
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
name: Deploy to Production Server
env:
SERVER_USER: 'swissh'
SERVER_IP: 'swis.sh'
DEPLOY_PATH: '/home/swissh/swis.sh/'
DOCKER_IMAGE: 'swissh-app'
CONTAINER_PORT: '8091'
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build Docker image
run: |
DOCKER_BUILDKIT=1 docker build \
--progress=plain \
--no-cache \
-t ${{ env.DOCKER_IMAGE }} .
- name: Set up SSH agent
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Add SSH Key to Known Hosts
run: |
mkdir -p ~/.ssh
echo "$(ssh-keyscan -t rsa ${{ env.SERVER_IP }} 2>/dev/null)" >> ~/.ssh/known_hosts
- name: Save Docker image
run: docker save ${{ env.DOCKER_IMAGE }} | gzip > image.tar.gz
- name: Copy Docker image to server
run: |
scp image.tar.gz ${{ env.SERVER_USER }}@${{ env.SERVER_IP }}:${{ env.DEPLOY_PATH }}/
- name: Deploy using Docker
run: |
ssh ${{ env.SERVER_USER }}@${{ env.SERVER_IP }} << 'EOF'
set -e
cd ${{ env.DEPLOY_PATH }}
# Load the new image
docker load < image.tar.gz
# Stop and remove the old container
docker stop ${{ env.DOCKER_IMAGE }} || true
docker rm ${{ env.DOCKER_IMAGE }} || true
# Run the new container with port 8091 mapped to container's port 80
docker run -d \
--name ${{ env.DOCKER_IMAGE }} \
-p 8091:80 \
--restart unless-stopped \
${{ env.DOCKER_IMAGE }}
# Verify the container is running
if ! docker ps | grep -q ${{ env.DOCKER_IMAGE }}; then
echo "Container failed to start"
docker logs ${{ env.DOCKER_IMAGE }}
exit 1
fi
# Clean up
rm image.tar.gz
docker image prune -f
EOF