NextJS CD #27
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 -i ssh_key -o StrictHostKeyChecking=no $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 $USER@$WEB_IP << 'ENDSSH' | |
echo $KCR_PASSWORD | docker login dkation.kr-central-2.kcr.dev -u $KCR_USERNAME --password-stdin | |
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 | |
docker images dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe --format '{{.ID}}' | xargs -r docker rmi | |
docker pull dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${DEPLOY_TAG} | |
docker run -d -p 80:3000 dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${DEPLOY_TAG} | |
ENDSSH | |
done | |
EOF | |
rm ssh_key |