Skip to content

Commit 5b28c0e

Browse files
working on deploy.yml
1 parent 44b1e0f commit 5b28c0e

1 file changed

Lines changed: 53 additions & 26 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/setup-node@v4
2525
with:
2626
node-version: ${{ env.NODE_VERSION }}
27-
cache: 'yarn'
27+
cache: "yarn"
2828

2929
- name: Install dependencies
3030
run: yarn install --frozen-lockfile
@@ -35,12 +35,43 @@ jobs:
3535
- name: Check types
3636
run: yarn check-types
3737

38-
deploy:
38+
build-and-push:
3939
runs-on: ubuntu-latest
4040
needs: ci
4141
environment: production
4242

4343
steps:
44+
- name: Checkout code
45+
uses: actions/checkout@v4
46+
with:
47+
ref: release
48+
49+
- name: Login to Docker Hub
50+
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
51+
52+
- name: Build and push Docker image
53+
run: |
54+
docker build \
55+
--build-arg VITE_APP_API_URL=${{ secrets.VITE_APP_API_URL }} \
56+
--build-arg VITE_APP_APP_URL=${{ secrets.VITE_APP_APP_URL }} \
57+
-t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.APP_NAME }}:latest \
58+
.
59+
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.APP_NAME }}:latest
60+
61+
- name: Logout from Docker Hub
62+
run: docker logout
63+
64+
deploy:
65+
runs-on: ubuntu-latest
66+
needs: build-and-push
67+
environment: production
68+
69+
steps:
70+
- name: Checkout code
71+
uses: actions/checkout@v4
72+
with:
73+
ref: release
74+
4475
- name: Add VPS to known hosts
4576
run: |
4677
mkdir -p ~/.ssh
@@ -52,47 +83,43 @@ jobs:
5283
SSH_PORT: ${{ secrets.SSH_PORT }}
5384
VPS_HOST: ${{ secrets.VPS_HOST }}
5485
APP_NAME: ${{ secrets.APP_NAME }}
55-
REPO_PATH: ${{ secrets.REPO_PATH }}
56-
VITE_APP_API_URL: ${{ secrets.VITE_APP_API_URL }}
57-
VITE_APP_APP_URL: ${{ secrets.VITE_APP_APP_URL }}
86+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
5887
NGINX_SITE_NAME: ${{ secrets.NGINX_SITE_NAME }}
5988
run: |
6089
mkdir -p ~/.ssh
6190
echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy_key
6291
chmod 600 ~/.ssh/deploy_key
6392
93+
# Copy nginx-host.conf to VPS for comparison
94+
scp -P $SSH_PORT -i ~/.ssh/deploy_key nginx/nginx-host.conf deploy@$VPS_HOST:/tmp/nginx-host.conf.new
95+
6496
ssh -p $SSH_PORT -i ~/.ssh/deploy_key deploy@$VPS_HOST << EOF
6597
set -e
6698
67-
# Navigate to repo directory
68-
cd $REPO_PATH
69-
70-
# Checkout and pull release branch
71-
git checkout release
72-
git pull origin release --rebase
73-
74-
# Build the project with Docker
75-
docker build \
76-
--build-arg VITE_APP_API_URL=$VITE_APP_API_URL \
77-
--build-arg VITE_APP_APP_URL=$VITE_APP_APP_URL \
78-
-o dist .
99+
# Pull the latest Docker image
100+
docker pull $DOCKERHUB_USERNAME/$APP_NAME:latest
79101
80-
# Move entire dist directory to web location for clean deployment
81-
sudo rm -rf /var/www/$APP_NAME/dist
82-
sudo mv dist /var/www/$APP_NAME/
102+
# Stop and remove old container if exists
103+
docker stop $APP_NAME 2>/dev/null || true
104+
docker rm $APP_NAME 2>/dev/null || true
83105
84-
# Make the file globally readable
85-
sudo chmod -R a+rX /var/www/$APP_NAME/dist
106+
# Run new container
107+
docker run -d \
108+
--name $APP_NAME \
109+
--restart unless-stopped \
110+
-p 127.0.0.1:3000:80 \
111+
$DOCKERHUB_USERNAME/$APP_NAME:latest
86112
87-
# Check if nginx config has changed and update if needed
88-
if ! cmp -s nginx/nginx.conf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf; then
89-
sudo cp nginx/nginx.conf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf
113+
# Check if nginx-host.conf has changed and update if needed
114+
if ! cmp -s /tmp/nginx-host.conf.new /etc/nginx/sites-available/$NGINX_SITE_NAME.conf; then
115+
sudo cp /tmp/nginx-host.conf.new /etc/nginx/sites-available/$NGINX_SITE_NAME.conf
90116
sudo ln -sf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf /etc/nginx/sites-enabled/$NGINX_SITE_NAME.conf
91-
# Reload nginx
117+
# Test and reload nginx
92118
sudo nginx -t && sudo nginx -s reload
93119
fi
94120
95121
# Cleanup
122+
rm -f /tmp/nginx-host.conf.new
96123
docker system prune -f
97124
EOF
98125

0 commit comments

Comments
 (0)