Refactor AWS App Runner deployment workflow: update environment input… #17
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 | |
| # PERBAIKAN: Mengubah default agar cocok dengan nama environment Anda | |
| default: 'Production' | |
| type: choice | |
| options: | |
| # PERBAIKAN: Mengubah opsi agar cocok | |
| - 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 | |
| # PERBAIKAN: Mengubah default menjadi 'Production' (P besar) agar cocok dengan environment Anda | |
| 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 | |
| # Menambahkan loop untuk menunggu deployment selesai | |
| echo "Waiting for deployment to complete..." | |
| timeout 10m aws apprunner wait service-stable --service-arn $SERVICE_ARN | |
| echo "Deployment finished successfully!" | |
| 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 # Opsional: buat job gagal jika service tidak ditemukan | |
| fi |