updated #70
This file contains 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 API and Monitor Health | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the code from the repository | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
# Step 2: Set up Docker | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
# Step 3: Install Docker Compose | |
- name: Install Docker Compose | |
run: | | |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
docker-compose --version | |
# Step 4: Build and push Docker image | |
- name: Build and push Docker image | |
run: | | |
docker-compose -f docker-compose.yml build api | |
docker-compose -f docker-compose.yml up -d | |
# Step 5: Check container health | |
- name: Check API container health | |
run: | | |
container_health=$(docker inspect --format '{{.State.Health.Status}}' myapp_api) | |
if [ "$container_health" != "healthy" ]; then | |
echo "API container is unhealthy, sending Slack notification..." | |
curl -X POST -H 'Content-type: application/json' \ | |
--data '{"text":"❌ API container failed health check! Check logs."}' \ | |
https://hooks.slack.com/services/your/slack/webhook | |
exit 1 # Fail the pipeline | |
else | |
echo "API container is healthy." | |
fi | |
# Step 6: Run tests or post-deployment tasks (optional) | |
- name: Run tests | |
run: | | |
# Add your testing commands here | |
echo "Tests go here." | |
# Step 7: Clean up | |
- name: Tear down Docker containers | |
run: | | |
docker-compose -f docker-compose.yml down | |
# Step 8: Slack notification on success | |
- name: Notify Slack on success | |
if: success() | |
uses: slackapi/[email protected] | |
with: | |
method: chat.postMessage | |
token: ${{ secrets.SLACK_BOT_TOKEN }} | |
payload: | | |
channel: ${{ secrets.SLACK_CHANNEL_ID }} | |
text: "✅ Deployment succeeded! API container is healthy." | |
attachments: | |
- color: "00FF00" | |
fields: | |
- title: "Status" | |
short: true | |
value: "Success" | |
- title: "API Health" | |
short: true | |
value: "Healthy" | |
# Step 9: Slack notification on failure (already covered in Step 5) |