Skip to content

Commit 10fed6e

Browse files
committed
Add GitHub Actions deployment workflow for ECR-based App Runner deployment
1 parent 39856e8 commit 10fed6e

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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"

QUICK_DEPLOY.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# AWS App Runner Deployment via GitHub Actions
2+
3+
## Quick Solution for GitHub Connection Issues
4+
5+
Since AWS App Runner GitHub connection is experiencing loading issues, we can use **GitHub Actions + ECR deployment** as an alternative.
6+
7+
### Step 1: Setup AWS Credentials in GitHub
8+
9+
1. **Get AWS Access Keys:**
10+
```bash
11+
# In AWS Console → IAM → Users → Your User → Security Credentials
12+
# Create Access Key for CLI/SDK usage
13+
```
14+
15+
2. **Add GitHub Secrets:**
16+
- Go to: `https://github.com/hk-dev13/project-permit-api/settings/secrets/actions`
17+
- Add these secrets:
18+
- `AWS_ACCESS_KEY_ID`: Your AWS Access Key
19+
- `AWS_SECRET_ACCESS_KEY`: Your AWS Secret Key
20+
21+
### Step 2: Run GitHub Action
22+
23+
1. **Go to GitHub Actions:**
24+
- URL: `https://github.com/hk-dev13/project-permit-api/actions`
25+
- Click on "Deploy to AWS App Runner" workflow
26+
- Click "Run workflow" → "Run workflow"
27+
28+
2. **The action will:**
29+
- Build Docker image using `Dockerfile.apprunner`
30+
- Push to Amazon ECR
31+
- Provide image URI for App Runner
32+
33+
### Step 3: Create App Runner Service with ECR
34+
35+
After GitHub Action completes:
36+
37+
1. **In AWS App Runner Console:**
38+
- Create service
39+
- Source: **"Container registry"**
40+
- Provider: **Amazon ECR**
41+
- Container image URI: *(from GitHub Action output)*
42+
- Port: `8000`
43+
44+
2. **Service Configuration:**
45+
```yaml
46+
Service name: permit-api-service
47+
CPU: 1 vCPU
48+
Memory: 2 GB
49+
Port: 8000
50+
Environment variables:
51+
FLASK_ENV: production
52+
FLASK_DEBUG: 0
53+
PORT: 8000
54+
```
55+
56+
### Alternative: Manual ECR Push (if GitHub Actions fails)
57+
58+
If you prefer manual deployment:
59+
60+
```powershell
61+
# Install AWS CLI first if not installed
62+
choco install awscli -y
63+
64+
# Configure AWS
65+
aws configure
66+
67+
# Get account ID
68+
$AWS_ACCOUNT_ID = aws sts get-caller-identity --query Account --output text
69+
70+
# Login to ECR (after Docker is installed)
71+
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.ap-southeast-2.amazonaws.com"
72+
73+
# Build and push (after Docker is ready)
74+
docker build -f Dockerfile.apprunner -t permit-api .
75+
docker tag permit-api "$AWS_ACCOUNT_ID.dkr.ecr.ap-southeast-2.amazonaws.com/permit-api:latest"
76+
docker push "$AWS_ACCOUNT_ID.dkr.ecr.ap-southeast-2.amazonaws.com/permit-api:latest"
77+
```
78+
79+
## Next Steps
80+
81+
1. **Setup GitHub Secrets** (AWS credentials)
82+
2. **Run GitHub Action** to build and push Docker image
83+
3. **Create App Runner service** using ECR image URI
84+
4. **Test deployment** once service is running
85+
86+
This approach bypasses the GitHub connection loading issue completely!

0 commit comments

Comments
 (0)