From 5dbabf289b07ee1278f372162c753c503da50be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?toni=20=28=EC=9D=B4=EC=86=8C=EC=9D=80=29?= <144209738+saokiritoni@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:43:37 +0900 Subject: [PATCH] =?UTF-8?q?Chore:=20=EC=9B=8C=ED=81=AC=ED=94=8C=EB=A1=9C?= =?UTF-8?q?=EC=9A=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 86 +++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ec80764..340a164 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,16 +1,30 @@ -name: Deploy to EC2 with Docker Compose +name: akoming CI/CD pipeline on: push: - branches: ["main"] + branches: [ "main" ] + pull_request: + branches: [ "main" ] jobs: - deploy: + build: runs-on: ubuntu-latest + permissions: + contents: read + + env: + DB_URL: ${{ secrets.DB_URL }} + DB_USERNAME: ${{ secrets.DB_USERNAME }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + REDIS_HOST: ${{ secrets.REDIS_HOST }} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + SMTP_HOST: ${{ secrets.SMTP_HOST }} + SMTP_PORT: ${{ secrets.SMTP_PORT }} + SMTP_USERNAME: ${{ secrets.SMTP_USERNAME }} + SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }} steps: - - name: Check out the code - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v4 @@ -18,36 +32,50 @@ jobs: java-version: '17' distribution: 'temurin' - - name: Build with Gradle - run: ./gradlew build - - - name: Log in to Docker Hub - run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + # Gradle 캐시 설정 + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: ~/.gradle/caches + key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + gradle-${{ runner.os }}- - - name: Build Docker image - run: docker build -t ${{ secrets.DOCKER_USERNAME }}/akoming-test:latest . + - name: Build with Gradle Wrapper + run: ./gradlew build - - name: Push Docker image to Docker Hub - run: docker push ${{ secrets.DOCKER_USERNAME }}/akoming-test:latest + # Docker 빌드 및 Docker Hub에 푸시 + - name: Docker build & push to Docker Hub + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t ${{ secrets.DOCKER_USERNAME }}/docker-test:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/docker-test:latest - # docker-compose.yml 파일을 EC2로 복사 - - name: Copy docker-compose.yml to EC2 - uses: appleboy/scp-action@master - with: - host: ${{ secrets.EC2_HOST }} - username: ubuntu - key: ${{ secrets.EC2_PRIVATE_KEY }} - source: "./docker-compose.yml" # 로컬의 docker-compose.yml 파일 - target: "/home/ubuntu/docker-compose.yml" # EC2 내의 정확한 파일 경로 - - - name: Deploy on EC2 with Docker Compose + # EC2에 배포 + - name: Docker image pull & deploy to EC2 uses: appleboy/ssh-action@master with: host: ${{ secrets.EC2_HOST }} username: ubuntu key: ${{ secrets.EC2_PRIVATE_KEY }} script: | - cd /home/ubuntu - sudo docker-compose down # 기존 컨테이너 중지 - sudo docker-compose pull # 최신 이미지 가져오기 - sudo docker-compose up -d # 새로운 컨테이너 시작 + # 최신 이미지 Pull + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/docker-test:latest + + # 기존 컨테이너 중지 및 삭제 + if [ "$(sudo docker ps -q -f name=docker-test)" ]; then + sudo docker stop docker-test + sudo docker rm docker-test + fi + + # 새 컨테이너 실행 및 환경 변수 전달 + sudo docker run -d --log-driver=syslog --name docker-test -p 8080:8080 \ + --env-file ~/.env \ + ${{ secrets.DOCKER_USERNAME }}/docker-test:latest + + # 실행 중인 컨테이너 확인 + sudo docker ps -a | grep "docker-test" + + # 사용하지 않는 이미지 및 컨테이너 정리 + sudo docker image prune -a -f +