Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 배포 방식을 jar 직접 배포로 변경 #126

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
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"
run: |
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/[email protected]
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
Expand All @@ -67,12 +74,11 @@ jobs:
uses: webfactory/[email protected]
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'"
Loading