|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main, develop] |
| 8 | + |
| 9 | +env: |
| 10 | + NODE_VERSION: '18.x' |
| 11 | + AWS_REGION: us-east-1 |
| 12 | + |
| 13 | +jobs: |
| 14 | + lint-and-test: |
| 15 | + name: Lint and Test |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + services: |
| 19 | + mongodb: |
| 20 | + image: mongo:7.0 |
| 21 | + ports: |
| 22 | + - 27017:27017 |
| 23 | + redis: |
| 24 | + image: redis:7-alpine |
| 25 | + ports: |
| 26 | + - 6379:6379 |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Setup Node.js |
| 33 | + uses: actions/setup-node@v4 |
| 34 | + with: |
| 35 | + node-version: ${{ env.NODE_VERSION }} |
| 36 | + cache: 'npm' |
| 37 | + |
| 38 | + - name: Install dependencies |
| 39 | + run: npm ci |
| 40 | + |
| 41 | + - name: Run ESLint |
| 42 | + run: npm run lint |
| 43 | + |
| 44 | + - name: Run Prettier check |
| 45 | + run: npm run format -- --check |
| 46 | + |
| 47 | + - name: Run tests |
| 48 | + run: npm test |
| 49 | + env: |
| 50 | + NODE_ENV: test |
| 51 | + MONGODB_URL: mongodb://localhost:27017/test |
| 52 | + REDIS_HOST: localhost |
| 53 | + REDIS_PORT: 6379 |
| 54 | + JWT_SECRET: test-secret-key |
| 55 | + |
| 56 | + - name: Upload coverage reports |
| 57 | + uses: codecov/codecov-action@v3 |
| 58 | + with: |
| 59 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 60 | + files: ./coverage/lcov.info |
| 61 | + fail_ci_if_error: false |
| 62 | + |
| 63 | + build: |
| 64 | + name: Build Docker Image |
| 65 | + runs-on: ubuntu-latest |
| 66 | + needs: lint-and-test |
| 67 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 68 | + |
| 69 | + steps: |
| 70 | + - name: Checkout code |
| 71 | + uses: actions/checkout@v4 |
| 72 | + |
| 73 | + - name: Configure AWS credentials |
| 74 | + uses: aws-actions/configure-aws-credentials@v4 |
| 75 | + with: |
| 76 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 77 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 78 | + aws-region: ${{ env.AWS_REGION }} |
| 79 | + |
| 80 | + - name: Login to Amazon ECR |
| 81 | + id: login-ecr |
| 82 | + uses: aws-actions/amazon-ecr-login@v2 |
| 83 | + |
| 84 | + - name: Build, tag, and push image to Amazon ECR |
| 85 | + env: |
| 86 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 87 | + ECR_REPOSITORY: nodejs-backend-template |
| 88 | + IMAGE_TAG: ${{ github.sha }} |
| 89 | + run: | |
| 90 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 91 | + docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 92 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 93 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 94 | +
|
| 95 | + deploy: |
| 96 | + name: Deploy to AWS ECS |
| 97 | + runs-on: ubuntu-latest |
| 98 | + needs: build |
| 99 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 100 | + |
| 101 | + steps: |
| 102 | + - name: Checkout code |
| 103 | + uses: actions/checkout@v4 |
| 104 | + |
| 105 | + - name: Configure AWS credentials |
| 106 | + uses: aws-actions/configure-aws-credentials@v4 |
| 107 | + with: |
| 108 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 109 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 110 | + aws-region: ${{ env.AWS_REGION }} |
| 111 | + |
| 112 | + - name: Deploy to Amazon ECS |
| 113 | + run: | |
| 114 | + aws ecs update-service \ |
| 115 | + --cluster nodejs-backend-cluster \ |
| 116 | + --service nodejs-backend-service \ |
| 117 | + --force-new-deployment \ |
| 118 | + --region ${{ env.AWS_REGION }} |
| 119 | +
|
| 120 | + - name: Wait for service to be stable |
| 121 | + run: | |
| 122 | + aws ecs wait services-stable \ |
| 123 | + --cluster nodejs-backend-cluster \ |
| 124 | + --services nodejs-backend-service \ |
| 125 | + --region ${{ env.AWS_REGION }} |
0 commit comments