-
Notifications
You must be signed in to change notification settings - Fork 0
/
.drone.star
148 lines (117 loc) · 3.77 KB
/
.drone.star
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
def main(ctx):
platforms = ["amd64"]
volumes = [
{
"name": "cache",
"path": "/go",
}
]
workspace = {"path": "/go/src/github.com/${DRONE_REPO}"}
resources = {"requests": {"cpu": 400, "memory": "1Gi"}}
trigger = {"branch": ["main"]}
test_steps = {
"test": append_volumes(test_step(), volumes),
}
pipelines = [
{
"kind": "pipeline",
"type": "kubernetes",
"name": "pre-build",
"resources": resources,
"steps": [append_volumes(license_step(), volumes)],
"trigger": trigger,
"volumes": volumes,
"workspace": workspace,
}
]
for plat in platforms:
pipe = {
"kind": "pipeline",
"type": "kubernetes",
"name": plat,
"platform": {"arch": plat},
"resources": resources,
"steps": [v for v in test_steps.values()],
"trigger": trigger,
"volumes": volumes,
"workspace": workspace,
}
pipe = append_depends_on(pipe, ["pre-build"])
bsnp = build("build", plat, False, False)
bsnp = set_when(bsnp, {"event": ["pull_request"]})
bsnp = append_depends_on(bsnp, test_steps.keys())
bsnp = append_volumes(bsnp, volumes)
pipe["steps"].append(bsnp)
bs = build("publish", plat, False, True)
bs = set_when(bs, {"event": ["push"]})
bs = append_depends_on(bs, test_steps.keys())
bs = append_volumes(bs, volumes)
pipe["steps"].append(bs)
bstp = build("publish-tag", plat, True, True)
bstp = set_when(bstp, {"event": ["tag"]})
bstp = append_volumes(bstp, volumes)
bstp = append_depends_on(bstp, test_steps.keys())
pipe["steps"].append(bstp)
pipelines.append(pipe)
return pipelines
def build(name, arch, tag, publish):
step = {
"name": name,
"image": "plugins/kaniko-ecr",
"pull": "always",
"environment": {
"TAG": "${DRONE_REPO_NAME}",
},
"settings": {
"repo": "${DRONE_REPO_NAME}",
"build_args": ["TAG"],
"tags": [
"git-${DRONE_COMMIT_SHA:0:7}-" + arch,
],
"create_repository": True,
},
}
if tag:
step["settings"]["tags"].append("${DRONE_TAG}-" + arch)
step["environment"]["VERSION"] = "${DRONE_TAG}-" + arch
step["settings"]["build_args"].append("VERSION")
else:
step["settings"]["tags"].append("latest-" + arch)
if publish:
step["settings"]["registry"] = {"from_secret": "ecr_registry"}
step["settings"]["access_key"] = {"from_secret": "ecr_access_key"}
step["settings"]["secret_key"] = {"from_secret": "ecr_secret_key"}
else:
step["settings"]["no_push"] = True
return step
def test_step():
return {
"name": "test",
"image": "golangci/golangci-lint:v1.52",
"pull": "always",
"commands": ["make test"],
}
def license_step():
return {
"name": "license-check",
"image": "public.ecr.aws/kanopy/licensed-go:3.7.3",
"commands": ["licensed cache", "licensed status"],
}
def set_when(step, when_condition):
when_cons = getattr(step, "when", {})
for k, v in when_condition.items():
when_cons[k] = v
step["when"] = when_cons
return step
def append_volumes(step, vols):
volumes = getattr(step, "volumes", [])
for i in vols:
volumes.append(i)
step["volumes"] = volumes
return step
def append_depends_on(step, refs):
deps = getattr(step, "depends_on", [])
for ref in refs:
deps.append(ref)
step["depends_on"] = deps
return step