Merge branch 'develop' #96
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 | |
# main 또는 deploy 브랜치에 push하면 워크플로우 실행 | |
on: | |
push: | |
branches: [ "main", "deploy" ] | |
permissions: | |
contents: read | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 21 | |
distribution: 'zulu' | |
# 환경별 yml 파일 생성(1) - prod | |
- name: Make application-prod.yml | |
if: | | |
contains(github.ref, 'main') | |
run: | | |
cd ./src/main/resources | |
touch ./application.yml | |
echo "${{ secrets.APPLICATION_PROD }}" > ./application.yml | |
# 환경별 yml 파일 생성(2) - dev | |
- name: Bake application-dev.yml | |
if: contains(github.ref, 'deploy') | |
run: | | |
cd ./src/main/resources | |
touch ./application.yml | |
echo "${{ secrets.APPLICATION }}" > ./application.yml | |
- 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: | | |
chmod +x ./gradlew | |
./gradlew build -x test | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# 브랜치에 따라 Docker 이미지 빌드 및 푸시 | |
- name: Docker build & push - prod | |
if: contains(github.ref, 'main') | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKER_REPO_PROD }}:latest | |
- name: Docker build & push - dev | |
if: contains(github.ref, 'deploy') | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKER_REPO }}:latest | |
# 브랜치에 따라 서버에 배포 | |
- name: Deploy to Server - prod | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
if: contains(github.ref, 'main') | |
with: | |
host: ${{ secrets.HOST_PROD }} | |
username: ${{ secrets.USERNAME_PROD }} | |
key: ${{ secrets.KEY_PROD }} | |
envs: GITHUB_SHA | |
script: | | |
echo "${{ secrets.DOCKER_COMPOSE_PROD }}" | sudo tee ./docker-compose.yml | |
sudo docker ps -q | xargs -r sudo docker stop && sudo docker ps -aq | xargs -r sudo docker rm | |
sudo docker-compose -f ./docker-compose.yml down --rmi all | |
sudo docker pull ${{ secrets.DOCKER_REPO_PROD }}:latest | |
sudo docker-compose -f ./docker-compose.yml up -d | |
- name: Deploy to Server - dev | |
uses: appleboy/ssh-action@master | |
id: deploy-dev | |
if: contains(github.ref, 'deploy') | |
with: | |
host: ${{ secrets.HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.KEY }} | |
envs: GITHUB_SHA | |
script: | | |
echo "${{ secrets.DOCKER_COMPOSE }}" | sudo tee ./docker-compose.yml | |
sudo docker ps -q | xargs -r sudo docker stop && sudo docker ps -aq | xargs -r sudo docker rm | |
sudo docker-compose -f ./docker-compose.yml down --rmi all | |
sudo docker pull ${{ secrets.DOCKER_REPO }}:latest | |
sudo docker-compose -f ./docker-compose.yml up -d |