-
Notifications
You must be signed in to change notification settings - Fork 0
66 lines (55 loc) · 1.96 KB
/
deploy.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
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout the code
- name: Checkout Code
uses: actions/checkout@v2
# Set up AWS CLI
- 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: ${{ secrets.AWS_REGION }}
# Log in to Amazon ECR
- name: Login to Amazon ECR
id: ecr_login
run: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REPOSITORY }}
# Build the Docker image
- name: Build Docker image
run: |
docker build -t my_repo .
docker tag my_repo:latest ${{ secrets.ECR_REPOSITORY }}/my_repo:latest
# Push the Docker image to Amazon ECR
- name: Push Docker image to ECR
run: |
docker push ${{ secrets.ECR_REPOSITORY }}/my_repo:latest
deploy:
runs-on: ubuntu-latest
needs: build
steps:
# Adding SSH key
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Add SSH known hosts (EC2 public IP or domain)
run: |
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: SSH into EC2 instance and pull
run: |
ssh -i ~/.ssh/id_rsa ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REPOSITORY }}
cd ~/doneify_dir
docker compose pull ${{ secrets.ECR_REPOSITORY }}/my_repo:latest
docker compose down
docker compose up -d
EOF