-
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 #234 from TEAM-MONGDOL/MF-361-Next.js-CD-Github-Ac…
…tion [chore] next.js cd GitHub action
- Loading branch information
Showing
2 changed files
with
60 additions
and
27 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,10 +1,13 @@ | ||
name: NextJS CD | ||
|
||
on: | ||
release: | ||
types: [created] | ||
workflow_dispatch: | ||
repository_dispatch: | ||
|
||
env: | ||
KCR_REGISTRY: dkation.kr-central-2.kcr.dev | ||
KCR_REPOSITORY: dkation-prod-front/dkation-prod-fe | ||
ACCESS_KEY: ${{ secrets.ACCESS_KEY }} | ||
ACCESS_SECRET_KEY: ${{ secrets.ACCESS_SECRET_KEY }} | ||
|
||
jobs: | ||
deploy: | ||
|
@@ -15,50 +18,79 @@ jobs: | |
with: | ||
fetch-depth: 0 | ||
|
||
- name: Get release tag | ||
id: get_tag | ||
- name: Get release tag or commit hash | ||
id: get_version | ||
run: | | ||
if [[ ${{ github.event_name }} == 'release' ]]; then | ||
echo "tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT | ||
echo "version=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT | ||
else | ||
git fetch --tags | ||
latest_tag=$(git describe --tags --abbrev=0) | ||
echo "tag=$latest_tag" >> $GITHUB_OUTPUT | ||
echo "version=$latest_tag" >> $GITHUB_OUTPUT | ||
fi | ||
- name: Setup SSH | ||
uses: webfactory/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.SSH_KEY }} | ||
|
||
- name: Deploy to FE | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.BASTION_HOST }} | ||
host: ${{ secrets.BASTION_IP }} | ||
username: ${{ secrets.USER }} | ||
key: ${{ secrets.SSH_KEY }} | ||
script: | | ||
mkdir -p ~/.ssh | ||
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa | ||
chmod 600 ~/.ssh/id_rsa | ||
IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" | ||
KCR_REGISTRY="${{ env.KCR_REGISTRY }}" | ||
KCR_REPOSITORY="${{ env.KCR_REPOSITORY }}" | ||
VERSION="${{ steps.get_version.outputs.version }}" | ||
AK="${{ env.ACCESS_KEY }}" | ||
SK="${{ env.ACCESS_SECRET_KEY }}" | ||
for host in "${HOSTS[@]}" | ||
do | ||
echo "$host" | ||
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@$host << EOF | ||
echo "Deploying to $host" | ||
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF | ||
set -e | ||
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 | ||
docker ps --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | xargs -r docker stop | ||
docker ps -a --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | 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 "Login to KCR" | ||
docker login dkation.kr-central-2.kcr.dev --username ${{ secrets.ACCESS_KEY }} --password ${{ secrets.ACCESS_SECRET_KEY }} | ||
docker images --format '{{.Repository}}:{{.Tag}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | xargs -r docker rmi | ||
echo "Docker login" | ||
echo "$SK" | docker login $KCR_REGISTRY -u "$AK" --password-stdin | ||
echo "Pulling new Docker image" | ||
docker pull dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${{ steps.get_tag.outputs.tag }} | ||
docker pull $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION | ||
echo "Running new Docker container" | ||
docker run -d -p 80:3000 dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe:${{ steps.get_tag.outputs.tag }} | ||
EOF | ||
docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION | ||
echo "Checking container health" | ||
max_retries=5 | ||
retries=0 | ||
until docker ps | grep dkation-frontend | grep -q "Up" || [ \$retries -eq \$max_retries ] | ||
do | ||
echo "Waiting for container to be healthy..." | ||
sleep 5 | ||
retries=\$((retries+1)) | ||
done | ||
if [ \$retries -eq \$max_retries ]; then | ||
echo "Container failed to start properly" | ||
exit 1 | ||
fi | ||
echo "Container is up and running" | ||
EOF | ||
if [ $? -ne 0 ]; then | ||
echo "Deployment to $host failed" | ||
exit 1 | ||
fi | ||
echo "Deployment completed successfully" | ||
done |
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