-
Notifications
You must be signed in to change notification settings - Fork 269
108 lines (94 loc) · 2.66 KB
/
backend-ci-cd.yml
File metadata and controls
108 lines (94 loc) · 2.66 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
name: Backend CI/CD
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
paths:
- 'backend/**'
- '.github/workflows/backend-ci-cd.yml'
pull_request:
branches:
- main
paths:
- 'backend/**'
- '.github/workflows/backend-ci-cd.yml'
env:
REGISTRY: chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Cache Go modules
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Download dependencies
run: go mod download
- name: Run tests
run: go test -v ./...
- name: Validate go.mod and go.sum
run: |
go mod tidy
go mod verify
if [ -n "$(git status --porcelain)" ]; then
echo "go.mod or go.sum files are not up to date"
git diff
exit 1
fi
build:
needs: test
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
defaults:
run:
working-directory: ./backend
outputs:
version: ${{ steps.get_version.outputs.VERSION }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get version
id: get_version
run: |
VERSION=${GITHUB_REF#refs/tags/}
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Aliyun Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.CT_ALIYUN_USER }}
password: ${{ secrets.CT_ALIYUN_PASS }}
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: .
file: ./build/Dockerfile
push: true
platforms: linux/amd64, linux/arm64
tags: |
${{ env.REGISTRY }}/backend:v${{ steps.get_version.outputs.VERSION }}
${{ env.REGISTRY }}/backend:latest
build-args: |
GOCACHE=/tmp/go-build
GOMODCACHE=/tmp/go-mod
REPO_COMMIT=${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max