Skip to content

[#2]๐Ÿ›Fix: ํ† ํฐ ๋ฐœ๊ธ‰ ๋ฐฉ์‹ ์ˆ˜์ • #69

[#2]๐Ÿ›Fix: ํ† ํฐ ๋ฐœ๊ธ‰ ๋ฐฉ์‹ ์ˆ˜์ •

[#2]๐Ÿ›Fix: ํ† ํฐ ๋ฐœ๊ธ‰ ๋ฐฉ์‹ ์ˆ˜์ • #69

Workflow file for this run

# ํ•„์š”ํ•œ Repo Secret ์„ค์ •
#### CI
# ${{ secrets.SUBMODULE_ACCESS_TOKEN }} : ๊นƒํ—ˆ๋ธŒ ์•ก์„ธ์Šค ํ† ํฐ
#### CD
# ${{ secrets.DOCKER_ID }} : ๋„์ปคํ—ˆ๋ธŒ id
# ${{ secrets.DOCKER_PASSWORD }} : ๋„์ปคํ—ˆ๋ธŒ pw
# ${{ secrets.REMOTE_HOST_DEV }} : ๋ฐฐํฌ ์„œ๋ฒ„ HOSTNAME
# ${{ secrets.REMOTE_PORT_DEV }} : ๋ฐฐํฌ ์„œ๋ฒ„ PORT
# ${{ secrets.REMOTE_USERNAME_DEV }} : ๋ฐฐํฌ ์„œ๋ฒ„ USERNAME
# ${{ secrets.REMOTE_SSH_KEY_DEV }} : ๋ฐฐํฌ ์„œ๋ฒ„ ์—ฐ๊ฒฐ์„ ์œ„ํ•œ SSH KEY
name: Backend CI & CD (dev)
on:
pull_request:
branches: [main]
push:
branches: [main]
env:
CONTAINER_NAME: yesummit
jobs:
Continuous-Integration:
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
# CI ์‹คํ–‰ (ํ™˜๊ฒฝ์€ github ์ œ๊ณต)
runs-on: ubuntu-20.04
steps:
# ์†Œ์Šค์ฝ”๋“œ ์ฒดํฌ์•„์›ƒ
- name: Checkout source code
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.ACTION_TOKEN }}
ref: ${{ github.head_ref }}
- name: Install JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'zulu'
cache: 'gradle'
# Gradle Package Caching
- name: Caching Gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
- name: Grant execute permission for gradle
run: chmod +x ./gradlew
# develop ๋ธŒ๋žœ์น˜์ผ ๊ฒฝ์šฐ dev ํ™˜๊ฒฝ ๋นŒ๋“œ
# ํ˜„์žฌ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„. test ์—†์ด ๋นŒ๋“œํ•จ
- name: create build file
run: ./gradlew clean build -x test -i --no-daemon -Dspring.profiles.active=prod
# push event์ผ ๊ฒฝ์šฐ CD job์— jar file ์—…๋กœ๋“œ
- name: (Push) Archive production artifacts
if: github.event_name == 'push'
uses: actions/upload-artifact@v4
with:
name: build
path: build/libs/*.jar
Continuous-Deploy:
# push ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ฐฐํฌ JOB ์‹คํ–‰
if: github.event_name == 'push'
needs: Continuous-Integration
runs-on: ubuntu-latest
steps:
# ์†Œ์Šค์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ
- name: Checkout source code
uses: actions/checkout@v4
# ์ด์ „ Job์—์„œ ์—…๋กœ๋“œํ•œ Jar file ๋‹ค์šด๋กœ๋“œ
- name : Download a built Jar File
uses: actions/download-artifact@v4
with:
name: build
path: build/libs
# Docker Buildx Setting
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Docker Login
- name: Docker Login
uses: docker/[email protected]
with:
# Username used to log against the Docker registry
username: ${{ secrets.DOCKER_ID }}
# Password or personal access token used to log against the Docker registry
password: ${{ secrets.DOCKER_PASSWORD }}
# Docker Build & Push
- name: Docker Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile-dev
platforms: linux/amd64
push: true
tags: |
${{ secrets.DOCKER_ID }}/${{ env.CONTAINER_NAME }}:${{github.run_number}}
${{ secrets.DOCKER_ID }}/${{ env.CONTAINER_NAME }}:latest
cache-from: type=gha # gha=Github Action Cache
cache-to: type=gha,mode=max
- name: Create and execute deploy script
run: |
echo '#!/bin/bash' > deploy.sh
echo 'sudo docker rm -f ${{ env.CONTAINER_NAME }}' >> deploy.sh
echo 'sudo docker rmi ${{ secrets.DOCKER_ID }}/${{ env.CONTAINER_NAME }}' >> deploy.sh
echo 'sudo docker pull ${{ secrets.DOCKER_ID }}/${{ env.CONTAINER_NAME }}' >> deploy.sh
echo 'sudo docker run -d -p 8080:8080 --add-host host.docker.internal:host-gateway --restart=unless-stopped --log-opt max-size=10m --log-opt max-file=3 --name ${{ env.CONTAINER_NAME }} ${{ secrets.DOCKER_ID }}/${{ env.CONTAINER_NAME }}' >> deploy.sh
- name: Transfer Deploy Script use SCP
uses: appleboy/scp-action@master
with:
host: ${{ secrets.REMOTE_HOST_DEV }}
port: ${{ secrets.REMOTE_PORT_DEV }}
username: ${{ secrets.REMOTE_USERNAME_DEV }}
key: ${{ secrets.REMOTE_SSH_KEY_DEV }}
source: deploy.sh
target: /home/${{ secrets.REMOTE_USERNAME_DEV }}/deploy
# SSH Connect
- name: Execute Server Init Script
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST_DEV }}
port: ${{ secrets.REMOTE_PORT_DEV }}
username: ${{ secrets.REMOTE_USERNAME_DEV }}
key: ${{ secrets.REMOTE_SSH_KEY_DEV }}
script_stop: true
script: |
chmod +x /home/${{ secrets.REMOTE_USERNAME_DEV }}/deploy/deploy.sh && sh /home/${{ secrets.REMOTE_USERNAME_DEV }}/deploy/deploy.sh