Skip to content

Commit

Permalink
fixed deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Dec 15, 2023
1 parent aa3ddda commit fb218ce
Showing 1 changed file with 97 additions and 32 deletions.
129 changes: 97 additions & 32 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
name: CI/CD
# name: CI/CD

# on:
# push:
# branches:
# - "*"
# pull_request:

# env:
# IMAGE_NAME: "project_badging"

# jobs:
# build:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Install dependencies
# run: npm install
# - name: Lint code
# run: npm run format

# deploy:
# needs: build
# runs-on: ubuntu-latest
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# steps:
# - uses: actions/checkout@v3
# - name: Build Docker image
# run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} .

# - name: Create image archive
# run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging.tar

# - name: Upload image archive using appleboy/scp-action
# uses: appleboy/scp-action@v0.1.4
# with:
# host: ${{ secrets.HOST }}
# username: ${{ secrets.USERNAME }}
# password: ${{ secrets.PASSWORD }}
# source: badging.tar
# target: ~/

# - name: Extract and load image on droplet
# uses: appleboy/ssh-action@v0.1.10
# with:
# host: ${{ secrets.HOST }}
# username: ${{ secrets.USERNAME }}
# password: ${{ secrets.PASSWORD }}
# script: |
# if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then
# docker stop ${{ env.IMAGE_NAME }}
# docker rm ${{ env.IMAGE_NAME }}
# fi
# docker load -i ~/badging.tar
# docker run -d \
# -p ${{ secrets.PORT }}:${{ secrets.PORT }} \
# --env-file /home/${{ secrets.USERNAME }}/.env \
# --restart=always \
# --name ${{ env.IMAGE_NAME }} \
# ${{ env.IMAGE_NAME }}:${{ github.sha }}


name: Node.js CI/CD

on:
push:
Expand All @@ -7,54 +69,57 @@ on:
pull_request:

env:
IMAGE_NAME: "project_badging"
DEPLOY_FOLDER: "project-badging-backend"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm install
- name: Lint code
run: npm run format

deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'

steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} .
- name: Checkout repository
uses: actions/checkout@v2

- name: Create image archive
run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging.tar
- name: Cleanup existing content on the server
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
if [ -d ./${{ env.DEPLOY_FOLDER }} ]; then
cd ./${{ env.DEPLOY_FOLDER }}
find . -maxdepth 1 -type f ! -name '.env' -exec rm -f {} +
else
mkdir -p ./${{ env.DEPLOY_FOLDER }}
fi
- name: Upload image archive using appleboy/scp-action
- name: Upload repository contents to the server
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: badging.tar
target: ~/
source: ./
target: ./${{ env.DEPLOY_FOLDER }}/

- name: Extract and load image on droplet
- name: Install npm dependencies on the server
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then
docker stop ${{ env.IMAGE_NAME }}
docker rm ${{ env.IMAGE_NAME }}
fi
docker load -i ~/badging.tar
docker run -d \
-p ${{ secrets.PORT }}:${{ secrets.PORT }} \
--env-file /home/${{ secrets.USERNAME }}/.env \
--restart=always \
--name ${{ env.IMAGE_NAME }} \
${{ env.IMAGE_NAME }}:${{ github.sha }}
cd ./${{ env.DEPLOY_FOLDER }}
npm install
- name: Deploy with PM2
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ./${{ env.DEPLOY_FOLDER }}
pm2 restart index.js
pm2 save --force

0 comments on commit fb218ce

Please sign in to comment.