Skip to content

News Intelligent CD - Deploy to AWS EC2 #126

News Intelligent CD - Deploy to AWS EC2

News Intelligent CD - Deploy to AWS EC2 #126

Workflow file for this run

name: News Intelligent CD - Deploy to AWS EC2
on:
workflow_run:
# CI 성공 시 수행
workflows:
- "News Intelligent CI"
types:
- completed
jobs:
deploy:
if: ${{github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-22.04
steps:
- name: Checkout repository at CI commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 1
- name: List workspace & check compose file
run: |
echo "HEAD SHA: ${{ github.event.workflow_run.head_sha }}"
pwd
ls -al
test -f docker-compose-prod.yml || (echo "compose file not found" && exit 1)
- name: Prepare remote dir
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
mkdir -p ~/${{ secrets.REPOSITORY_PATH }}
- name: Upload compose file
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "docker-compose-prod.yml"
target: "~/${{ secrets.REPOSITORY_PATH }}/"
- name: Deploy to EC2
uses: appleboy/[email protected]
env:
IMAGE_TAG: ${{ github.event.workflow_run.head_sha }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
envs: IMAGE_TAG,DOCKERHUB_USERNAME,DOCKERHUB_REPOSITORY
command_timeout: 30m
script: |
set -eo pipefail
cd ~/${{ secrets.REPOSITORY_PATH }}
export IMAGE_TAG="$IMAGE_TAG"
export DOCKERHUB_USERNAME="$DOCKERHUB_USERNAME"
export DOCKERHUB_REPOSITORY="$DOCKERHUB_REPOSITORY"
docker-compose -f docker-compose-prod.yml down --remove-orphans || true
docker-compose -f docker-compose-prod.yml pull
docker-compose -f docker-compose-prod.yml up -d --force-recreate --remove-orphans
echo "Health-check: http://127.0.0.1:8081/actuator/health/readiness"
for i in $(seq 1 30); do
if curl -fsS "http://127.0.0.1:8081/actuator/health/readiness" | grep -q '"status":"UP"'; then
echo "App is healthy "; break
fi
echo "waiting health... ($i)"
sleep 2
done
if ! curl -fsS "http://127.0.0.1:8081/actuator/health/readiness" | grep -q '"status":"UP"'; then
echo "App failed to become healthy "
docker-compose -f docker-compose-prod.yml ps || true
docker-compose -f docker-compose-prod.yml logs --tail=200 || true
exit 1
fi
docker image prune -f