-
Notifications
You must be signed in to change notification settings - Fork 0
52 lines (44 loc) · 1.42 KB
/
deploy.yml
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
name: Deploy Dockerized Service
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: ./app
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.sha }}
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TARGET_HOST }}
username: ${{ secrets.TARGET_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
# Pull the latest image
docker pull ghcr.io/${{ github.repository }}:latest
# Stop and remove existing container if it exists
docker stop dockerized-service || true
docker rm dockerized-service || true
# Run the new container
docker run -d \
--name dockerized-service \
--restart always \
-p 3000:3000 \
--env-file /opt/dockerized-service/.env \
ghcr.io/${{ github.repository }}:latest