-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (158 loc) · 6.03 KB
/
CD.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: CD
env:
REGISTRY: "codemaster482"
IMAGE_NAME: "hammywallet"
CONTAINER_NAME: "hammywallet-api"
FOLDER_COMPOSE: "production"
AUTH_CONTAINER: "hammywallet-auth"
ACCOUNT_CONTAINER: "hammywallet-account"
CATEGORY_CONTAINER: "hammywallet-category"
AUTH_ADDR: auth:8010
ACCOUNT_ADDR: account:8020
CATEGORY_ADDR: category:8030
on:
push:
branches:
- deploy
pull_request:
branches:
- deploy
jobs:
lint_and_test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
- name: Create test folder
run: |
sudo mkdir /images
sudo chmod -R 777 /images
- name: Test with the Go CLI
run: go test ./...
push_to_dockerhub:
runs-on: ubuntu-latest
needs: lint_and_test
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Buildx
uses: docker/setup-buildx-action@v2
- name: DockerHub login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | head -c 8)" >> $GITHUB_ENV
- name: Build docker and push to dockerhub of CodeMaster
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
file: ./build/Dockerfile
build-args: |
IMAGE_NAME=${{ env.IMAGE_NAME }}
REGISTRY=${{ env.REGISTRY }}
GITHUB_SHA_SHORT=${{ env.GITHUB_SHA_SHORT }}
- name: Build and push auth
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.AUTH_CONTAINER }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.AUTH_CONTAINER }}:latest
file: ./build/auth.Dockerfile
build-args: |
IMAGE_NAME=${{ env.AUTH_CONTAINER }}-web
REGISTRY=${{ env.REGISTRY }}
GITHUB_SHA_SHORT=${{ env.GITHUB_SHA_SHORT }}
- name: Build and push account
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.ACCOUNT_CONTAINER }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.ACCOUNT_CONTAINER }}:latest
file: ./build/account.Dockerfile
build-args: |
IMAGE_NAME=${{ env.ACCOUNT_CONTAINER }}
REGISTRY=${{ env.REGISTRY }}
GITHUB_SHA_SHORT=${{ env.GITHUB_SHA_SHORT }}
- name: Build and push category
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.CATEGORY_CONTAINER }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.CATEGORY_CONTAINER }}:latest
file: ./build/category.Dockerfile
build-args: |
IMAGE_NAME=${{ env.CATEGORY_CONTAINER }}
REGISTRY=${{ env.REGISTRY }}
GITHUB_SHA_SHORT=${{ env.GITHUB_SHA_SHORT }}
remote_deploy:
runs-on: ubuntu-latest
needs: push_to_dockerhub
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy docker-compose via SSH action
uses: appleboy/scp-action@master
with:
debug: true
host: ${{ secrets.DEPLOY_HOST }}
port: ${{ secrets.DEPLOY_PORT }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.SSHKEY }}
rm: true
source: docker-compose.yml, build/schema/initdb.sql, metrics/prometheus/prometheus.yml, build/account.Dockerfile, build/auth.Dockerfile, build/category.Dockerfile
target: ~/${{ env.FOLDER_COMPOSE }}
- name: Get docker form dockerhub via SSH action
uses: appleboy/[email protected]
with:
debug: true
host: ${{ secrets.DEPLOY_HOST }}
port: ${{ secrets.DEPLOY_PORT }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.SSHKEY }}
envs: REGISTRY, IMAGE_NAME, CONTAINER_NAME, GITHUB_SHA, FOLDER_COMPOSE
script: |
sudo bash
cd $(echo $FOLDER_COMPOSE)
cat <<EOF>.env
DB_NAME=${{ secrets.DB_NAME }}
DB_USER=${{ secrets.POSTGRES_USER }}
DB_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
DB_HOST=${{ secrets.DB_HOST }}
DB_PORT=${{ secrets.DB_PORT }}
DB_SSLMODE=disable
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
SECRET=${{ secrets.SECRET }}
IMAGE_NAME=${{ env.IMAGE_NAME }}
AUTH_CONTAINER=${{ env.AUTH_CONTAINER }}
ACCOUNT_CONTAINER=${{ env.ACCOUNT_CONTAINER }}
CATEGORY_CONTAINER=${{ env.CATEGORY_CONTAINER }}
REGISTRY=${{ env.REGISTRY }}
CONTAINER_NAME=${{ env.CONTAINER_NAME }}
REDIS_HOST=${{ secrets.REDIS_HOST }}
REDIS_PORT=${{ secrets.REDIS_PORT }}
EOF
echo "GITHUB_SHA_SHORT=`echo $GITHUB_SHA | head -c8`" >> .env
echo ${{ secrets.DOCKERHUB_TOKEN }} | sudo docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
sudo docker pull $REGISTRY/$IMAGE_NAME:latest
sudo docker pull $REGISTRY/$AUTH_CONTAINER:latest
sudo docker pull $REGISTRY/$ACCOUNT_CONTAINER:latest
sudo docker pull $REGISTRY/$CATEGORY_CONTAINER:latest
sudo docker system prune -f
sudo docker-compose down
sudo docker-compose up -d
sudo sleep 7
sudo chown ubuntu:ubuntu api-logs