Merge pull request #22 from mahaputrailhamawal/cicd-pipeline #21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Staging 🚀 | |
on: | |
push: | |
branches: ["main"] | |
jobs: | |
deploy-staging: | |
name: Deploy to staging server | |
runs-on: ubuntu-latest | |
steps: | |
- name: Execute deployment command | |
uses: appleboy/[email protected] | |
env: | |
APP_PATH_STAGING: ${{ vars.APP_PATH_STAGING }} | |
GIT_URL: ${{ vars.GIT_URL }} | |
POSTGRES_USER: ${{ secrets.DB_USER_STAGING }} | |
POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD_STAGING }} | |
POSTGRES_DB: ${{ vars.DB_DBNAME_STAGING }} | |
REACT_APP_API_URL: ${{ vars.REACT_APP_API_URL_STAGING }} | |
with: | |
host: ${{ secrets.SSH_HOST_STAGING }} | |
username: ${{ secrets.SSH_USER_NAME_STAGING }} | |
key: ${{ secrets.SSH_PRIVATE_KEY_STAGING }} | |
envs: APP_PATH_STAGING, GIT_URL, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, REACT_APP_API_URL | |
script: | | |
if [[ -d "/home/ubuntu/${APP_PATH_STAGING}" ]]; then | |
cd /home/ubuntu/$APP_PATH_STAGING | |
sudo docker compose down | |
git stash | |
git pull --rebase | |
else | |
ssh-keyscan github.com > ~/.ssh/known_hosts | |
git clone $GIT_URL /home/ubuntu/$APP_PATH_STAGING | |
cd /home/ubuntu/$APP_PATH_STAGING | |
fi | |
# If there are any envars update | |
echo "POSTGRES_USER=$POSTGRES_USER" > .env | |
echo "POSTGRES_PASSWORD=$POSTGRES_PASSWORD" >> .env | |
echo "POSTGRES_DB=$POSTGRES_DB" >> .env && cp .env backend/.env | |
echo "REACT_APP_API_URL=${{ vars.REACT_APP_API_URL_STAGING }}" > frontend/.env | |
# Run app | |
sudo docker compose up my-database my-backend my-frontend --build --detach | |
- name: Hit Endpoint | |
run: | | |
sleep 15 | |
curl ${{ vars.STAGING_URL }} | |
- name: Clear Docker Image Cache | |
run: | | |
sudo docker image prune |