Enhance AWS App Runner deployment workflow: update environment input … #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to AWS App Runner | |
| on: | |
| workflow_dispatch: # Enable manual trigger | |
| inputs: | |
| environment: | |
| description: 'Deployment environment' | |
| required: true | |
| default: 'Production' | |
| type: choice | |
| options: | |
| - Production | |
| - staging | |
| push: | |
| branches: [ main ] | |
| paths-ignore: | |
| - 'README.md' | |
| - 'docs/**' | |
| - '*.md' | |
| env: | |
| AWS_REGION: ap-southeast-2 | |
| ECR_REPOSITORY: permit-api | |
| APP_RUNNER_SERVICE_NAME: permit-api-service # Nama service dijadikan variabel | |
| IMAGE_TAG: ${{ github.sha }} | |
| jobs: | |
| deploy: | |
| name: Build and Deploy to App Runner | |
| runs-on: ubuntu-latest | |
| environment: ${{ github.event.inputs.environment || 'Production' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS credentials | |
| 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 }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| - name: Create ECR repository if not exists | |
| run: | | |
| aws ecr describe-repositories --repository-names $ECR_REPOSITORY --region $AWS_REGION || \ | |
| aws ecr create-repository --repository-name $ECR_REPOSITORY --region $AWS_REGION | |
| - name: Build, tag, and push image to Amazon ECR | |
| id: build-image | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| run: | | |
| docker build -f Dockerfile.apprunner -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker build -f Dockerfile.apprunner -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest | |
| echo "image_uri=$ECR_REGISTRY/$ECR_REPOSITORY:latest" >> $GITHUB_OUTPUT | |
| - name: Deploy to App Runner and wait | |
| run: | | |
| SERVICE_ARN=$(aws apprunner list-services --query "ServiceSummaryList[?ServiceName=='${{ env.APP_RUNNER_SERVICE_NAME }}'].ServiceArn | [0]" --output text) | |
| if [ -n "$SERVICE_ARN" ] && [ "$SERVICE_ARN" != "None" ]; then | |
| echo "Updating existing App Runner service: $SERVICE_ARN" | |
| aws apprunner start-deployment --service-arn $SERVICE_ARN | |
| # PERBAIKAN: Menggunakan perintah 'wait' yang benar dan menambahkan pengecekan status akhir | |
| echo "Waiting for service to become stable..." | |
| # Menunggu hingga layanan kembali ke status RUNNING, dengan timeout 10 menit | |
| aws apprunner wait service-status-running --service-arn $SERVICE_ARN | |
| echo "Deployment finished. Checking final operation status..." | |
| # Mengambil status operasi deployment terakhir | |
| LATEST_OP_STATUS=$(aws apprunner list-operations --service-arn $SERVICE_ARN --max-results 1 --query "OperationSummaryList[0].Status" --output text) | |
| if [ "$LATEST_OP_STATUS" == "SUCCEEDED" ]; then | |
| echo "✅ Deployment Succeeded!" | |
| else | |
| echo "❌ Deployment resulted in status: $LATEST_OP_STATUS" | |
| # Membuat job gagal jika deployment tidak berhasil | |
| exit 1 | |
| fi | |
| else | |
| echo "Service '${{ env.APP_RUNNER_SERVICE_NAME }}' not found. Please create it manually." | |
| echo "Use Image URI: ${{ steps.build-image.outputs.image_uri }}" | |
| exit 1 # Membuat job gagal jika service tidak ditemukan | |
| fi |