๐ ๏ธ Style: ๋ณ์ ์ด๋ฆ, Code Convention ์ผ์นํ ๋ฐ README.md ์ถ๊ฐ (#32) #13
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: 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 # ๊ตฌ๋ฒ์ ์ ๋์ปค ์ด๋ฏธ์ง ์ ๊ฑฐ |