Merge pull request #122 from 22caps/dev #51
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 | |
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 }} | |
port: ${{ secrets.EC2_PORT }} | |
envs: GITHUB_SHA | |
script: | | |
docker stop caps-api | |
docker rm caps-api | |
sudo docker pull ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }} | |
docker run -d --name caps-api -p 8080:8080 ${{ secrets.DOCKER_ID }}/${{ secrets.DOCKER_REPO }} | |
docker rmi $(docker images -a | grep "<none>" | awk '{print $3}') |