From 7eb9e09cfa46f9288432b354ccc76d19c0047bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B0=80=EC=98=81?= Date: Sun, 28 Jul 2024 19:18:13 +0900 Subject: [PATCH] chore: main.yml --- .github/workflows/main.yml | 50 +++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 66afc2a..4dd3d11 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: CI/CD using GitHub Actions & Docker +name: CI/CD using github actions & docker on: push: @@ -9,56 +9,62 @@ jobs: 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: make application.yml + run: | + cd ./src/main/resources + touch ./application.yml + echo "${{ secrets.APPLICATION }}" > ./application.yml + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: set chmod + run: chmod +x ./gradlew + - name: Build with Gradle run: ./gradlew bootJar - # Gradle 빌드 후 JAR 파일 생성 확인 - # 필요에 따라 `ls -l build/libs` 등을 추가하여 파일 존재 확인 - - name: Build Docker image and push + - name: Docker build 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 + docker build -t alimo . # Dockerfile 경로 수정 + docker tag alimo ${{ secrets.DOCKER_USERNAME }}/alimo:latest + docker push ${{ secrets.DOCKER_USERNAME }}/alimo:latest - - name: Deploy to Production + - name: Deploy with SSH + if: github.ref == 'refs/heads/main' uses: appleboy/ssh-action@master - id: deploy-prod + with: - host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS + host: ${{ secrets.HOST }} # EC2 인스턴스 퍼블릭 DNS username: ubuntu - key: ${{ secrets.PRIVATE_KEY }} # EC2 private key + key: ${{ secrets.PRIVATE_KEY }} # pem 키 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 파일이 있는 디렉토리로 이동 + sudo service reids-server start + 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 \ No newline at end of file + + docker system prune -f