-
Notifications
You must be signed in to change notification settings - Fork 1
/
.drone.jsonnet
275 lines (263 loc) · 8.46 KB
/
.drone.jsonnet
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// --------- Common ---------
local make_commands_fail_on_error(commands) =
// Not adding `-o pipefail` because it is not supported by sh
['set -euf'] + commands;
// Using arm64 not because it's required but due to CI resourcing - would ideally be "any" (https://github.com/jnohlgard/drone-yaml/tree/arch-any)
local build_arch = 'arm64';
local create_setup_commands(extra_apk_packages=[]) = make_commands_fail_on_error([
'apk add --update-cache bash git go make %s' % std.join(' ', extra_apk_packages),
'git config --global --add safe.directory "$${PWD}"',
]);
// --------- Lint Pipeline ---------
local lint_pipeline = {
kind: 'pipeline',
type: 'docker',
name: 'lint',
platform: {
arch: build_arch,
},
steps: [
{
name: 'lint-code',
image: 'golangci/golangci-lint',
commands: make_commands_fail_on_error([
'make lint-code',
]),
depends_on: [],
},
{
name: 'lint-markdown',
image: 'python:3-alpine',
commands: create_setup_commands() + [
'pip install mdformat-gfm',
'make lint-markdown',
],
depends_on: [],
},
{
name: 'lint-jsonnet',
// Was not able tp use `bitnami/jsonnet` as it has the user set to non-root
image: 'alpine',
commands: create_setup_commands() + [
'GOBIN=/usr/local/bin/ go install github.com/google/go-jsonnet/cmd/jsonnetfmt@latest',
'make lint-jsonnet',
],
depends_on: [],
},
],
};
// --------- Test Pipeline ---------
local test_pipeline = {
kind: 'pipeline',
type: 'docker',
name: 'test',
platform: {
arch: build_arch,
},
steps: [
{
name: 'unit-tests',
image: 'golang:alpine',
commands: create_setup_commands(['gcc', 'libc-dev']) + [
'make test-unit',
],
depends_on: [],
},
{
name: 'system-tests',
image: 'golang:alpine',
commands: create_setup_commands(['bash', 'gcc', 'jq', 'parallel', 'libc-dev']) + [
// XXX: It would be better to `go install` from github but I could not get it to work for drone-cli
|||
git clone --depth=1 --branch master https://github.com/harness/drone-cli.git /tmp/drone-cli
cd /tmp/drone-cli
go install ./...
cd -
|||,
'git submodule update --init --recursive',
'make test-system',
],
environment: {
DRONE_TEST_SERVER: {
from_secret: 'drone_test_server',
},
DRONE_TEST_TOKEN: {
from_secret: 'drone_test_token',
},
DRONE_TEST_REPOSITORY: {
from_secret: 'drone_test_repository',
},
DRONE_TEST_ORGANISATION: {
from_secret: 'drone_test_organisation',
},
},
depends_on: [],
},
{
name: 'compile-coverage-report',
image: 'golang:alpine',
commands: create_setup_commands() + [
'make test-coverage-report',
],
depends_on: ['unit-tests', 'system-tests'],
},
{
// Installing codecov uploader from source to support any runner arch
name: 'codecov-builder',
image: 'node:16-alpine',
commands: make_commands_fail_on_error([
'apk add --update-cache curl git',
'repository_directory="$${PWD}"',
'git clone --depth=1 --branch=main https://github.com/codecov/uploader.git /tmp/uploader',
'cd /tmp/uploader',
'npm install',
'npm run build',
'npx pkg . --targets alpine --output "$${repository_directory}/build/third-party/codecov"',
]),
depends_on: [],
},
{
name: 'publish-coverage',
image: 'alpine',
commands: [
'build/third-party/codecov',
],
environment: {
CODECOV_TOKEN: {
from_secret: 'codecov_token',
},
},
depends_on: ['codecov-builder', 'compile-coverage-report'],
},
],
};
// --------- Build Pipeline ---------
local target_builds = ['linux/arm', 'linux/arm64', 'linux/amd64', 'darwin/amd64', 'darwin/arm64'];
local target_platforms = ['linux/arm', 'linux/arm64', 'linux/amd64'];
local run_only_on_tag_if_not_build_architecture(architecture) = if architecture != build_arch then { when: { event: ['tag'] } } else {};
local binary_build_step_name_prefix = 'build-binary_';
local binary_build_step(target) =
local architecture = std.split(target, '/')[1];
{
name: '%s%s' % [binary_build_step_name_prefix, std.strReplace(target, '/', '-')],
image: 'golang:alpine',
commands: create_setup_commands() + [
'make build TARGET_BUILD=%s' % [target],
],
depends_on: [],
} + run_only_on_tag_if_not_build_architecture(architecture);
local image_build_step_name_prefix = 'build-image_';
local image_build_step(platform) =
local architecture = std.split(platform, '/')[1];
{
name: '%s%s' % [image_build_step_name_prefix, std.strReplace(platform, '/', '-')],
image: 'golang:alpine',
commands: create_setup_commands() + [
'make build-image TARGET_PLATFORM=%s KANIKO_EXECUTOR=build/third-party/kaniko/out/executor' % [platform],
],
depends_on: ['build-kaniko-tool'],
} + run_only_on_tag_if_not_build_architecture(architecture);
local create_image_publish_step(name_postfix, tag_expression) = {
name: 'publish-image-%s' % name_postfix,
image: 'alpine',
commands: create_setup_commands(['docker', 'skopeo']) + [
'echo "$${DOCKER_TOKEN}" | docker login --password-stdin --username "$${DOCKER_USERNAME}"',
'skopeo copy --all dir:build/release/$$(make version)/multiarch docker://colinnolan/drone-secrets-sync:%s' % tag_expression,
],
environment: {
DOCKER_USERNAME: {
from_secret: 'dockerhub_username',
},
DOCKER_TOKEN: {
from_secret: 'dockerhub_token',
},
},
when: {
event: ['tag'],
},
depends_on: ['build-multiarch-image'],
};
local find_build_steps(step_name_prefix, steps) = std.filter(function(name) std.startsWith(name, step_name_prefix), std.map(function(step) step.name, steps));
local build_pipeline = {
kind: 'pipeline',
type: 'docker',
name: 'build',
platform: {
arch: build_arch,
},
steps:
[binary_build_step(target_build) for target_build in target_builds] +
[{
// Unfortunately, we cannot use the official kaniko image for the image builds because Drone CI converts commands
// into a shell script and the kaniko image does not have a shell (https://docs.drone.io/pipeline/docker/syntax/steps/#commands)
name: 'build-kaniko-tool',
image: 'golang:alpine',
commands: create_setup_commands(['bash']) + [
|||
if [[ ! -d build/third-party/kaniko ]]; then
git clone --depth=1 --branch=main https://github.com/GoogleContainerTools/kaniko.git build/third-party/kaniko
fi
|||,
'cd build/third-party/kaniko',
'make out/executor',
],
depends_on: [],
}] +
[image_build_step(target_platform) for target_platform in target_platforms] +
[
{
name: 'build-multiarch-image',
image: 'alpine',
commands: create_setup_commands(['bash', 'jq', 'skopeo']) + [
'make build-image-multiarch TARGET_PLATFORMS="%s"' % [std.join(' ', target_platforms)],
],
depends_on: find_build_steps(image_build_step_name_prefix, build_pipeline.steps),
when: {
event: ['tag'],
},
},
create_image_publish_step('latest', 'latest'),
create_image_publish_step('release', '$$(make version)'),
{
name: 'link-latest',
image: 'alpine',
commands: create_setup_commands() + [
'mkdir -p build/release',
|||
version="$(make version)"
cd build/release
ln -f -s "$${version}" latest
|||,
],
when: {
event: ['tag'],
},
depends_on: [],
},
{
name: 'publish-github-release',
image: 'plugins/github-release:latest',
settings: {
api_key: {
from_secret: 'github_release_token',
},
files: [
'build/release/latest/drone-secrets-sync*',
'CHANGELOG.md',
'README.md',
'LICENCE.txt',
],
},
when: {
event: ['tag'],
},
depends_on: find_build_steps(binary_build_step_name_prefix, build_pipeline.steps) + find_build_steps(image_build_step_name_prefix, build_pipeline.steps) + ['link-latest'],
},
],
};
// --------- Finalise ---------
[
lint_pipeline,
test_pipeline,
build_pipeline,
]