-
Notifications
You must be signed in to change notification settings - Fork 0
83 lines (70 loc) · 3.08 KB
/
cd.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
name: cd
on:
workflow_dispatch:
pull_request:
branches: [main]
env:
AWS_REGION: ap-south-1
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
PRIVATE_SSH_KEY: ${{ secrets.AWS_SSH_KEY }}
SERVER_PUBLIC_IP: ${{ secrets.AWS_PUBLIC_KEY }}
jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
- name: Checkout code
uses: actions/checkout@v3
- name: Build the app
run: |
chmod +x ./scripts/build.sh
./scripts/build.sh
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: verses
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: install goose
run: go install github.com/pressly/goose/v3/cmd/goose@latest
- name: Run migrations
run: |
cd sql/schema
goose postgres ${{secrets.DB_CONN}} up
- name: Deploy docker image to EC2
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: verses
IMAGE_TAG: latest
AWS_DEFAULT_REGION: ap-south-1
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_PUBLIC_KEY}}
username: ubuntu
key: ${{ secrets.AWS_SSH_KEY }}
envs: PRIVATE_SSH_KEY,REGISTRY,REPOSITORY,IMAGE_TAG,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_DEFAULT_REGION,AWS_REGION
script: |-
sudo apt update
sudo apt install docker.io -y
sudo apt install awscli -y
sudo $(aws ecr get-login --no-include-email --region ap-south-1);
sudo docker stop deploy || true
sudo docker rm deploy || true
sudo docker pull $REGISTRY/$REPOSITORY:$IMAGE_TAG
sudo docker run -d -e PORT=80 -e JWT_SECRET=${{secrets.JWT_TOKEN}} -e GOLD_KEY=nfjd878 -e DB_CONN=${{secrets.DB_CONN}} --name deploy -p 80:80 $REGISTRY/$REPOSITORY:$IMAGE_TAG