[chore] : CI/CD 구축 (#155) #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD Backend | |
on: | |
push: | |
branches: [ "main", "dev" ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
# 자바 버전 설정 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# yml 파일 생성 | |
- name: Generate application-core.yml | |
run: | | |
mkdir -p ./core/src/main/resources | |
echo "${{ secrets.APPLICATION_CORE }}" | base64 -d > ./core/src/main/resources/application-core.yml | |
# firebase json 파일 생성 | |
- name : Generate firebase_account.json | |
run: | | |
mkdir -p ./core/src/main/resources/firebase | |
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/main/resources/firebase/firebase_account.json | |
mkdir -p ./core/src/test/resources/firebase | |
echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/test/resources/firebase/firebase_account.json | |
# gradle 권한 부여 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
shell: bash | |
# 빌드 시 캐시 적용 | |
- name: Gradle Caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
# 빌드 | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
# 도커 허브 로그인 | |
- name: Docker Hub Login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# 도커 이미지 빌드 및 푸시 | |
- name: docker image build and push | |
run: | | |
docker build -f Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} . | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} | |
# 서버 백그라운드 실행 | |
- name: pull image and run container | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
password: ${{ secrets.EC2_PASSWORD }} | |
port: ${{ secrets.EC2_PORT }} | |
script: | | |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} | |
docker stop ${{ secrets.DOCKERHUB_APP_NAME }} | |
docker rm ${{ secrets.DOCKERHUB_APP_NAME }} | |
docker run -d \ | |
-p 8080:8080 \ | |
-e TZ=Asia/Seoul \ | |
--name ${{ secrets.DOCKERHUB_APP_NAME }} \ | |
${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} | |
docker system prune -f |