-
Notifications
You must be signed in to change notification settings - Fork 2
89 lines (78 loc) · 3.28 KB
/
github-actions.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
name: CI/CD using GitHub Actions & Docker
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_run:
workflows: [ "CI/CD using GitHub Actions & Docker" ]
types:
- completed
branches: [ "main" ]
result: failure
permissions:
contents: read
jobs:
CI-CD:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ' 3.11.6'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# application.yml 파일 생성
- name: make application.yml
if: |
contains(github.ref, 'main') ||
contains(github.ref, 'develop')
run: |
mkdir -p ./config # config 폴더 생성
echo "${{ secrets.YML }}" > ./config/application.yml # GitHub Secrets에서 설정한 값을 application.yml 파일에 쓰기
# # 프로젝트 실행
# - name: Run project
# run: |
# python3 app.py
# Docker 빌드 & 푸시
- name: Docker build & push
if: github.ref == 'refs/heads/main' # 메인 브랜치에 푸시했을 때만 실행
run: |
docker login -u ${{ secrets.DOCKERHUB_NAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
docker build -t ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest .
docker push ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
# EC2로 배포
- name: Deploy to EC2
if: github.ref == 'refs/heads/main' # 메인 브랜치에 푸시했을 때만 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
script: |
# 실행 중인 동일한 컨테이너가 있으면 중지하고 제거
sudo docker stop $(sudo docker ps -aqf "name=${{ secrets.DOCKERHUB_REPOSITORY }}") || true
sudo docker rm $(sudo docker ps -aqf "name=${{ secrets.DOCKERHUB_REPOSITORY }}") || true
# 새로운 이미지를 pull 받음
sudo docker pull ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY}}
# 새로운 컨테이너를 실행
sudo docker run -d -p 5000:5000 --name ${{ secrets.DOCKERHUB_REPOSITORY }} ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY}}
# # EC2로 배포
# - name: Deploy to EC2
# if: github.ref == 'refs/heads/main' # 메인 브랜치에 푸시했을 때만 실행
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS
# username: ubuntu
# key: ${{ secrets.PRIVATE_KEY }}
# script: |
# sudo docker login -u ${{ secrets.DOCKERHUB_NAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
# sudo docker-compose stop ${{ secrets.DOCKER_SERVICE_NAME }}
# sudo docker-compose rm -f ${{ secrets.DOCKER_SERVICE_NAME }}
# sudo docker pull ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY}}
# sudo docker tag ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DOCKERHUB_REPOSITORY}} ${{ secrets.DOCKER_IMAGE_NAME }}
# sudo docker-compose up -d