Skip to content

Commit 3b3314a

Browse files
committed
[feat] ci/cd 추가
1 parent e38db8a commit 3b3314a

File tree

2 files changed

+73
-25
lines changed

2 files changed

+73
-25
lines changed

.github/workflows/cicd.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: CI/CD
2+
3+
on:
4+
push:
5+
branches: [ dev ]
6+
pull_request:
7+
branches: [ main, dev ]
8+
9+
env:
10+
DOCKER_IMAGE: ghcr.io/${{ github.actor }}/myproject
11+
VERSION: ${{ github.sha }}
12+
NAME: ness-fastapi
13+
# Docker image 를 ghcr.io 에 올릴 때 우리의github이름/이미지이름 으로 저장한다. 이미지이름을 정해주면 된다.
14+
# Docker image 의 이름을 newproject 이라고 해놓은 것. 이름 뭐할지 정하면 된다.
15+
16+
jobs:
17+
build:
18+
name: Build
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v2
23+
24+
- name: Setup docker buildx
25+
id: buildx
26+
uses: docker/setup-buildx-action@v1
27+
28+
- name: Cache docker layers
29+
uses: actions/cache@v2
30+
with:
31+
path: /tmp/.buildx-cache
32+
key: ${{ runner.os }}-buildx-${{ env.VERSION }}
33+
restore-keys: |
34+
${{ runner.os }}-buildx-
35+
- name: Login to ghcr
36+
uses: docker/login-action@v1
37+
with:
38+
registry: ghcr.io
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.GHCR_TOKEN }}
41+
42+
- name: Build and push
43+
id: docker_build
44+
uses: docker/build-push-action@v2
45+
with:
46+
builder: ${{ steps.buildx.outputs.name }}
47+
push: true
48+
tags: ${{ env.DOCKER_IMAGE }}:latest
49+
50+
deploy:
51+
needs: build
52+
name: Deploy
53+
runs-on: [ self-hosted, label-NESS ]
54+
# label-newproject 라는 이름으로 AWS EC2 가 Runner 를 작동시킬 때 사용했던 그 label
55+
steps:
56+
- name: Login to ghcr
57+
uses: docker/login-action@v1
58+
with:
59+
registry: ghcr.io
60+
username: ${{ github.actor }}
61+
password: ${{ secrets.GHCR_TOKEN }}
62+
63+
- name: Docker run
64+
run: |
65+
docker stop ${{ env.NAME }} && docker rm ${{ env.NAME }} && docker rmi ${{ env.DOCKER_IMAGE }}:latest
66+
docker run -d -p 3000:3000 --name newproject --restart always ${{ env.DOCKER_IMAGE }}:latest

Dockerfile

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
1-
# Python 이미지를 기반으로 설정
1+
22
FROM python:3.9
33

44
# 작업 디렉토리 설정
5-
WORKDIR /app
5+
WORKDIR /code
66

77
# 환경변수 설정
8-
ENV PYTHONDONTWRITEBYTECODE 1
9-
ENV PYTHONUNBUFFERED 1
8+
# ENV PYTHONDONTWRITEBYTECODE 1
9+
# ENV PYTHONUNBUFFERED 1
1010

1111
# 의존성 설치
12-
COPY requirements.txt .
13-
RUN pip install --no-cache-dir -r requirements.txt
14-
15-
# 소스 코드 복사
16-
COPY . .
17-
18-
# 애플리케이션 실행
19-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
20-
21-
# 나의 python 버전
22-
FROM python:3.11.1
23-
24-
# /code 폴더 만들기
25-
WORKDIR /code
26-
27-
# ./requirements.txt 를 /code/requirements.txt 로 복사
2812
COPY ./requirements.txt /code/requirements.txt
13+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
2914

30-
# requirements.txt 를 보고 모듈 전체 설치(-r)
31-
RUN pip install --no-cache-dir -r /code/requirements.txt
32-
33-
# 이제 app 에 있는 파일들을 /code/app 에 복사
15+
# 소스 코드 복사
3416
COPY ./app /code/app
3517

36-
# 실행
18+
# 어플리케이션 실행
3719
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]

0 commit comments

Comments
 (0)