From 447a5e5171510528bdf20b8e5ca33c14163a2937 Mon Sep 17 00:00:00 2001 From: Daoud-Hussain Date: Mon, 18 Nov 2024 15:20:55 +0500 Subject: [PATCH] Test change triggered across all env --- .github/workflows/stagging-cicd.yml | 77 +++++++++++++++++------------ app/page.test.tsx | 2 +- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/.github/workflows/stagging-cicd.yml b/.github/workflows/stagging-cicd.yml index a45559f..708fb23 100644 --- a/.github/workflows/stagging-cicd.yml +++ b/.github/workflows/stagging-cicd.yml @@ -1,14 +1,17 @@ + name: Staging CI/CD Pipeline + on: push: branches: - - staging - + - stagging + env: IMAGE_NAME: daoudhussaindev/next-js-app NODE_VERSION: 18.x AWS_REGION: ap-south-1 - CONTAINER_NAME: next-js-app-staging + CONTAINER_NAME: next-js-app-staging + PORT: 3000 jobs: build: @@ -16,15 +19,19 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 + - name: Set up Node.js ${{ env.NODE_VERSION }} uses: actions/setup-node@v3 with: node-version: ${{ env.NODE_VERSION }} cache: 'npm' + - name: Install dependencies run: npm ci + - name: Build Next.js application run: npm run build + - name: Cache build output uses: actions/cache@v3 with: @@ -41,11 +48,13 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 + - name: Set up Node.js ${{ env.NODE_VERSION }} uses: actions/setup-node@v3 with: node-version: ${{ env.NODE_VERSION }} cache: 'npm' + - name: Restore cache uses: actions/cache@v3 with: @@ -53,8 +62,10 @@ jobs: .next node_modules key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} + - name: Install dependencies run: npm ci + - name: Run tests run: npm run test @@ -75,60 +86,64 @@ jobs: echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key chmod 600 ~/.ssh/deploy_key ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts - echo "Testing SSH connection..." - ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} 'echo "SSH connection successful"' - name: Deploy to EC2 run: | echo "Starting deployment..." - ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "\ + ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << EOF set -e echo 'Connected to EC2 instance' - + echo 'Checking Docker status...' - docker ps - - echo 'Stopping existing container...' - if docker ps -a --filter name=${{ env.CONTAINER_NAME }} | grep -q ${{ env.CONTAINER_NAME }}; then - echo 'Stopping and removing existing container...' - docker stop ${{ env.CONTAINER_NAME }} || true - docker rm ${{ env.CONTAINER_NAME }} || true + docker ps -a + + echo 'Ensuring no containers are bound to port 3000...' + CONFLICTING_CONTAINER=\$(docker ps -q --filter publish=3000) + if [ -n "\$CONFLICTING_CONTAINER" ]; then + echo 'Stopping conflicting container...' + docker stop \$CONFLICTING_CONTAINER + echo 'Removing conflicting container...' + docker rm \$CONFLICTING_CONTAINER fi - + + echo 'Stopping and removing existing container named next-js-app-staging (if exists)...' + docker stop next-js-app-staging || true + docker rm next-js-app-staging || true + echo 'Pulling new image...' - docker pull ${{ env.IMAGE_NAME }}:dev - + docker pull daoudhussaindev/next-js-app:dev + echo 'Starting new container...' docker run -d \ - --name ${{ env.CONTAINER_NAME }} \ + --name next-js-app-staging \ -p 3000:3000 \ --restart unless-stopped \ - ${{ env.IMAGE_NAME }}:dev - + daoudhussaindev/next-js-app:dev + echo 'Cleaning up old images...' docker image prune -f - + echo 'Verifying deployment...' - if docker ps | grep -q ${{ env.CONTAINER_NAME }}; then + if docker ps | grep -q next-js-app-staging; then echo 'Container is running successfully' else echo 'Container failed to start' exit 1 fi - + echo 'Deployment completed successfully' - " + EOF - name: Verify Deployment run: | - echo "Waiting for application to start..." - sleep 15 - ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "\ - if curl -s http://localhost:3000 > /dev/null; then - echo 'Application is responding' + echo "Verifying deployment..." + sleep 15 # Give the application time to start + ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << EOF + if docker ps | grep -q ${{ env.CONTAINER_NAME }}; then + echo 'Deployment verified successfully' exit 0 else - echo 'Application is not responding' + echo 'Deployment verification failed' exit 1 fi - " \ No newline at end of file + EOF diff --git a/app/page.test.tsx b/app/page.test.tsx index 481e70e..b75bad3 100644 --- a/app/page.test.tsx +++ b/app/page.test.tsx @@ -6,7 +6,7 @@ describe("App Router", () => { render(); // Check for specific heading (e.g.,

text) - expect(screen.getByRole('heading', { name: /My Next\.js Site/i })).toBeInTheDocument(); + expect(screen.getByRole('heading', { name: /Next\.js Site/i })).toBeInTheDocument(); // Alternatively, you can check for other headings or count the number of heading elements const headings = screen.getAllByRole('heading');