Skip to content

[release] ์ตœ์ตœ์ข… ๋ฐฐํฌ #43

[release] ์ตœ์ตœ์ข… ๋ฐฐํฌ

[release] ์ตœ์ตœ์ข… ๋ฐฐํฌ #43

Workflow file for this run

# Workflow ์ด๋ฆ„
name: CD workflow
# Event Trigger ํ™˜๊ฒฝ
on:
pull_request:
branches: [ "main" ] # push๊ฐ€ main ๋ธŒ๋žœ์น˜์— ์ƒ์„ฑ๋˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ
permissions: # ์›Œํฌํ”Œ๋กœ์šฐ ๊ถŒํ•œ
id-token: write
contents: read # ์ฝ๊ธฐ
jobs:
build:
# ์‹คํ–‰ํ™˜๊ฒฝ ์„ค์ •
runs-on: ubuntu-24.04
# Action์„ ์‚ฌ์šฉํ•˜์—ฌ Step์„ ๊ตฌ์„ฑ
steps:
# GitHub repository ์ฝ”๋“œ ์ฒดํฌ์•„์›ƒ
- name: Checkout the repository
uses: actions/checkout@v4
# JDK 21 ์„ค์น˜
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
# resources ํด๋” ์ƒ์„ฑ
- name: Create resources folder if not exist
run: |
if [ ! -d "./src/main/resources" ]; then
mkdir -p ./src/main/resources
fi
# application.yml ํŒŒ์ผ ์ƒ์„ฑ
- name: make application.yml
run: |
touch ./src/main/resources/application.yml
echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml
shell: bash
# cloud ํด๋” ์ƒ์„ฑ
- name: Create cloud folder if not exist
run: |
if [ ! -d "./src/main/resources/cloud" ]; then
mkdir -p ./src/main/resources/cloud
fi
# application-cloud.yml ํŒŒ์ผ ์ƒ์„ฑ
- name: make application-cloud.yml
run: |
touch ./src/main/resources/cloud/application-cloud.yml
echo "${{ secrets.APPLICATION_CLOUD_YML }}" > ./src/main/resources/cloud/application-cloud.yml
shell: bash
# openapi ํด๋” ์ƒ์„ฑ
- name: Create cloud folder if not exist
run: |
if [ ! -d "./src/main/resources/openapi" ]; then
mkdir -p ./src/main/resources/openapi
fi
# application-spotify.yml ํŒŒ์ผ ์ƒ์„ฑ
- name: make application-spotify.yml
run: |
touch ./src/main/resources/openapi/application-spotify.yml
echo "${{ secrets.APPLICATION_SPOTIFY_YML }}" > ./src/main/resources/openapi/application-spotify.yml
shell: bash
# ๋นŒ๋“œ ์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•œ Gradle ์บ์‹ฑ
- name: Gradle Caching
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-${{ runner.os }}-
# ๋นŒ๋“œ๋ฅผ ์œ„ํ•œ ๊ถŒํ•œ ๋ถ€์—ฌ
- name: Grant execute permission for gradlew
run: chmod +x gradlew # Gradle wrapper์— ์‹คํ–‰ ๊ถŒํ•œ ๋ถ€์—ฌ
# Gradle์„ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œ ์‹คํ–‰
- name: Build with Gradle Wrapper
run: ./gradlew clean build -x test
# Docker ๋กœ๊ทธ์ธ
- name: login docker hub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Docker ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ
- name: install docker buildx
uses: docker/[email protected]
- name: docker image build & push
run: |
docker build --platform linux/amd64 -t confetiserver/deploy .
docker push confetiserver/deploy
deploy:
needs: build
# ์‹คํ–‰ํ™˜๊ฒฝ ์„ค์ •
runs-on: ubuntu-24.04
environment: production
# Action์„ ์‚ฌ์šฉํ•˜์—ฌ Step์„ ๊ตฌ์„ฑ
steps:
# Github Action ํ™˜๊ฒฝ์˜ Public IP ๊ฐ€์ ธ์˜ค๊ธฐ
- name: Get Github action IP
id: ip
uses: haythem/[email protected]
# AWS ์ธ์ฆ
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
# AWS ECR ๋กœ๊ทธ์ธ
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: true
# ๋ณด์•ˆ ๊ทœ์น™์— ssh IP ์ถ”๊ฐ€
- name: Add Github Actions IP to Security group
run: |
aws ec2 authorize-security-group-ingress --group-name ${{ secrets.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
# ์›๊ฒฉ ์„œ๋ฒ„์— ๋ฐฐํฌ
- name: docker container deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
port: ${{ secrets.PORT }}
script: |
cd ~
docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}"
./deploy.sh
# ๋ณด์•ˆ ๊ทœ์น™์— ssh IP ์‚ญ์ œ
- name: Remove Github Actions IP from security group
run: |
aws ec2 revoke-security-group-ingress --group-name ${{ secrets.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32