From def7e48aa5cd205bbeff7acd1e08a436f85528c6 Mon Sep 17 00:00:00 2001 From: Vishwanath Martur <64204611+vishwamartur@users.noreply.github.com> Date: Mon, 2 Dec 2024 00:30:12 +0530 Subject: [PATCH 1/3] Add CI/CD pipeline for Vercel and AWS deployment Related to #140 Add CI/CD pipeline for Vercel and AWS deployment. * **Vercel Deployment:** - Modify `apps/web-mobile/vercel.json` to include `buildCommand`, `outputDirectory`, and `framework`. - Add `.github/workflows/vercel-deploy.yml` to create a GitHub Actions workflow for Vercel deployment. - Add a script `deploy:vercel` in `package.json` for Vercel deployment. * **AWS Deployment:** - Add `.github/workflows/aws-deploy.yml` to create a GitHub Actions workflow for AWS deployment. - Add a script `deploy:aws` in `package.json` for AWS deployment. --- .github/workflows/aws-deploy.yml | 37 +++++++++++++++++++++++++++++ .github/workflows/vercel-deploy.yml | 33 +++++++++++++++++++++++++ apps/web-mobile/vercel.json | 5 +++- package.json | 4 +++- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/aws-deploy.yml create mode 100644 .github/workflows/vercel-deploy.yml diff --git a/.github/workflows/aws-deploy.yml b/.github/workflows/aws-deploy.yml new file mode 100644 index 00000000..0a4ada0f --- /dev/null +++ b/.github/workflows/aws-deploy.yml @@ -0,0 +1,37 @@ +name: Deploy to AWS + +on: + push: + branches: + - main + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: Install dependencies + run: npm install + + - name: Build the project + run: npm run build:api + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Deploy to AWS + run: | + aws s3 sync ./dist/apps/api s3://${{ secrets.AWS_S3_BUCKET_NAME }} + aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" diff --git a/.github/workflows/vercel-deploy.yml b/.github/workflows/vercel-deploy.yml new file mode 100644 index 00000000..7871b818 --- /dev/null +++ b/.github/workflows/vercel-deploy.yml @@ -0,0 +1,33 @@ +name: Deploy to Vercel + +on: + push: + branches: + - main + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: Install dependencies + run: npm install + + - name: Build the project + run: npm run build:web-mobile + + - name: Deploy to Vercel + uses: amondnet/vercel-action@v20 + with: + vercel-token: ${{ secrets.VERCEL_TOKEN }} + vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} + vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} + working-directory: ./apps/web-mobile diff --git a/apps/web-mobile/vercel.json b/apps/web-mobile/vercel.json index 408821b1..b53d6c8a 100644 --- a/apps/web-mobile/vercel.json +++ b/apps/web-mobile/vercel.json @@ -4,5 +4,8 @@ "source": "/(.*)", "destination": "/" } - ] + ], + "buildCommand": "npm run build", + "outputDirectory": "dist/apps/web-mobile", + "framework": "vite" } diff --git a/package.json b/package.json index a10db6d7..01f1ea13 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "start:api": "nx serve api", "start:contracts": "nx serve contracts", "start:web-mobile": "nx serve web-mobile", - "update": "nx migrate latest" + "update": "nx migrate latest", + "deploy:vercel": "vercel --prod", + "deploy:aws": "aws s3 sync ./dist/apps/api s3://${{ secrets.AWS_S3_BUCKET_NAME }} && aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths '/*'" }, "devDependencies": { "@eslint/js": "^9.8.0", From cf84af78ef2352ca389720ecbd919bf9274c790f Mon Sep 17 00:00:00 2001 From: vishwamartur Date: Mon, 2 Dec 2024 00:50:03 +0530 Subject: [PATCH 2/3] Updated as per coderabbitai Updated as per coderabbitai --- .github/workflows/aws-deploy.yml | 23 +++++++++++++++++------ .github/workflows/vercel-deploy.yml | 11 ++++++++--- package.json | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.github/workflows/aws-deploy.yml b/.github/workflows/aws-deploy.yml index 0a4ada0f..304bd9bf 100644 --- a/.github/workflows/aws-deploy.yml +++ b/.github/workflows/aws-deploy.yml @@ -11,21 +11,26 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: - node-version: '14' + node-version: '20' + cache: 'npm' - name: Install dependencies + env: + NODE_ENV: production run: npm install - name: Build the project + env: + NODE_ENV: production run: npm run build:api - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 + uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -33,5 +38,11 @@ jobs: - name: Deploy to AWS run: | - aws s3 sync ./dist/apps/api s3://${{ secrets.AWS_S3_BUCKET_NAME }} - aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" + if ! aws s3 sync ./dist/apps/api s3://${{ secrets.AWS_S3_BUCKET_NAME }} --delete; then + echo "Failed to sync with S3" + exit 1 + fi + if ! aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*"; then + echo "Failed to invalidate CloudFront distribution" + exit 1 + fi diff --git a/.github/workflows/vercel-deploy.yml b/.github/workflows/vercel-deploy.yml index 7871b818..fa9ea8cb 100644 --- a/.github/workflows/vercel-deploy.yml +++ b/.github/workflows/vercel-deploy.yml @@ -11,17 +11,22 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: - node-version: '14' + node-version: '20' + cache: 'npm' - name: Install dependencies + env: + NODE_ENV: production run: npm install - name: Build the project + env: + NODE_ENV: production run: npm run build:web-mobile - name: Deploy to Vercel diff --git a/package.json b/package.json index 01f1ea13..b2690b38 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "start:web-mobile": "nx serve web-mobile", "update": "nx migrate latest", "deploy:vercel": "vercel --prod", - "deploy:aws": "aws s3 sync ./dist/apps/api s3://${{ secrets.AWS_S3_BUCKET_NAME }} && aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths '/*'" + "deploy:aws": "aws s3 sync ./dist/apps/api s3://${AWS_S3_BUCKET_NAME} && aws cloudfront create-invalidation --distribution-id ${AWS_CLOUDFRONT_DISTRIBUTION_ID} --paths '/*'" }, "devDependencies": { "@eslint/js": "^9.8.0", From 3a0cee1fc9e48faa380f68204c50b7add4bfd727 Mon Sep 17 00:00:00 2001 From: vishwamartur Date: Mon, 2 Dec 2024 00:54:36 +0530 Subject: [PATCH 3/3] Update vercel-deploy.yml --- .github/workflows/vercel-deploy.yml | 47 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/.github/workflows/vercel-deploy.yml b/.github/workflows/vercel-deploy.yml index fa9ea8cb..33c496e8 100644 --- a/.github/workflows/vercel-deploy.yml +++ b/.github/workflows/vercel-deploy.yml @@ -22,17 +22,60 @@ jobs: - name: Install dependencies env: NODE_ENV: production - run: npm install + run: | + npm clean-install + npm audit + + - name: Cache build + uses: actions/cache@v3 + with: + path: | + dist + .next + key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }} - name: Build the project env: NODE_ENV: production - run: npm run build:web-mobile + run: | + npm run build:web-mobile || { + echo "Build failed" + exit 1 + } - name: Deploy to Vercel + id: deploy uses: amondnet/vercel-action@v20 with: vercel-token: ${{ secrets.VERCEL_TOKEN }} vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} working-directory: ./apps/web-mobile + alias-domains: | + staging.your-domain.com + pr-{{PR_NUMBER}}.your-domain.com + + - name: Verify deployment + run: | + DEPLOY_URL="${{ steps.deploy.outputs.preview-url }}" + MAX_RETRIES=5 + COUNT=0 + until curl -s -o /dev/null -w "%{http_code}" "$DEPLOY_URL" | grep -q "200\|404"; do + if [ $COUNT -eq $MAX_RETRIES ]; then + echo "Deployment verification failed after $MAX_RETRIES attempts" + exit 1 + fi + COUNT=$((COUNT+1)) + sleep 10 + done + + - name: Comment PR + uses: actions/github-script@v6 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `✅ Deployed to: ${{ steps.deploy.outputs.preview-url }}` + })