Skip to content

Commit a07a6d5

Browse files
committed
Enhance AWS App Runner deployment workflow: add testing and caching steps, improve deployment status checks, and implement Slack notifications for deployment status.
1 parent 422166d commit a07a6d5

1 file changed

Lines changed: 68 additions & 21 deletions

File tree

.github/workflows/deploy-apprunner.yml

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,47 @@ on:
2121
env:
2222
AWS_REGION: ap-southeast-2
2323
ECR_REPOSITORY: permit-api
24-
APP_RUNNER_SERVICE_NAME: permit-api-service # Nama service dijadikan variabel
24+
APP_RUNNER_SERVICE_NAME: permit-api-service
2525
IMAGE_TAG: ${{ github.sha }}
26+
PYTHON_VERSION: '3.11' # Menentukan versi Python
2627

2728
jobs:
28-
deploy:
29-
name: Build and Deploy to App Runner
29+
build-test-deploy: # Mengubah nama job agar lebih deskriptif
30+
name: Build, Test, and Deploy to App Runner
3031
runs-on: ubuntu-latest
3132
environment: ${{ github.event.inputs.environment || 'Production' }}
3233

3334
steps:
3435
- name: Checkout code
3536
uses: actions/checkout@v4
3637

38+
# --- SARAN 1 & 2: Testing & Caching ---
39+
- name: Set up Python ${{ env.PYTHON_VERSION }}
40+
uses: actions/setup-python@v4
41+
with:
42+
python-version: ${{ env.PYTHON_VERSION }}
43+
44+
- name: Cache pip dependencies
45+
# Menggunakan cache agar instalasi dependency lebih cepat di run berikutnya
46+
uses: actions/cache@v3
47+
with:
48+
path: ~/.cache/pip
49+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
50+
restore-keys: |
51+
${{ runner.os }}-pip-
52+
53+
- name: Install Python dependencies
54+
run: |
55+
python -m pip install --upgrade pip
56+
# Pastikan Anda memiliki file requirements.txt di repository Anda
57+
pip install -r requirements.txt
58+
pip install pytest # Install pytest untuk testing
59+
60+
- name: Run tests with pytest
61+
# Menjalankan tes. Jika ada tes yang gagal, workflow akan berhenti di sini.
62+
run: pytest
63+
64+
# --- Langkah Deployment yang Sudah Ada ---
3765
- name: Configure AWS credentials
3866
uses: aws-actions/configure-aws-credentials@v4
3967
with:
@@ -70,25 +98,44 @@ jobs:
7098
if [ -n "$SERVICE_ARN" ] && [ "$SERVICE_ARN" != "None" ]; then
7199
echo "Updating existing App Runner service: $SERVICE_ARN"
72100
aws apprunner start-deployment --service-arn $SERVICE_ARN
73-
74-
# PERBAIKAN: Menggunakan perintah 'wait' yang benar dan menambahkan pengecekan status akhir
75-
echo "Waiting for service to become stable..."
76-
# Menunggu hingga layanan kembali ke status RUNNING, dengan timeout 10 menit
77-
aws apprunner wait service-status-running --service-arn $SERVICE_ARN
78-
79-
echo "Deployment finished. Checking final operation status..."
80-
# Mengambil status operasi deployment terakhir
81-
LATEST_OP_STATUS=$(aws apprunner list-operations --service-arn $SERVICE_ARN --max-results 1 --query "OperationSummaryList[0].Status" --output text)
82-
83-
if [ "$LATEST_OP_STATUS" == "SUCCEEDED" ]; then
84-
echo "✅ Deployment Succeeded!"
85-
else
86-
echo "❌ Deployment resulted in status: $LATEST_OP_STATUS"
87-
# Membuat job gagal jika deployment tidak berhasil
88-
exit 1
89-
fi
101+
102+
echo "Waiting for deployment to complete..."
103+
while true; do
104+
STATUS=$(aws apprunner list-operations --service-arn $SERVICE_ARN --max-results 1 --query "OperationSummaryList[0].Status" --output text)
105+
if [ "$STATUS" == "SUCCEEDED" ]; then
106+
echo "✅ Deployment Succeeded!"
107+
break
108+
elif [ "$STATUS" == "FAILED" ]; then
109+
echo "❌ Deployment Failed!"
110+
exit 1
111+
elif [ "$STATUS" == "IN_PROGRESS" ]; then
112+
echo "Deployment is in progress... waiting 30 seconds."
113+
sleep 30
114+
else
115+
echo "Current operation status: $STATUS. Waiting..."
116+
sleep 30
117+
fi
118+
done
90119
else
91120
echo "Service '${{ env.APP_RUNNER_SERVICE_NAME }}' not found. Please create it manually."
92121
echo "Use Image URI: ${{ steps.build-image.outputs.image_uri }}"
93-
exit 1 # Membuat job gagal jika service tidak ditemukan
122+
exit 1
94123
fi
124+
125+
# --- SARAN 3: Notifikasi ---
126+
notify:
127+
name: Send Notification
128+
runs-on: ubuntu-latest
129+
if: always() # Selalu berjalan, baik job sebelumnya sukses maupun gagal
130+
needs: [build-test-deploy] # Bergantung pada job sebelumnya
131+
steps:
132+
- name: Send Slack Notification
133+
uses: rtCamp/action-slack-notify@v2
134+
env:
135+
# Anda perlu menambahkan secret ini di Settings > Secrets and variables > Actions
136+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
137+
SLACK_TITLE: "Deployment Status: ${{ needs.build-test-deploy.result }}"
138+
SLACK_MESSAGE: "Deployment for `${{ github.repository }}` on branch `${{ github.ref_name }}` has finished."
139+
# Warna notifikasi akan hijau jika sukses, merah jika gagal
140+
SLACK_COLOR: ${{ needs.build-test-deploy.result == 'success' && 'good' || 'danger' }}
141+
SLACK_FOOTER: "GitHub Actions Workflow"

0 commit comments

Comments
 (0)