-
Notifications
You must be signed in to change notification settings - Fork 0
116 lines (93 loc) · 3.99 KB
/
aws_cicd_final.yaml
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: Build, Push Docker Image and Deploy to AWS EC2
on:
push:
paths:
- '.github/workflows/aws_cicd_final.yaml'
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3
- name: Run my custom Docker build/test/push action
uses: ./.github/actions/custom-docker-build
with:
image_name: "ryankor/devops-sparta"
image_tag: "aws-cicd-final-0.0.2"
context: "04-test-fastapi"
file: "04-test-fastapi/Dockerfile"
만약 사용자/패스워드를 받아야 한다면(위 action.yml에서 추가했다면):
docker_username: ${{ secrets.DOCKER_USERNAME }}
docker_password: ${{ secrets.DOCKER_PASSWORD }}
deploy-to-ec2:
name: Deploy to AWS EC2
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Check AWS CLI version
run: aws --version
- name: Check EC2 Instances Status
run: |
aws ec2 describe-instances --query 'Reservations[*].Instances[*].{Name: Tags[?Key==`Name`].Value | [0], State: State.Name}' --output table
- name: SSH into EC2 and deploy Docker container
uses: appleboy/ssh-action@v1.2.0
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
timeout: 180s
script: |
set -e
# Docker 설치 여부 확인
if ! command -v docker &> /dev/null
then
echo "Docker not found. Installing Docker..."
# 업데이트 및 필수 패키지 설치
sudo apt-get update -y
sudo apt-get install -y \
ca-certificates \
curl \
gnupg \
lsb-release
# Docker 공식 GPG 키 추가
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Docker 저장소 설정
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Docker Engine 설치
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 현재 사용자를 docker 그룹에 추가 (필요 시)
sudo usermod -aG docker $USER
echo "Docker installed successfully."
else
echo "Docker is already installed."
fi
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p "${{ secrets.DOCKER_PASSWORD }}"
# 기존 컨테이너 중지 및 제거
if [ "$(sudo docker ps -q -f name=aws-fastapi-final)" ]; then
sudo docker stop aws-fastapi-final
echo "Running Container Stopped, aws-fastapi-final"
fi
if [ "$(sudo docker ps -aq -f status=exited -f name=aws-fastapi-final)" ]; then
sudo docker rm aws-fastapi-final
echo "Running Container Removed, aws-fastapi-final"
fi
IMAGE="${{ secrets.DOCKER_USERNAME }}/devops-sparta:aws-cicd-final-0.0.2"
echo "Pulling Docker image: $IMAGE"
sudo docker pull "$IMAGE"
sudo docker run -d \
-p 8000:8000 \
--name aws-fastapi-final \
"$IMAGE"
echo "Docker container deployed successfully."