Merge pull request #35 from Sopo2023/feat/#1 #52
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: CI/CD using GitHub Actions & Docker | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Build with Gradle | |
run: ./gradlew bootJar | |
# Gradle 빌드 후 JAR 파일 생성 확인 | |
# 필요에 따라 `ls -l build/libs` 등을 추가하여 파일 존재 확인 | |
- name: Build Docker image and push | |
run: | | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/sopo_v2 . | |
docker push ${{ secrets.DOCKER_USERNAME }}/sopo_v2 | |
- name: Deploy to Production | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
with: | |
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS | |
username: ubuntu | |
key: ${{ secrets.PRIVATE_KEY }} # EC2 private key | |
port: ${{ secrets.PORT }} # ssh port (22) | |
envs: GITHUB_SHA | |
script: | | |
# EC2에서 기존 Docker 컨테이너 및 이미지 정리 | |
docker stop sopo_v2 || true | |
docker rm sopo_v2 || true | |
docker rmi ${{ secrets.DOCKER_USERNAME }}/sopo_v2 || true | |
docker pull ${{ secrets.DOCKER_USERNAME }}/sopo_v2 | |
docker run -d -p 8080:8080 --name sopo_v2 ${{ secrets.DOCKER_USERNAME }}/sopo_v2 | |
# Redis 서버 시작 | |
sudo chkconfig --add redis-server | |
sudo chkconfig --level 345 redis-server on | |
sudo service redis-server start | |
# Docker Compose 파일이 있는 디렉토리로 이동 | |
mkdir -p /home/ubuntu/app | |
cd /home/ubuntu/app | |
# docker-compose.yml 파일을 생성하거나 덮어쓰기 | |
echo "${{ secrets.DOCKER_COMPOSE }}" > docker-compose.yml | |
# Docker Compose 명령어 실행 | |
docker-compose pull | |
docker-compose up -d | |
# 사용하지 않는 Docker 데이터 정리 (선택 사항) | |
docker system prune -f |