Skip to content

feat: #dev-deploy backup compose #36

feat: #dev-deploy backup compose

feat: #dev-deploy backup compose #36

name: DEV - KodemyFrontend deploy
on:
push:
branches: [ 'dev-deploy' ]
paths-ignore:
- '.gitignore'
- 'README.md'
env:
WORKING_DIRECTORY: ${{ vars.WORKING_DIRECTORY }}
NETWORK_NAME: ${{ vars.NETWORK_NAME }}
EXPOSE_PORT: ${{ vars.EXPOSE_PORT }}
NODE_ENV: ${{ vars.NODE_ENV }}
NEXT_PUBLIC_API_URL: ${{ vars.NEXT_PUBLIC_API_URL }}
ERROR_HOLD_TIME_IN_MS: ${{ vars.ERROR_HOLD_TIME_IN_MS }}
NVM_DIR: '/root/.nvm'
jobs:
prepare:
name: Prepare
runs-on: self-hosted
environment: dev
steps:
- name: Check environments
run: |
REQUIRED_VARS=\
(
"WORKING_DIRECTORY"
"NETWORK_NAME"
"EXPOSE_PORT"
"NODE_ENV"
"NEXT_PUBLIC_API_URL"
"ERROR_HOLD_TIME_IN_MS"
)
EXIT=0
for VAR in "${REQUIRED_VARS[@]}"; do
[ -z "${!VAR}" ] && echo "Environment variable $VAR is missing." && EXIT=1
done
exit $EXIT
update-branch:
name: Update branch
needs: [ prepare ]
runs-on: self-hosted
environment: dev
steps:
- name: Update ${{ github.ref_name }}
uses: appleboy/ssh-action@master
env:
BRANCH: ${{ github.ref_name }}
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: WORKING_DIRECTORY,BRANCH
script: |
cd $WORKING_DIRECTORY
git fetch origin || exit 1
git checkout $BRANCH || exit 1
git reset --hard origin/$BRANCH || exit 1
git pull origin $BRANCH || exit 1
validate:
name: Validate components
runs-on: self-hosted
needs: [ update-branch ]
environment: dev
steps:
- name: Validate components
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: NVM_DIR
script: |
source $NVM_DIR/nvm.sh
echo "Validate npm"
which npm || { echo "npm not found in PATH"; exit 1; }
build-and-deploy:
name: Build, Test, Deploy
runs-on: self-hosted
needs: [ validate ]
environment: dev
steps:
- name: Build, Test, Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY, NVM_DIR,
NETWORK_NAME, EXPOSE_PORT,
NODE_ENV,
NEXT_PUBLIC_API_URL, ERROR_HOLD_TIME_IN_MS
script: |
set -e
source $NVM_DIR/nvm.sh
cd $WORKING_DIRECTORY
npm install
echo "Run Linter"
npm run lint
echo "Build"
npm run build
docker compose up --build -d
- name: Cleanup
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker system prune -f
create-tag:
name: Create tag
runs-on: self-hosted
environment: dev
needs: [ build-and-deploy ]
steps:
- name: Create tag
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY
script: |
cd $WORKING_DIRECTORY
git tag -l | xargs git tag -d
git fetch --tags
tags=$(git tag -l)
latest_tag=$(echo "$tags" | grep -E '^v?0\.[0-9]+\.[0-9]+(-.*)?$' | sort -V | tail -n 1)
if [ -n "$latest_tag" ]; then
if [[ "$latest_tag" == v* ]]; then
latest_tag="${latest_tag#v}"
fi
base_version=$(echo "$latest_tag" | sed 's/-.*//')
major=$(echo "$base_version" | cut -d'.' -f1)
minor=$(echo "$base_version" | cut -d'.' -f2)
patch=$(echo "$base_version" | cut -d'.' -f3)
new_patch=$((patch + 1))
new_tag="${major}.${minor}.${new_patch}-dev"
else
new_tag="0.0.1"
fi
echo "New tag: $new_tag"
git tag $new_tag
git push origin --tags