|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - develop |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + |
| 12 | +env: |
| 13 | + AWS_REGION: ap-northeast-2 |
| 14 | + JAVA_VERSION: 21 |
| 15 | + |
| 16 | +jobs: |
| 17 | + test: |
| 18 | + name: Run Tests |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Set up JDK 21 |
| 26 | + uses: actions/setup-java@v3 |
| 27 | + with: |
| 28 | + java-version: '21' |
| 29 | + distribution: 'temurin' |
| 30 | + |
| 31 | + - name: Setup Gradle |
| 32 | + uses: gradle/gradle-build-action@v2 |
| 33 | + |
| 34 | + - name: Run tests |
| 35 | + run: ./gradlew test |
| 36 | + |
| 37 | + - name: Upload test results |
| 38 | + uses: actions/upload-artifact@v3 |
| 39 | + if: always() |
| 40 | + with: |
| 41 | + name: test-results |
| 42 | + path: build/test-results/ |
| 43 | + |
| 44 | + build: |
| 45 | + name: Build and Push Image |
| 46 | + runs-on: ubuntu-latest |
| 47 | + needs: test |
| 48 | + if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' |
| 49 | + |
| 50 | + outputs: |
| 51 | + image: ${{ steps.image.outputs.image }} |
| 52 | + tag: ${{ steps.image.outputs.tag }} |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout code |
| 56 | + uses: actions/checkout@v4 |
| 57 | + |
| 58 | + - name: Set environment |
| 59 | + id: env |
| 60 | + run: | |
| 61 | + if [[ $GITHUB_REF == 'refs/heads/main' ]]; then |
| 62 | + echo "environment=prod" >> $GITHUB_OUTPUT |
| 63 | + else |
| 64 | + echo "environment=dev" >> $GITHUB_OUTPUT |
| 65 | + fi |
| 66 | + |
| 67 | + - name: Configure AWS credentials |
| 68 | + uses: aws-actions/configure-aws-credentials@v2 |
| 69 | + with: |
| 70 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 71 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 72 | + aws-region: ${{ env.AWS_REGION }} |
| 73 | + |
| 74 | + - name: Login to Amazon ECR |
| 75 | + id: login-ecr |
| 76 | + uses: aws-actions/amazon-ecr-login@v1 |
| 77 | + |
| 78 | + - name: Build, tag, and push image to Amazon ECR |
| 79 | + id: image |
| 80 | + env: |
| 81 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 82 | + ENVIRONMENT: ${{ steps.env.outputs.environment }} |
| 83 | + run: | |
| 84 | + IMAGE_TAG=$GITHUB_SHA |
| 85 | + IMAGE_URI=$ECR_REGISTRY/team12-ecr-1:$IMAGE_TAG |
| 86 | + |
| 87 | + docker build -t $IMAGE_URI . |
| 88 | + docker push $IMAGE_URI |
| 89 | + |
| 90 | + echo "image=$IMAGE_URI" >> $GITHUB_OUTPUT |
| 91 | + echo "tag=$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 92 | + |
| 93 | + # Also tag as latest |
| 94 | + docker tag $IMAGE_URI $ECR_REGISTRY/team12-ecr-1:latest |
| 95 | + docker push $ECR_REGISTRY/team12-ecr-1:latest |
| 96 | + |
| 97 | + deploy-dev: |
| 98 | + name: Deploy to Development |
| 99 | + runs-on: ubuntu-latest |
| 100 | + needs: build |
| 101 | + if: github.ref == 'refs/heads/develop' |
| 102 | + environment: development |
| 103 | + |
| 104 | + steps: |
| 105 | + - name: Checkout code |
| 106 | + uses: actions/checkout@v4 |
| 107 | + |
| 108 | + - name: Configure AWS credentials |
| 109 | + uses: aws-actions/configure-aws-credentials@v2 |
| 110 | + with: |
| 111 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 112 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 113 | + aws-region: ${{ env.AWS_REGION }} |
| 114 | + |
| 115 | + - name: Deploy Application |
| 116 | + run: | |
| 117 | + # Trigger instance refresh to deploy new version |
| 118 | + aws autoscaling start-instance-refresh \ |
| 119 | + --auto-scaling-group-name team12-asg-1 \ |
| 120 | + --preferences '{"MinHealthyPercentage": 50}' |
| 121 | + |
| 122 | + echo "Deployment initiated for team12-asg-1" |
| 123 | + |
| 124 | + - name: Wait for deployment |
| 125 | + run: | |
| 126 | + echo "Waiting for instance refresh to complete..." |
| 127 | + aws autoscaling wait instance-refresh-complete \ |
| 128 | + --auto-scaling-group-name team12-asg-1 \ |
| 129 | + --max-attempts 60 |
| 130 | + |
| 131 | + - name: Health Check |
| 132 | + run: | |
| 133 | + echo "Running health check on https://api.bid-market.shop" |
| 134 | + for i in {1..30}; do |
| 135 | + if curl -f https://api.bid-market.shop/actuator/health; then |
| 136 | + echo "Health check passed!" |
| 137 | + exit 0 |
| 138 | + fi |
| 139 | + echo "Waiting for application... ($i/30)" |
| 140 | + sleep 10 |
| 141 | + done |
| 142 | + echo "Health check failed!" |
| 143 | + exit 1 |
| 144 | + |
| 145 | + deploy-prod: |
| 146 | + name: Deploy to Production |
| 147 | + runs-on: ubuntu-latest |
| 148 | + needs: build |
| 149 | + if: github.ref == 'refs/heads/main' |
| 150 | + environment: production |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Checkout code |
| 154 | + uses: actions/checkout@v4 |
| 155 | + |
| 156 | + - name: Configure AWS credentials |
| 157 | + uses: aws-actions/configure-aws-credentials@v2 |
| 158 | + with: |
| 159 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 160 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 161 | + aws-region: ${{ env.AWS_REGION }} |
| 162 | + |
| 163 | + - name: Setup Terraform |
| 164 | + uses: hashicorp/setup-terraform@v2 |
| 165 | + with: |
| 166 | + terraform_version: 1.5.0 |
| 167 | + |
| 168 | + - name: Terraform Init |
| 169 | + run: | |
| 170 | + cd terraform/environments/prod |
| 171 | + terraform init |
| 172 | + |
| 173 | + - name: Terraform Apply |
| 174 | + run: | |
| 175 | + cd terraform/environments/prod |
| 176 | + terraform apply -auto-approve |
| 177 | + |
| 178 | + - name: Deploy Application |
| 179 | + run: | |
| 180 | + cd terraform/environments/prod |
| 181 | + ASG_NAME=$(terraform output -raw asg_name) |
| 182 | + |
| 183 | + # Gradual rollout with 90% healthy instances maintained |
| 184 | + aws autoscaling start-instance-refresh \ |
| 185 | + --auto-scaling-group-name $ASG_NAME \ |
| 186 | + --preferences '{"MinHealthyPercentage": 90, "InstanceWarmup": 300}' |
| 187 | + |
| 188 | + echo "Production deployment initiated for $ASG_NAME" |
| 189 | + |
| 190 | + - name: Wait for deployment |
| 191 | + run: | |
| 192 | + cd terraform/environments/prod |
| 193 | + ASG_NAME=$(terraform output -raw asg_name) |
| 194 | + |
| 195 | + echo "Waiting for instance refresh to complete..." |
| 196 | + aws autoscaling wait instance-refresh-complete \ |
| 197 | + --auto-scaling-group-name $ASG_NAME \ |
| 198 | + --max-attempts 60 |
| 199 | + |
| 200 | + - name: Health Check |
| 201 | + run: | |
| 202 | + cd terraform/environments/prod |
| 203 | + ELASTIC_IP=$(terraform output -raw elastic_ip) |
| 204 | + |
| 205 | + echo "Running health check on https://$ELASTIC_IP" |
| 206 | + for i in {1..30}; do |
| 207 | + if curl -f -k https://$ELASTIC_IP/actuator/health; then |
| 208 | + echo "Production deployment successful!" |
| 209 | + exit 0 |
| 210 | + fi |
| 211 | + echo "Waiting for application... ($i/30)" |
| 212 | + sleep 10 |
| 213 | + done |
| 214 | + echo "Health check failed!" |
| 215 | + exit 1 |
0 commit comments