Skip to content

Commit

Permalink
refactor: 환경변수 수정 및 이미지 정리 로직 추가
Browse files Browse the repository at this point in the history
환경변수를 용이하게 관리할 수 있도록 수정하고, 오래된 이미지를 삭제하는 스크립트를 추가로 작성한다.
  • Loading branch information
kanguk01 committed Oct 24, 2024
1 parent ca8a879 commit 6468af6
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ jobs:
id: image_tag
run: echo "IMAGE_TAG=$(date +'%Y-%m-%d_%H-%M-%S')-$(echo ${{ github.sha }} | cut -c1-8)" >> $GITHUB_ENV

- name: Build and Push Docker image
run: docker buildx build --push --platform linux/amd64 -t kimsongmok/splanet:${{ env.IMAGE_TAG }} .
- name: Decode env.properties from GitHub Secrets
run: |
echo "${{ secrets.ENV_FILE }}" | base64 --decode > ./src/main/resources/env.properties
- name: Transfer env.properties to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
source: "./env.properties"
target: "/home/ubuntu/"


- name: Deploy to EC2
uses: appleboy/[email protected]
Expand All @@ -43,15 +54,29 @@ jobs:
sudo docker pull kimsongmok/splanet:${{ env.IMAGE_TAG }}
sudo docker stop splanet || true
sudo docker rm splanet || true
sudo docker network inspect splanet >/dev/null 2>&1 || sudo docker network create splanet
scp ./src/main/resources/env.properties ubuntu@${{ secrets.EC2_HOST }}:/home/ubuntu/env.properties
sudo docker run -d --name splanet \
--network splanet \
-e MYSQL_PROD_URL=${{ secrets.MYSQL_PROD_URL }} \
-e MYSQL_PROD_USER=${{ secrets.MYSQL_PROD_USER }} \
-e MYSQL_PROD_PASSWORD=${{ secrets.MYSQL_PROD_PASSWORD }} \
-e MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \
-e CLIENT_ID=${{ secrets.CLIENT_ID }} \
-e CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e REDIRECT_URL=${{ secrets.REDIRECT_URL }} \
-p 80:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }}\
-v ./src/main/resources/env.properties
--network splanet \
--env-file /home/ubuntu/env.properties \
-p 80:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }}
- name: Check Docker container status
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker ps -a
sudo docker logs splanet
- name: Clean up old Docker images
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print $1}' | xargs docker rmi -f
sudo docker system prune -f

0 comments on commit 6468af6

Please sign in to comment.