This repository was archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
217 lines (190 loc) · 6 KB
/
ci.yaml
File metadata and controls
217 lines (190 loc) · 6 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
name: Continuous Integration
on:
push:
branches: [ '*' ]
tags: [ 'v*' ]
pull_request:
permissions:
contents: read
pull-requests: read
env:
GO_VERSION: '1.19'
jobs:
swagger:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Get Swag
run: |
wget https://github.com/swaggo/swag/releases/download/v1.8.6/swag_1.8.6_Linux_x86_64.tar.gz -O swag.tar.gz
tar -xzf swag.tar.gz swag
rm swag.tar.gz
chmod +x swag
- run: ./swag init
- uses: EndBug/add-and-commit@v9
id: commit
with:
add: 'docs'
default_author: github_actions
message: 'docs: update swagger docs'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- uses: actions/checkout@v3
- name: Cache build dependencies
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/_go.sum') }}
- uses: golangci/golangci-lint-action@v3
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- uses: actions/checkout@v3
- name: Cache build dependencies
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/_go.sum') }}
- name: Install dependencies
run: go get .
- name: Test
run: go test -v -race ./...
docker:
needs: [ lint, test ]
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/checkout@v3
- uses: docker/metadata-action@v4
id: meta
with:
images: |
ghcr.io/${{ github.repository }}
tags: |
type=edge
type=ref,event=pr
type=ref,event=branch,prefix=branch-
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- id: commit
uses: pr-mpt/actions-commit-hash@v2
with:
commit: "${{ github.sha }}"
- name: Build
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
build-args: |
VERSION=${{ steps.commit.outputs.short }}
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Check manifest
if: ${{ github.event_name != 'pull_request' }}
run: |
docker buildx imagetools inspect ghcr.io/${{ github.repository }}:${{ steps.meta.outputs.version }}
migrations:
runs-on: ubuntu-latest
services:
postgres:
# Docker Hub image
image: postgres
env:
POSTGRES_PASSWORD: ci
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
db:
- 'db/**'
- name: Download migrate
if: steps.filter.outputs.db == 'true'
run: |
wget https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz -O migrate.tar.gz
tar -xzvf migrate.tar.gz migrate
rm migrate.tar.gz
chmod +x migrate
chmod +x migrate.sh
sudo mv migrate /usr/bin
- name: Set up .env
if: steps.filter.outputs.db == 'true'
run: |
echo "POSTGRES_HOST=localhost" >> .env
echo "POSTGRES_PORT=5432" >> .env
echo "POSTGRES_USER=postgres" >> .env
echo "POSTGRES_PASSWORD=ci" >> .env
echo "POSTGRES_DB=postgres" >> .env
# First, migrate all the way up
- name: Up (1/2)
if: steps.filter.outputs.db == 'true'
run: ./migrate.sh up
# Now check if we can revert everything
- name: Down
if: steps.filter.outputs.db == 'true'
run: yes | ./migrate.sh down
# Lastly check if up + down is idempotent
- name: Up (2/2)
if: steps.filter.outputs.db == 'true'
run: ./migrate.sh up
deploy:
needs: [ migrations, docker ]
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: deploying via ssh
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEPLOY_SSH_HOSTNAME }}
username: ${{ secrets.DEPLOY_SSH_USERNAME }}
key: ${{ secrets.DEPLOY_SSH_PRIVATEKEY }}
port: ${{ secrets.DEPLOY_SSH_PORT }}
script: ./pull.sh # pulls the newest docker image and migrations from github