[Setting] 깃헙 액션 파일 수정 #36
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
# github repository actions 페이지에 나타날 이름 | |
name: CI/CD using github actions & docker | |
# event trigger | |
# main이나 develop 브랜치에 push가 되었을 때 실행 | |
on: | |
push: | |
branches: [ "main" ] | |
permissions: | |
contents: read | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
# JDK setting - github actions에서 사용할 JDK 설정 (프로젝트나 AWS의 java 버전과 달라도 무방) | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# gradle caching - 빌드 시간 향상 | |
- 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- | |
# 환경별 yml 파일 생성 - application.yml | |
- name: make application.yml | |
if: | | |
contains(github.ref, 'main') | |
run: | | |
mkdir ./planfit/src/main/resources # resources 폴더 생성 | |
cd ./planfit/src/main/resources # resources 폴더로 이동 | |
touch ./application.yml # application.yml 생성 | |
echo "${{ secrets.YML }}" > ./application.yml # github actions에서 설정한 값을 application.yml 파일에 쓰기 | |
shell: bash | |
# gradle build | |
- name: Build with Gradle | |
run: | | |
./planfit/gradlew build --project-dir ./planfit -x test | |
# docker login | |
- name: Docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# docker build & push to production | |
- name: Docker build & push to prod | |
if: contains(github.ref, 'main') | |
run: | | |
pwd | |
docker build -f ./planfit/Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/planfit . | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/planfit | |
## deploy to production | |
- name: Deploy to prod | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
if: contains(github.ref, 'main') | |
with: | |
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS | |
username: ubuntu | |
key: ${{ secrets.PEM_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
sudo docker ps -q --filter ancestor=${{ secrets.DOCKERHUB_USERNAME }}/planfit | xargs -r sudo docker stop | xargs -r sudo docker rm | |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/planfit | |
sudo docker run -d --name planfit_container -p 8081:8081 ${{ secrets.DOCKERHUB_USERNAME }}/planfit | |
sudo docker image prune -f |