-
Notifications
You must be signed in to change notification settings - Fork 1
172 lines (145 loc) · 4.72 KB
/
cicd-go.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
name: 'CICD Go'
on:
workflow_call:
inputs:
deploy:
description: 'Should the CD run?'
type: boolean
default: false
service:
description: 'The name of the service.'
type: string
required: true
permissions:
contents: read
pull-requests: read
# We cannot use the global "defaults" syntax. ${{ inputs.service }} will not resolve.
# defaults:
# run:
# working-directory: services/${{ inputs.service }}
env:
# renovate: datasource=docker depName=golang versioning=docker
GO_VERSION: '1.23'
jobs:
lint:
runs-on: ubuntu-22.04
defaults:
run:
working-directory: services/${{ inputs.service }}
steps:
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- uses: actions/checkout@v4
- name: Cache build dependencies
uses: actions/cache@v4
with:
path: /go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/_go.sum') }}
- uses: golangci/golangci-lint-action@v6
with:
# Can this be a bug in golangci-lint? We already defined the working-directory for this step.
working-directory: services/${{ inputs.service }}
args: "--out-format=line-number"
test:
runs-on: ubuntu-22.04
defaults:
run:
working-directory: services/${{ inputs.service }}
steps:
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- uses: actions/checkout@v4
- name: Cache build dependencies
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/_go.sum') }}
- name: Install dependencies
run: go get .
- name: Test
run: go test -v ./...
docker:
needs: [ lint, test ]
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Lowercase github.repository_owner
run: |
echo "REPOSITORY_OWNER_LC=${REPOSITORY_OWNER,,}" >>${GITHUB_ENV}
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
- name: Image name
run: |
echo "IMAGE_NAME=ghcr.io/${{ env.REPOSITORY_OWNER_LC }}/${{ inputs.service }}" >>${GITHUB_ENV}
- uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.IMAGE_NAME }}
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@v3
- uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx
- name: Login to GitHub Container Registry
if: inputs.deploy
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- id: commit
uses: pr-mpt/actions-commit-hash@v3
with:
commit: "${{ github.sha }}"
- name: Build
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.service
platforms: linux/amd64
push: ${{ inputs.deploy }}
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: |
SERVICE=${{ inputs.service }}
VERSION=${{ steps.commit.outputs.short }}
- name: Build Standalone
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.standalone
platforms: linux/amd64
push: false
tags: "standalone"
cache-from: type=local,src=/tmp/.buildx-cache-standalone
cache-to: type=local,dest=/tmp/.buildx-cache-new-standalone
build-args: |
SERVICE=${{ inputs.service }}
VERSION=${{ steps.commit.outputs.short }}
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
rm -rf /tmp/.buildx-cache-standalone
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
mv /tmp/.buildx-cache-new-standalone /tmp/.buildx-cache-standalone
- name: Check manifest
if: inputs.deploy
run: |
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}