Skip to content

News Intelligent CD - Deploy to AWS EC2 #96

News Intelligent CD - Deploy to AWS EC2

News Intelligent CD - Deploy to AWS EC2 #96

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
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 pull
docker-compose -f docker-compose-prod.yml up -d --force-recreate --remove-orphans
docker image prune -f