Skip to content

bug: 배포를 위해 임시로 CD 코드 수정 #31

bug: 배포를 위해 임시로 CD 코드 수정

bug: 배포를 위해 임시로 CD 코드 수정 #31

Workflow file for this run

name: CD (자동배포)
on:
push:
branches:
- develop
- bug/85-bug-cors
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Java 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
- name: Set up Docker
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin
- name: Set Image Tag
id: image_tag
run: echo "IMAGE_TAG=$(date +'%Y-%m-%d_%H-%M-%S')-$(echo ${{ github.sha }} | cut -c1-8)" >> $GITHUB_ENV
- name: Decode env.properties from GitHub Secrets
run: |
echo "${{ secrets.ENV_FILE }}" | base64 --decode > ./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: Build and Push Docker image
run: docker buildx build --push --platform linux/amd64 -t kimsongmok/splanet:${{ env.IMAGE_TAG }} .
- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
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
sudo docker run -d --name splanet \
--network splanet \
--env-file /home/ubuntu/env.properties \
-p 8080: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