1
+ name : CI/CD of Develop branch to Dev Server
2
+
3
+ on :
4
+ push :
5
+ branches :
6
+ - develop
7
+
8
+ jobs :
9
+ build-and-push :
10
+ name : Build and Push Docker image to ECR
11
+ runs-on : ubuntu-latest
12
+ outputs :
13
+ ecr_registry : ${{ steps.login-ecr.outputs.registry }}
14
+ steps :
15
+ - name : Checkout repo
16
+ uses : actions/checkout@v3
17
+
18
+ - name : Configure AWS credentials
19
+ uses : aws-actions/configure-aws-credentials@v4
20
+ with :
21
+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
22
+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
23
+ aws-region : ap-northeast-2
24
+
25
+ - name : Login to Amazon ECR
26
+ id : login-ecr
27
+ uses : aws-actions/amazon-ecr-login@v2
28
+ with :
29
+ mask-password : ' false'
30
+
31
+ - name : Build, tag, and push docker image to Amazon ECR
32
+ env :
33
+ ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
34
+ ECR_REPOSITORY : fastapi/new_dev
35
+ run : |
36
+ docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }} .
37
+ docker push $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}
38
+
39
+ - name : Output the image URI
40
+ run : |
41
+ echo "Image URI: $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}"
42
+
43
+ deploy-to-ec2 :
44
+ name : Deploy to EC2
45
+ needs : build-and-push
46
+ runs-on : ubuntu-latest
47
+ env :
48
+ CONTAINER_NAME : fastapi_new_dev_container
49
+ ECR_REGISTRY : ${{ needs.build-and-push.outputs.ecr_registry }}
50
+ ECR_REPOSITORY : fastapi/new_dev
51
+ steps :
52
+ - name : Check Env value
53
+ run :
54
+ echo $ECR_REGISTRY
55
+ echo $CONTAINER_NAME
56
+ echo $ECR_REPOSITORY
57
+ - name : Deploy Docker image to EC2
58
+ uses : appleboy/ssh-action@master
59
+ with :
60
+ host : ${{ secrets.DEV_BASTION_HOST }}
61
+ username : ${{ secrets.EC2_USERNAME }}
62
+ key : ${{ secrets.DEV_BASTION_SSH_PRIVATE_KEY }}
63
+ script : |
64
+ pwd # Debugging check
65
+ ls # Debugging check
66
+ ssh -o StrictHostKeyChecking=no -i new-dev-an2-ono-fastapi-key.pem ubuntu@${{ secrets.DEV_FASTAPI_SERVER_IP }} << 'EOSSH'
67
+ export CONTAINER_NAME=fastapi_new_dev_container
68
+ export ECR_REPOSITORY=fastapi/new_dev
69
+ aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${{secrets.ECR_REGISTRY}}
70
+ docker pull ${{secrets.ECR_REGISTRY}}/$ECR_REPOSITORY:${{ github.sha }}
71
+ docker stop $CONTAINER_NAME || true
72
+ docker rm $CONTAINER_NAME || true
73
+ docker run -d --name $CONTAINER_NAME -p 8000:8000 --restart always ${{secrets.ECR_REGISTRY}}/$ECR_REPOSITORY:${{ github.sha }}
74
+ EOSSH
0 commit comments