Skip to content

Merge pull request #177 from 1e5i-Shark/dev #49

Merge pull request #177 from 1e5i-Shark/dev

Merge pull request #177 from 1e5i-Shark/dev #49

Workflow file for this run

name: CD
on:
push:
branches:
- main
permissions: write-all
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# gradle 캐싱
- 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-
# prod properties 설정
- name: Make application-prod.properties
run: |
cd ./src/main/resources
touch ./application-prod.properties
echo "${{ secrets.PROPERTIES_PROD }}" > ./application-prod.properties
shell: bash
# test를 제외한 프로젝트 빌드
- name: Build With Gradle
run: ./gradlew build -x test
# 도커 빌드 & 이미지 push
- name: Docker build & Push
run: |
docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_HUB_TOKEN }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }} .
docker push ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }}
# docker-compose 파일을 ec2 서버에 배포
- name: Deploy to Prod
uses: appleboy/ssh-action@master
id: deploy-prod
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
docker stop algobaro-api
docker rm algobaro-api
sudo docker pull ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }}
docker run -d --name algobaro-api -p 8080:8080 ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }}
docker rmi $(docker images -a | grep "<none>" | awk '{print $3}')