From 16e2bbdbfc36cffbf8c98d67c32006ac9bdb3a4c Mon Sep 17 00:00:00 2001 From: Kim Minju <111269144+miiiinju1@users.noreply.github.com> Date: Tue, 27 Aug 2024 09:48:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=B0=B0=ED=8F=AC=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=EC=9D=84=20jar=20=EC=A7=81=EC=A0=91=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 도커라이징하지 않고, jar를 직접 배포하는 방식으로 변경합니다. --- .github/workflows/cicd.yml | 56 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 709bf4a4..a16911ef 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -1,45 +1,38 @@ name: cicd pipeline - on: push: branches: [ "dev" ] - env: - DOCKER_IMAGE_TAG: logbat/logbat:latest - DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }} - DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }} + JAR_FILE: logbat.jar BASTION_HOST_IP: ${{ secrets.BASTION_HOST_IP }} BACKEND_HOST_IP: ${{ secrets.BACKEND_HOST_IP }} - permissions: contents: read - jobs: build-and-push: runs-on: ubuntu-latest - steps: - name: Checkout uses: actions/checkout@v3 - - - name: Set up JDK 17 + + - name: Set up Liberica JDK 17 uses: actions/setup-java@v3 with: distribution: 'corretto' java-version: '17' - + - name: Cleanup application.yml run: | cd logbat rm -f src/main/resources/application.yml - + - name: Create application-dev.yml from GitHub Secret run: | cd logbat mkdir -p src/main/resources echo "${{ secrets.APPLICATION_YML }}" > src/main/resources/application.yml - + - name: Build with Gradle env: ORG_GRADLE_OPTS: "-Duser.timezone=Asia/Seoul" @@ -47,16 +40,30 @@ jobs: cd logbat ./gradlew clean bootJar -x test - - name: Login to Docker Hub - run: echo ${{ env.DOCKER_HUB_PASSWORD }} | docker login -u ${{ env.DOCKER_HUB_USERNAME }} --password-stdin - - - name: Build Docker Image + - name: Rename JAR file to logbat.jar run: | - cd logbat - docker build -t ${{ env.DOCKER_IMAGE_TAG }} . + cd logbat/build/libs + JAR_FILE=$(ls *.jar) + if [ -z "$JAR_FILE" ]; then + echo "Error: No JAR files found!" + exit 1 + fi + mv "$JAR_FILE" logbat.jar + echo "Renamed $JAR_FILE to logbat.jar" - - name: Push Docker Image to Docker Hub - run: docker push ${{ env.DOCKER_IMAGE_TAG }} + - name: Start SSH Agent and Add Bastion Key + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.BASTION_SSH_SECRET_KEY }} + + - name: Add Target Server Key to SSH Agent + run: echo "${{ secrets.PRIVATE_SSH_SECRET_KEY }}" | ssh-add - + + - name: SCP JAR file to EC2 via Bastion + run: | + cd logbat/build/libs + scp -o StrictHostKeyChecking=no -o ProxyCommand="ssh -W %h:%p -o StrictHostKeyChecking=no ubuntu@${{ secrets.BASTION_HOST_IP }}" \ + logbat.jar ubuntu@${{ secrets.BACKEND_HOST_IP }}:/home/ubuntu/logbat.jar deploy: name: Deploy to EC2 @@ -67,12 +74,11 @@ jobs: uses: webfactory/ssh-agent@v0.9.0 with: ssh-private-key: ${{ secrets.BASTION_SSH_SECRET_KEY }} - + - name: Add Target Server Key to SSH Agent run: echo "${{ secrets.PRIVATE_SSH_SECRET_KEY }}" | ssh-add - - + - name: Connect to Bastion and Deploy to EC2 run: | ssh -o StrictHostKeyChecking=no -A ubuntu@${{ secrets.BASTION_HOST_IP }} \ - "ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.BACKEND_HOST_IP }} 'bash /home/ubuntu/deploy.sh'" - + "ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.BACKEND_HOST_IP }} 'bash -l /home/ubuntu/deploy.sh'"