From 90bc30ba15e2605e2123bc0b168bed47793c8a4b Mon Sep 17 00:00:00 2001 From: BaeJunho Date: Sat, 30 Nov 2024 12:12:00 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EB=82=B4=EB=B6=80=20=EB=94=94?= =?UTF-8?q?=EB=A0=89=ED=86=A0=EB=A6=AC=20=EC=9D=B8=EC=8B=9D=EC=9D=84=20?= =?UTF-8?q?=EB=AA=BB=ED=95=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 62 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 189adf5..e3f7cca 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,7 +3,10 @@ name: Deploy on Merge on: push: branches: - - main # main 브랜치에 push될 때 실행 + - main + pull_request: + branches: + - main permissions: contents: read @@ -13,39 +16,44 @@ jobs: runs-on: ubuntu-latest steps: - # 1. 저장소 클론 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - + # 지정한 저장소(현재 REPO)에서 코드를 워크플로우 환경으로 가져오도록 하는 github action - name: Checkout repository uses: actions/checkout@v3 - # 2. Docker 로그인 (Docker Hub 또는 Private Registry) - - name: Log in to Docker Hub - uses: docker/login-action@v2 + # open jdk 17 버전 환경을 세팅 + - name: Set up JDK 17 + uses: actions/setup-java@v3 with: - username: ${{ secrets.DOCKER_USERNAME }} # GitHub Secrets에 설정 - password: ${{ secrets.DOCKER_PASSWORD }} # GitHub Secrets에 설정 + java-version: '17' + distribution: 'adopt' - # 3. Docker 이미지 빌드 및 푸시 - - name: Build and push Docker image + # gradle을 통해 소스를 빌드. + - name: Build with Gradle run: | - docker build -t baejunh0/myapp:latest . - docker push baejunh0/myapp:latest + chmod +x ./gradlew + ./gradlew clean build -x test - # 4. 서버에 배포 스크립트 실행 (SSH 사용) - - name: Deploy to Server + # dockerfile을 통해 이미지를 빌드하고, 이를 docker repo로 push 합니다. + # 이 때 사용되는 ${{ secrets.DOCKER_REPO }}/directors-dev 가 위에서 만든 도커 repository 입니다. + - name: Docker build & push to docker repo + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}/directors-dev . + docker push ${{ secrets.DOCKER_REPO }}/directors-dev + + # appleboy/ssh-action@master 액션을 사용하여 지정한 서버에 ssh로 접속하고, script를 실행합니다. + # script의 내용은 도커의 기존 프로세스들을 제거하고, docker repo로부터 방금 위에서 push한 내용을 pull 받아 실행하는 것입니다. + # 실행 시, docker-compose를 사용합니다. + - name: Deploy to server uses: appleboy/ssh-action@master + id: deploy with: - host: ${{ secrets.SERVER_HOST }} # 서버 IP - username: ${{ secrets.SERVER_USER }} # SSH 사용자 - key: ${{ secrets.SERVER_SSH_KEY }} # SSH 개인 키 (GitHub Secrets에 설정) - port: 22 + host: ${{ secrets.SERVER_HOST }} + username: ubuntu + key: ${{ secrets.SERVER_SSH_KEY }} + envs: GITHUB_SHA script: | - docker pull baejunh0/myapp:latest - docker stop myapp || true - docker rm myapp || true - docker run -d --name myapp -p 80:80 baejunh0/myapp:latest \ No newline at end of file + sudo docker rm -f $(docker ps -qa) + sudo docker pull ${{ secrets.DOCKER_REPO }}/directors-dev + docker-compose up -d + docker image prune -f \ No newline at end of file