|
| 1 | +name: Deploy to AWS App Runner |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: # Manual trigger |
| 5 | + push: |
| 6 | + branches: [ main ] |
| 7 | + paths-ignore: |
| 8 | + - 'README.md' |
| 9 | + - 'docs/**' |
| 10 | + |
| 11 | +env: |
| 12 | + AWS_REGION: ap-southeast-2 |
| 13 | + ECR_REPOSITORY: permit-api |
| 14 | + IMAGE_TAG: ${{ github.sha }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + deploy: |
| 18 | + name: Build and Deploy to App Runner |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Configure AWS credentials |
| 26 | + uses: aws-actions/configure-aws-credentials@v4 |
| 27 | + with: |
| 28 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 29 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 30 | + aws-region: ${{ env.AWS_REGION }} |
| 31 | + |
| 32 | + - name: Login to Amazon ECR |
| 33 | + id: login-ecr |
| 34 | + uses: aws-actions/amazon-ecr-login@v2 |
| 35 | + |
| 36 | + - name: Create ECR repository if not exists |
| 37 | + run: | |
| 38 | + aws ecr describe-repositories --repository-names $ECR_REPOSITORY --region $AWS_REGION || \ |
| 39 | + aws ecr create-repository --repository-name $ECR_REPOSITORY --region $AWS_REGION |
| 40 | +
|
| 41 | + - name: Build, tag, and push image to Amazon ECR |
| 42 | + env: |
| 43 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 44 | + run: | |
| 45 | + # Build Docker image |
| 46 | + docker build -f Dockerfile.apprunner -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 47 | + docker build -f Dockerfile.apprunner -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . |
| 48 | + |
| 49 | + # Push to ECR |
| 50 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 51 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 52 | + |
| 53 | + echo "Image URI: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + - name: Create apprunner.yaml for ECR deployment |
| 56 | + run: | |
| 57 | + cat > apprunner-ecr.yaml << EOF |
| 58 | + version: 1.0 |
| 59 | + runtime: docker |
| 60 | + build: |
| 61 | + commands: |
| 62 | + build: |
| 63 | + - echo "Using pre-built Docker image" |
| 64 | + run: |
| 65 | + runtime-version: latest |
| 66 | + command: python run_server.py |
| 67 | + network: |
| 68 | + port: 8000 |
| 69 | + env: PORT |
| 70 | + env: |
| 71 | + - name: FLASK_ENV |
| 72 | + value: "production" |
| 73 | + - name: FLASK_DEBUG |
| 74 | + value: "0" |
| 75 | + - name: PORT |
| 76 | + value: "8000" |
| 77 | + - name: API_KEYS |
| 78 | + value: "demo_basic_key:DemoBasic:basic,demo_premium_key:DemoPremium:premium" |
| 79 | + - name: MASTER_API_KEY |
| 80 | + value: "demo_master_key_12345" |
| 81 | + - name: LOG_LEVEL |
| 82 | + value: "INFO" |
| 83 | + EOF |
| 84 | +
|
| 85 | + - name: Deploy to App Runner (if service exists) |
| 86 | + env: |
| 87 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 88 | + run: | |
| 89 | + # Check if App Runner service exists |
| 90 | + SERVICE_ARN=$(aws apprunner list-services --query "ServiceSummaryList[?ServiceName=='permit-api-service'].ServiceArn | [0]" --output text --region $AWS_REGION) |
| 91 | + |
| 92 | + if [ "$SERVICE_ARN" != "None" ] && [ "$SERVICE_ARN" != "" ]; then |
| 93 | + echo "Updating existing App Runner service..." |
| 94 | + aws apprunner start-deployment --service-arn $SERVICE_ARN --region $AWS_REGION |
| 95 | + echo "Deployment started for service: $SERVICE_ARN" |
| 96 | + else |
| 97 | + echo "No existing App Runner service found. Please create service manually using:" |
| 98 | + echo "Image URI: $ECR_REGISTRY/$ECR_REPOSITORY:latest" |
| 99 | + fi |
| 100 | +
|
| 101 | + - name: Output deployment info |
| 102 | + env: |
| 103 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 104 | + run: | |
| 105 | + echo "🚀 Deployment completed!" |
| 106 | + echo "📦 Docker Image: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 107 | + echo "🔗 Use this image URI in App Runner console: $ECR_REGISTRY/$ECR_REPOSITORY:latest" |
0 commit comments