-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from TEAM-MONGDOL/MF-344-Next.js-CD-pipeline
[chore] next.js CD Github Action 완성
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
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 |