Merge pull request #353 #314
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: CI/CD using github actions & docker | |
on: | |
push: | |
branches: [ "main", "develop" ] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '21' | |
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- | |
# 환경별 properties 파일 생성(1) - 공통 실행 환경 (현재 ide 환경 설정이 안되서 주석처리함) | |
# 경로 이동 시 mos 하위에 있는 mos 폴더로 들어가야됨! | |
# - name: make application.properties | |
# if: | | |
# contains(github.ref, 'main') || | |
# contains(github.ref, 'develop') | |
# run: | | |
# mkdir -p mos/src/main/resources | |
# cd mos/src/main/resources | |
# touch ./application.properties | |
# echo "${{ secrets.PROPERTIES }}" > ./application.properties | |
# shell: bash | |
# 환경별 yml 파일 생성(2) - dev | |
# 경로 이동 시 mos 하위에 있는 mos 폴더로 들어가야됨! | |
- name: make application-dev.properties | |
if: contains(github.ref, 'develop') | |
run: | | |
mkdir -p mos/src/main/resources | |
cd mos/src/main/resources | |
touch ./application-dev.properties | |
echo "${{ secrets.PROPERTIES_DEV }}" > ./application-dev.properties | |
shell: bash | |
# 환경별 yml 파일 생성(3) - prod | |
# 경로 이동 시 mos 하위에 있는 mos 폴더로 들어가야됨! | |
- name: make application-prod.properties | |
if: contains(github.ref, 'main') | |
run: | | |
mkdir -p mos/src/main/resources | |
cd mos/src/main/resources | |
touch ./application-prod.properties | |
echo "${{ secrets.PROPERTIES_PROD }}" > ./application-prod.properties | |
shell: bash | |
# gradle build | |
- name: Build with Gradle | |
run: | | |
echo $(pwd) | |
echo $(ls) | |
cd mos | |
echo $(ls) | |
chmod +x ./gradlew | |
./gradlew clean build -x test | |
# docker build & push to production | |
- name: Docker build & push to prod | |
if: contains(github.ref, 'main') | |
run: | | |
cd mos | |
echo $(ls) | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/docker-test-prod . | |
docker push ${{ secrets.DOCKER_USERNAME }}/docker-test-prod | |
# docker build & push to develop | |
- name: Docker build & push to dev | |
if: contains(github.ref, 'develop') | |
run: | | |
cd mos | |
echo $(ls) | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/docker-test-dev . | |
docker push ${{ secrets.DOCKER_USERNAME }}/docker-test-dev | |
## 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 }} | |
# username: ubuntu | |
# key: ${{ secrets.PRIVATE_KEY }} | |
# envs: GITHUB_SHA | |
# script: | | |
# sudo docker ps | |
# sudo docker pull ${{ secrets.DOCKER_USERNAME }}/docker-test-prod | |
# sudo docker run -d -p 8082:8082 ${{ secrets.DOCKER_USERNAME }}/docker-test-prod | |
# sudo docker image prune -f | |
## deploy to develop | |
- name: Deploy to dev | |
uses: appleboy/ssh-action@master | |
id: deploy-dev | |
if: contains(github.ref, 'develop') | |
with: | |
host: ${{ secrets.HOST_DEV }} | |
username: ${{ secrets.USERNAME }} | |
password: ${{ secrets.PASSWORD }} | |
port: 22 | |
key: ${{ secrets.PRIVATE_KEY }} | |
script: | | |
docker ps -aq --filter name=modu_study | grep -q . && docker rm -f $(docker ps -aq --filter name=modu_study) | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/docker-test-dev | |
sudo docker run -d --name modu_study -p 8888:8888 ${{ secrets.DOCKER_USERNAME }}/docker-test-dev | |
sudo docker image prune -f | |
# slack으로 메시지 발송 | |
- name: send message to slack | |
uses: 8398a7/action-slack@v3 | |
with: | |
status: ${{ job.status }} | |
author_name: zhtmr | |
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took | |
mention: here | |
if_mention: failure,cancelled | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required. | |
if: always() # Pick up events even if the job fails or is canceled. | |
# run: curl -X POST -H "Content-type:application/json" --data '{"text":"배포 완료!"}' ${{ secrets.SLACK_WEBHOOK_URL }} |