diff --git a/.github/workflows/cd-production.yml b/.github/workflows/cd-production.yml new file mode 100644 index 0000000..7e63353 --- /dev/null +++ b/.github/workflows/cd-production.yml @@ -0,0 +1,47 @@ +name: Deploy Kaapi to EC2 Production + +on: + push: + tags: + - "v*" # Deploy only when tags like v1.0.0, v2.1.0, etc., are created + +jobs: + deploy: + name: Deploy Kaapi Frontend to EC2 Production + runs-on: ubuntu-latest + environment: AWS_PRODUCTION_ENV + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Deploy via SSH + uses: appleboy/ssh-action@v1.0.3 + env: + PM2_APP_NAME: ${{ secrets.PM2_APP_NAME }} + BUILD_DIRECTORY: ${{ secrets.BUILD_DIRECTORY }} + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.EC2_SSH_KEY }} + script_stop: true + script: | + set -e + + echo "=== Navigating to Project Directory ===" + cd $BUILD_DIRECTORY + + echo "=== Fetch Latest Code ===" + git pull origin release + + echo "=== Stop Running Application ===" + pm2 stop "$PM2_APP_NAME" || true + + echo "=== Install Dependencies ===" + npm ci + + echo "=== Build Application ===" + npm run build + + echo "=== Start Application ===" + pm2 start "$PM2_APP_NAME" diff --git a/.github/workflows/cd-staging.yml b/.github/workflows/cd-staging.yml new file mode 100644 index 0000000..8dc9b9d --- /dev/null +++ b/.github/workflows/cd-staging.yml @@ -0,0 +1,47 @@ +name: Deploy Kaapi to EC2 Staging + +on: + push: + branches: + - feat/frontend-cicd-deployment + +jobs: + deploy: + name: Deploy Kaapi Frontend to EC2 Staging + runs-on: ubuntu-latest + environment: AWS_STAGING_ENV + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Deploy via SSH + uses: appleboy/ssh-action@v1.0.3 + env: + PM2_APP_NAME: ${{ secrets.PM2_APP_NAME }} + BUILD_DIRECTORY: ${{ secrets.BUILD_DIRECTORY }} + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.EC2_SSH_KEY }} + script_stop: true + script: | + set -e + + echo "=== Navigating to Project Directory ===" + cd $BUILD_DIRECTORY + + echo "=== Fetch Latest Code ===" + git pull origin main + + echo "=== Stop Running Application ===" + pm2 stop "$PM2_APP_NAME" || true + + echo "=== Install Dependencies ===" + npm ci + + echo "=== Build Application ===" + npm run build + + echo "=== Start Application ===" + pm2 start "$PM2_APP_NAME" diff --git a/README.md b/README.md index e4b76fc..9876298 100644 --- a/README.md +++ b/README.md @@ -106,20 +106,27 @@ Deployments are automated via a GitHub Actions CD pipeline that SSHes into the E ### Branch Strategy -| Branch | Environment | -| --------- | ----------- | -| `main` | Staging | -| `release` | Production | +| Trigger | Environment | +| ------------------------------------- | ----------- | +| Push to `main` | Staging | +| Tag matching `v*.*.*` (e.g. `v1.0.0`) | Production | ### Pipeline Steps -On every push to `main` or `release`, the pipeline automatically: +**Staging** — on every push to `main`, the pipeline automatically: 1. SSHes into the EC2 instance 2. Runs `git pull` to fetch the latest code 3. Runs `npm run build` to create an optimized production build 4. Restarts the server to apply the new build +**Production** — on every version tag (e.g. `v1.0.0`, `v2.1.0`), the pipeline automatically: + +1. SSHes into the EC2 instance +2. Runs `git fetch --tags` and checks out the tag +3. Runs `npm run build` to create an optimized production build +4. Restarts the server to apply the new build + --- ## Learn More