-
Notifications
You must be signed in to change notification settings - Fork 0
138 lines (113 loc) · 3.83 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
name: CD
env:
REGISTRY: "codemaster482"
IMAGE_NAME: "hammywallet"
CONTAINER_NAME: "hammywallet-api"
FOLDER_COMPOSE: "production"
on:
push:
branches:
- main
- deploy
pull_request:
branches:
- main
- 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 }}
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
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 }}
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 system prune -f
sudo docker-compose down
sudo docker-compose up -d