Skip to content

Merge pull request #4 from nuuxcode/dev #48

Merge pull request #4 from nuuxcode/dev

Merge pull request #4 from nuuxcode/dev #48

Workflow file for this run

name: Deploy DEV
env:
JWT_SIGNATURE: ${{ vars.JWT_SIGNATURE }}
CORS_ALLOW_URL: ${{ vars.CORS_ALLOW_URL }}
API_PORT: ${{ vars.API_PORT }}
on:
push:
branches:
- dev
- main
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: 'https://registry.npmjs.org'
- name: check triggering branch
run: echo ${{ github.ref }}
- name: Set Database URL
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "DATABASE_URL=${{ vars.PROD_PDATABASE_URL }}" >> $GITHUB_ENV
echo "SERVER_HOST=${{ vars.PROD_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_USERNAME=${{ vars.PROD_SERVER_USERNAME }}" >> $GITHUB_ENV
elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
echo "DATABASE_URL=${{ vars.DEV_PDATABASE_URL }}" >> $GITHUB_ENV
echo "SERVER_HOST=${{ vars.DEV_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_USERNAME=${{ vars.DEV_SERVER_USERNAME }}" >> $GITHUB_ENV
fi
- name: Check the Environment Variable
run: |
echo "DATABASE_URL is $DATABASE_URL"
- name: pwd
run: pwd
- name: ls
run: ls -la
- name: check which branch
run: git branch
- name: yarn install
run: yarn install
- name: Install wait-port
run: npm install -g wait-port
- name: start api
run: |
cd api
yarn build
yarn start:prod > api.log 2>&1 &
- name: Wait for API to start
run: wait-port -t 60000 localhost:3300
continue-on-error: true
- name: Test API health with curl
if: ${{ success() }}
run: curl -si http://localhost:3300/api/v1/hello
- name: Display API logs failure
if: ${{ failure() }}
run: |
cat api/api.log
exit 1
- name: Display API logs
run: cat api/api.log
- name: Install serve
run: npm install -g serve
- name: start frontend
run: |
cd frontend
yarn build
serve -l 3000 -s dist > front.log 2>&1 &
- name: Wait for front to start
run: wait-port -t 60000 localhost:3000
continue-on-error: true
- name: Test front health with curl
if: ${{ success() }}
run: curl -si http://localhost:3000/
- name: Display Front logs failure
if: ${{ failure() }}
run: |
cat frontend/front.log
exit 1
- name: Display Front logs
run: cat frontend/front.log
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ env.SERVER_HOST }}
username: ${{ env.SERVER_USERNAME }}
key: ${{ vars.SSH_PRIVATE_KEY }}
port: ${{ vars.SSH_PORT }}
script: |
echo "--------"
echo "### CMD: cd BikeHub"
cd BikeHub
echo "Done!"
echo "--------"
echo "### CMD: git reset --hard"
git reset --hard
echo "Done!"
echo "--------"
echo "### CMD: find . -name 'package-lock.json' -exec rm -f {} +"
find . -name 'package-lock.json' -exec rm -f {} +
echo "### CMD: find . -name 'yarn.lock' -exec rm -f {} +"
find . -name 'yarn.lock' -exec rm -f {} +
echo "Done!"
echo "--------"
branch_name=$(basename "${{ github.ref }}")
echo "### CMD: git switch $branch_name"
git switch "$branch_name"
echo "Done!"
echo "--------"
echo "### CMD: git pull origin $branch_name"
git pull origin $branch_name
echo "Done!"
echo "--------"
echo "### CMD: npm install &deploy.txt& yarn install"
npm install && yarn install
echo "Done!"
echo "--------"
echo "### CMD: cd ~/BikeHub/api && cp .env.example .env"
cd ~/BikeHub/api && cp .env.example .env
echo "Done!"
echo "--------"
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "start deploying in ${{ github.ref }}"
echo "### CMD: cd ~/BikeHub/api && yarn build"
cd ~/BikeHub/api && yarn build
echo "### CMD: pm2 reload startapi --update-env"
pm2 reload startapi --update-env
echo "### CMD: cd ~/BikeHub/frontend && yarn build"
cd ~/BikeHub/frontend && yarn build
echo "### CMD: pm2 reload startfrontend --update-env"
pm2 reload startfrontend --update-env
echo "### CMD: pm2 reload prisma --update-env"
pm2 reload prisma --update-env
echo "### CMD: update deploy.txt"
echo "---------------------------" >> deploy.txt
echo "Date: $(date)" >> deploy.txt
echo "Branch: ${{ github.ref }}" >> deploy.txt
echo "Last Commit Message: ${{ github.event.head_commit.message }}" >> deploy.txt
echo "Last Commit Author: ${{ github.event.head_commit.author.name }}" >> deploy.txt
echo "Pusher: ${{ github.actor }}" >> deploy.txt
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "Pull Request Author: ${{ github.event.pull_request.user.login }}" >> deploy.txt
echo "Merged By: ${{ github.event.pull_request.merged_by.login }}" >> deploy.txt
fi
echo "### CMD: cp deploy.txt dist/deploy.txt"
cp deploy.txt dist/deploy.txt
echo "Done!"
fi
if [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "start deploying in ${{ github.ref }}"
echo "### CMD: cd ~/BikeHub/api && yarn build"
cd ~/BikeHub/api && yarn build
echo "### CMD: pm2 reload startapi --update-env"
pm2 reload startapi --update-env
echo "### CMD: cd ~/BikeHub/frontend && yarn build"
cd ~/BikeHub/frontend && yarn build
echo "### CMD: pm2 reload startfrontend --update-env"
pm2 reload startfrontend --update-env
echo "### CMD: pm2 reload prisma --update-env"
pm2 reload prisma --update-env
echo "### CMD: update deploy.txt"
echo "---------------------------" >> deploy.txt
echo "Date: $(date)" >> deploy.txt
echo "Branch: ${{ github.ref }}" >> deploy.txt
echo "Last Commit Message: ${{ github.event.head_commit.message }}" >> deploy.txt
echo "Last Commit Author: ${{ github.event.head_commit.author.name }}" >> deploy.txt
echo "Pusher: ${{ github.actor }}" >> deploy.txt
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "Pull Request Author: ${{ github.event.pull_request.user.login }}" >> deploy.txt
echo "Merged By: ${{ github.event.pull_request.merged_by.login }}" >> deploy.txt
fi
echo "### CMD: cp deploy.txt dist/deploy.txt"
cp deploy.txt dist/deploy.txt
echo "Done!"
fi
echo "--------"