Skip to content

๐Ÿ› ๏ธ Style: ๋ณ€์ˆ˜ ์ด๋ฆ„, Code Convention ์ผ์น˜ํ™” ๋ฐ README.md ์ถ”๊ฐ€ (#32) #13

๐Ÿ› ๏ธ Style: ๋ณ€์ˆ˜ ์ด๋ฆ„, Code Convention ์ผ์น˜ํ™” ๋ฐ README.md ์ถ”๊ฐ€ (#32)

๐Ÿ› ๏ธ Style: ๋ณ€์ˆ˜ ์ด๋ฆ„, Code Convention ์ผ์น˜ํ™” ๋ฐ README.md ์ถ”๊ฐ€ (#32) #13

Workflow file for this run

name: Build and Push Docker Image and Deploy
# main ๋ธŒ๋žœ์น˜์— push or PR์ด ์˜ค๋ฉด ์‹คํ–‰
on:
push:
branches:
- main
jobs:
# ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# secret.py ํŒŒ์ผ ์ƒ์„ฑ
- name: Create secret.py
run: |
echo "openai_api_key='${{ secrets.OPENAI_API_KEY }}'" > secret.py
echo "NAVER_CLOUD_ACCESS_KEY='${{ secrets.NAVER_CLOUD_ACCESS_KEY }}'" >> secret.py
echo "NAVER_CLOUD_SECRET_KEY='${{ secrets.NAVER_CLOUD_SECRET_KEY }}'" >> secret.py
echo "DB_HOST='${{ secrets.DB_HOST }}'" >> secret.py
echo "DB_USER='${{ secrets.DB_USER }}'" >> secret.py
echo "DB_PASSWORD='${{ secrets.DB_PASSWORD }}'" >> secret.py
# ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ
- name: Docker build
run: |
docker build -t ${{ secrets.DOCKER_IMAGE_NAME }}:latest . # ๋„์ปค ๋นŒ๋“œ
# ๋„์ปค ๋กœ๊ทธ์ธ
- name: Docker Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# ๋„์ปค ์ด๋ฏธ์ง€ push
- name: Push docker images
run: |
docker push ${{ secrets.DOCKER_IMAGE_NAME }}:latest
# ๋„์ปค ์ด๋ฏธ์ง€ EC2 ์ธ์Šคํ„ด์Šค์— ๋ฐฐํฌ
deploy-to-ec2:
needs: build-and-push
runs-on: ubuntu-24.04
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }} # EC2 IP ์ฃผ์†Œ
username: ${{ secrets.EC2_USER }} # EC2 ์‚ฌ์šฉ์ž
key: ${{ secrets.PRIVATE_KEY }} # pem ํ‚ค
script_stop: false # ์Šคํฌ๋ฆฝํŠธ ์ค‘์ง€ ๋ฐฉ์ง€
script: |
# ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€
CONTAINER_ID=$(sudo docker ps -aq --filter "name=golang-ai")
if [ ! -z "$CONTAINER_ID" ]; then
sudo docker stop $CONTAINER_ID || true
sudo docker rm -f $CONTAINER_ID || true
fi
# ์ตœ์‹  ๋„์ปค ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰
sudo docker pull ${{ secrets.DOCKER_IMAGE_NAME }}:latest # ๋„์ปค ์ตœ์‹  ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
sudo docker run --name golang-ai -d -v /home/ubuntu/files:/home/ubuntu/files -p 8000:8000 ${{ secrets.DOCKER_IMAGE_NAME }}:latest
sudo docker image prune -f # ๊ตฌ๋ฒ„์ „์˜ ๋„์ปค ์ด๋ฏธ์ง€ ์ œ๊ฑฐ