NextJS CD #31
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: NextJS CD | |
on: | |
release: | |
types: [created] | |
workflow_dispatch: | |
repository_dispatch: | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
- name: Get release tag | |
id: get_tag | |
run: | | |
if [[ ${{ github.event_name }} == 'release' ]]; then | |
echo "tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT | |
else | |
git fetch --tags | |
latest_tag=$(git describe --tags --abbrev=0) | |
echo "tag=$latest_tag" >> $GITHUB_OUTPUT | |
fi | |
- name: Deploy to Web Servers | |
env: | |
BASTION_IP: ${{ secrets.BASTION_IP }} | |
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 }} | |
DEPLOY_TAG: ${{ steps.get_tag.outputs.tag }} | |
run: | | |
echo "$SSH_KEY" > ssh_key | |
chmod 600 ssh_key | |
# 디버깅: SSH 접속 테스트 | |
echo "Testing SSH connection to Bastion host" | |
ssh -i ssh_key -o StrictHostKeyChecking=no $USER@$BASTION_IP "echo 'SSH to Bastion host successful. User: $(whoami)'" | |
# SSH 세션에서의 작업 | |
ssh -i ssh_key -o StrictHostKeyChecking=no $USER@$BASTION_IP << 'EOF' | |
set -x # 디버깅 모드 활성화 | |
echo "Environment variables:" | |
echo "KCR_USERNAME: $KCR_USERNAME" | |
echo "KCR_PASSWORD: $KCR_PASSWORD" | |
echo "DEPLOY_TAG: $DEPLOY_TAG" | |
echo "WEB_IPS: $WEB_IPS" | |
IFS=',' read -ra WEB_IP_ARRAY <<< "$WEB_IPS" | |
for WEB_IP in "${WEB_IP_ARRAY[@]}"; do | |
echo "Testing SSH connection to web server $WEB_IP" | |
ssh -i ssh_key -o StrictHostKeyChecking=no $USER@$WEB_IP << 'ENDSSH' | |
set -x # 디버깅 모드 활성화 | |
echo "Docker version:" | |
docker version | |
echo "Logging into Docker registry" | |
echo $KCR_PASSWORD | docker login dkation.kr-central-2.kcr.dev -u $KCR_USERNAME --password-stdin || { echo 'Docker login failed'; exit 1; } | |
echo "Stopping and removing 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 | |
echo "Removing old Docker images" | |
docker images dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe --format '{{.ID}}' | xargs -r docker rmi | |
echo "Pulling new Docker image" | |
docker pull dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${DEPLOY_TAG} || { echo 'Docker pull failed'; exit 1; } | |
echo "Running new Docker container" | |
docker run -d -p 80:3000 dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${DEPLOY_TAG} || { echo 'Docker run failed'; exit 1; } | |
ENDSSH | |
done | |
EOF | |
rm ssh_key |