Skip to content

Commit

Permalink
Merge pull request #222 from TEAM-MONGDOL/MF-344-Next.js-CD-pipeline
Browse files Browse the repository at this point in the history
[chore] next.js CD Github Action 완성
  • Loading branch information
MinhoJJang authored Aug 14, 2024
2 parents b4cb87a + deb673c commit 364af68
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/nextjs-prod-CD.yml
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
name: Next.js CD

on:
workflow_run:
workflows: ["Node.js CI"]
types:
- completed

jobs:
deploy:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Get latest tag
id: get_latest_tag
run: |
git fetch --tags
latest_tag=$(git describe --tags --abbrev=0)
echo "::set-output name=latest_tag::$latest_tag"
- name: Deploy to Web Servers
env:
BASTION_IP: ${{ secrets.BASTION_IP }}
BASTION_USER: ${{ secrets.BASTION_USER }}
SSH_KEY: ${{ secrets.SSH_KEY }}
KCR_USERNAME: ${{ secrets.ACCESS_KEY }}
KCR_PASSWORD: ${{ secrets.ACCESS_SECRET_KEY }}
WEB_IPS: ${{ secrets.WEB_IPS }}
LATEST_TAG: ${{ steps.get_latest_tag.outputs.latest_tag }}
run: |
# Write the SSH key to a file
echo "$SSH_KEY" > ssh_key
chmod 600 ssh_key
# Connect to Bastion and run deployment on each Web server
ssh -i ssh_key -o StrictHostKeyChecking=no $BASTION_USER@$BASTION_IP << EOF
IFS=',' read -ra WEB_IP_ARRAY <<< "$WEB_IPS"
for WEB_IP in "\${WEB_IP_ARRAY[@]}"; do
ssh -i ssh_key -o StrictHostKeyChecking=no ec2-user@\$WEB_IP << ENDSSH
# Login to KCR
echo $KCR_PASSWORD | docker login dkation.kr-central-2.kcr.dev -u $KCR_USERNAME --password-stdin
# Stop and remove existing containers
docker ps -q --filter ancestor=dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe | xargs -r docker stop
docker ps -aq --filter ancestor=dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe | xargs -r docker rm
# Remove existing images
docker images dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe --format '{{.ID}}' | xargs -r docker rmi
# Pull and run new image with the latest tag
docker pull dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${LATEST_TAG}
docker run -d -p 80:3000 dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${LATEST_TAG}
ENDSSH
done
EOF
# Clean up key file
rm ssh_key

0 comments on commit 364af68

Please sign in to comment.