|
| 1 | +def main(ctx): |
| 2 | + before = testing(ctx) |
| 3 | + |
| 4 | + stages = [ |
| 5 | + linux(ctx, 'amd64'), |
| 6 | + linux(ctx, 'arm64'), |
| 7 | + linux(ctx, 'arm'), |
| 8 | + windows(ctx, '1909'), |
| 9 | + windows(ctx, '1903'), |
| 10 | + windows(ctx, '1809'), |
| 11 | + ] |
| 12 | + |
| 13 | + after = manifest(ctx) + gitter(ctx) |
| 14 | + |
| 15 | + for b in before: |
| 16 | + for s in stages: |
| 17 | + s['depends_on'].append(b['name']) |
| 18 | + |
| 19 | + for s in stages: |
| 20 | + for a in after: |
| 21 | + a['depends_on'].append(s['name']) |
| 22 | + |
| 23 | + return before + stages + after |
| 24 | + |
| 25 | +def testing(ctx): |
| 26 | + return [{ |
| 27 | + 'kind': 'pipeline', |
| 28 | + 'type': 'docker', |
| 29 | + 'name': 'testing', |
| 30 | + 'platform': { |
| 31 | + 'os': 'linux', |
| 32 | + 'arch': 'amd64', |
| 33 | + }, |
| 34 | + 'steps': [ |
| 35 | + { |
| 36 | + 'name': 'staticcheck', |
| 37 | + 'image': 'golang:1.15', |
| 38 | + 'pull': 'always', |
| 39 | + 'commands': [ |
| 40 | + 'go run honnef.co/go/tools/cmd/staticcheck ./...', |
| 41 | + ], |
| 42 | + 'volumes': [ |
| 43 | + { |
| 44 | + 'name': 'gopath', |
| 45 | + 'path': '/go', |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + 'name': 'lint', |
| 51 | + 'image': 'golang:1.15', |
| 52 | + 'pull': 'always', |
| 53 | + 'commands': [ |
| 54 | + 'go run golang.org/x/lint/golint -set_exit_status ./...', |
| 55 | + ], |
| 56 | + 'volumes': [ |
| 57 | + { |
| 58 | + 'name': 'gopath', |
| 59 | + 'path': '/go', |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + 'name': 'vet', |
| 65 | + 'image': 'golang:1.15', |
| 66 | + 'pull': 'always', |
| 67 | + 'commands': [ |
| 68 | + 'go vet ./...', |
| 69 | + ], |
| 70 | + 'volumes': [ |
| 71 | + { |
| 72 | + 'name': 'gopath', |
| 73 | + 'path': '/go', |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + { |
| 78 | + 'name': 'test', |
| 79 | + 'image': 'golang:1.15', |
| 80 | + 'pull': 'always', |
| 81 | + 'commands': [ |
| 82 | + 'go test -cover ./...', |
| 83 | + ], |
| 84 | + 'volumes': [ |
| 85 | + { |
| 86 | + 'name': 'gopath', |
| 87 | + 'path': '/go', |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + ], |
| 92 | + 'volumes': [ |
| 93 | + { |
| 94 | + 'name': 'gopath', |
| 95 | + 'temp': {}, |
| 96 | + }, |
| 97 | + ], |
| 98 | + 'trigger': { |
| 99 | + 'ref': [ |
| 100 | + 'refs/heads/master', |
| 101 | + 'refs/tags/**', |
| 102 | + 'refs/pull/**', |
| 103 | + ], |
| 104 | + }, |
| 105 | + }] |
| 106 | + |
| 107 | +def linux(ctx, arch): |
| 108 | + docker = { |
| 109 | + 'dockerfile': 'docker/Dockerfile.linux.%s' % (arch), |
| 110 | + 'repo': 'plugins/webhook', |
| 111 | + 'username': { |
| 112 | + 'from_secret': 'docker_username', |
| 113 | + }, |
| 114 | + 'password': { |
| 115 | + 'from_secret': 'docker_password', |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + if ctx.build.event == 'pull_request': |
| 120 | + docker.update({ |
| 121 | + 'dry_run': True, |
| 122 | + 'tags': 'linux-%s' % (arch), |
| 123 | + }) |
| 124 | + else: |
| 125 | + docker.update({ |
| 126 | + 'auto_tag': True, |
| 127 | + 'auto_tag_suffix': 'linux-%s' % (arch), |
| 128 | + }) |
| 129 | + |
| 130 | + if ctx.build.event == 'tag': |
| 131 | + build = [ |
| 132 | + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-webhook ./cmd/drone-webhook' % (ctx.build.ref.replace("refs/tags/v", ""), arch), |
| 133 | + ] |
| 134 | + else: |
| 135 | + build = [ |
| 136 | + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-webhook ./cmd/drone-webhook' % (ctx.build.commit[0:8], arch), |
| 137 | + ] |
| 138 | + |
| 139 | + return { |
| 140 | + 'kind': 'pipeline', |
| 141 | + 'type': 'docker', |
| 142 | + 'name': 'linux-%s' % (arch), |
| 143 | + 'platform': { |
| 144 | + 'os': 'linux', |
| 145 | + 'arch': arch, |
| 146 | + }, |
| 147 | + 'steps': [ |
| 148 | + { |
| 149 | + 'name': 'environment', |
| 150 | + 'image': 'golang:1.15', |
| 151 | + 'pull': 'always', |
| 152 | + 'environment': { |
| 153 | + 'CGO_ENABLED': '0', |
| 154 | + }, |
| 155 | + 'commands': [ |
| 156 | + 'go version', |
| 157 | + 'go env', |
| 158 | + ], |
| 159 | + }, |
| 160 | + { |
| 161 | + 'name': 'build', |
| 162 | + 'image': 'golang:1.15', |
| 163 | + 'pull': 'always', |
| 164 | + 'environment': { |
| 165 | + 'CGO_ENABLED': '0', |
| 166 | + }, |
| 167 | + 'commands': build, |
| 168 | + }, |
| 169 | + { |
| 170 | + 'name': 'executable', |
| 171 | + 'image': 'golang:1.15', |
| 172 | + 'pull': 'always', |
| 173 | + 'commands': [ |
| 174 | + './release/linux/%s/drone-webhook --help' % (arch), |
| 175 | + ], |
| 176 | + }, |
| 177 | + { |
| 178 | + 'name': 'docker', |
| 179 | + 'image': 'plugins/docker', |
| 180 | + 'pull': 'always', |
| 181 | + 'settings': docker, |
| 182 | + }, |
| 183 | + ], |
| 184 | + 'depends_on': [], |
| 185 | + 'trigger': { |
| 186 | + 'ref': [ |
| 187 | + 'refs/heads/master', |
| 188 | + 'refs/tags/**', |
| 189 | + 'refs/pull/**', |
| 190 | + ], |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | +def windows(ctx, version): |
| 195 | + docker = [ |
| 196 | + 'echo $env:PASSWORD | docker login --username $env:USERNAME --password-stdin', |
| 197 | + ] |
| 198 | + |
| 199 | + if ctx.build.event == 'tag': |
| 200 | + build = [ |
| 201 | + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-webhook.exe ./cmd/drone-webhook' % (ctx.build.ref.replace("refs/tags/v", "")), |
| 202 | + ] |
| 203 | + |
| 204 | + docker = docker + [ |
| 205 | + 'docker build --pull -f docker/Dockerfile.windows.%s -t plugins/webhook:%s-windows-%s-amd64 .' % (version, ctx.build.ref.replace("refs/tags/v", ""), version), |
| 206 | + 'docker run --rm plugins/webhook:%s-windows-%s-amd64 --help' % (ctx.build.ref.replace("refs/tags/v", ""), version), |
| 207 | + 'docker push plugins/webhook:%s-windows-%s-amd64' % (ctx.build.ref.replace("refs/tags/v", ""), version), |
| 208 | + ] |
| 209 | + else: |
| 210 | + build = [ |
| 211 | + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-webhook.exe ./cmd/drone-webhook' % (ctx.build.commit[0:8]), |
| 212 | + ] |
| 213 | + |
| 214 | + docker = docker + [ |
| 215 | + 'docker build --pull -f docker/Dockerfile.windows.%s -t plugins/webhook:windows-%s-amd64 .' % (version, version), |
| 216 | + 'docker run --rm plugins/webhook:windows-%s-amd64 --help' % (version), |
| 217 | + 'docker push plugins/webhook:windows-%s-amd64' % (version), |
| 218 | + ] |
| 219 | + |
| 220 | + return { |
| 221 | + 'kind': 'pipeline', |
| 222 | + 'type': 'ssh', |
| 223 | + 'name': 'windows-%s' % (version), |
| 224 | + 'platform': { |
| 225 | + 'os': 'windows', |
| 226 | + }, |
| 227 | + 'server': { |
| 228 | + 'host': { |
| 229 | + 'from_secret': 'windows_server_%s' % (version), |
| 230 | + }, |
| 231 | + 'user': { |
| 232 | + 'from_secret': 'windows_username', |
| 233 | + }, |
| 234 | + 'password': { |
| 235 | + 'from_secret': 'windows_password', |
| 236 | + }, |
| 237 | + }, |
| 238 | + 'steps': [ |
| 239 | + { |
| 240 | + 'name': 'environment', |
| 241 | + 'environment': { |
| 242 | + 'CGO_ENABLED': '0', |
| 243 | + }, |
| 244 | + 'commands': [ |
| 245 | + 'go version', |
| 246 | + 'go env', |
| 247 | + ], |
| 248 | + }, |
| 249 | + { |
| 250 | + 'name': 'build', |
| 251 | + 'environment': { |
| 252 | + 'CGO_ENABLED': '0', |
| 253 | + }, |
| 254 | + 'commands': build, |
| 255 | + }, |
| 256 | + { |
| 257 | + 'name': 'executable', |
| 258 | + 'commands': [ |
| 259 | + './release/windows/amd64/drone-webhook.exe --help', |
| 260 | + ], |
| 261 | + }, |
| 262 | + { |
| 263 | + 'name': 'docker', |
| 264 | + 'environment': { |
| 265 | + 'USERNAME': { |
| 266 | + 'from_secret': 'docker_username', |
| 267 | + }, |
| 268 | + 'PASSWORD': { |
| 269 | + 'from_secret': 'docker_password', |
| 270 | + }, |
| 271 | + }, |
| 272 | + 'commands': docker, |
| 273 | + }, |
| 274 | + ], |
| 275 | + 'depends_on': [], |
| 276 | + 'trigger': { |
| 277 | + 'ref': [ |
| 278 | + 'refs/heads/master', |
| 279 | + 'refs/tags/**', |
| 280 | + ], |
| 281 | + }, |
| 282 | + } |
| 283 | + |
| 284 | +def manifest(ctx): |
| 285 | + return [{ |
| 286 | + 'kind': 'pipeline', |
| 287 | + 'type': 'docker', |
| 288 | + 'name': 'manifest', |
| 289 | + 'steps': [ |
| 290 | + { |
| 291 | + 'name': 'manifest', |
| 292 | + 'image': 'plugins/manifest', |
| 293 | + 'pull': 'always', |
| 294 | + 'settings': { |
| 295 | + 'auto_tag': 'true', |
| 296 | + 'username': { |
| 297 | + 'from_secret': 'docker_username', |
| 298 | + }, |
| 299 | + 'password': { |
| 300 | + 'from_secret': 'docker_password', |
| 301 | + }, |
| 302 | + 'spec': 'docker/manifest.tmpl', |
| 303 | + 'ignore_missing': 'true', |
| 304 | + }, |
| 305 | + }, |
| 306 | + { |
| 307 | + 'name': 'microbadger', |
| 308 | + 'image': 'plugins/webhook', |
| 309 | + 'pull': 'always', |
| 310 | + 'settings': { |
| 311 | + 'urls': { |
| 312 | + 'from_secret': 'microbadger_url', |
| 313 | + }, |
| 314 | + }, |
| 315 | + }, |
| 316 | + ], |
| 317 | + 'depends_on': [], |
| 318 | + 'trigger': { |
| 319 | + 'ref': [ |
| 320 | + 'refs/heads/master', |
| 321 | + 'refs/tags/**', |
| 322 | + ], |
| 323 | + }, |
| 324 | + }] |
| 325 | + |
| 326 | +def gitter(ctx): |
| 327 | + return [{ |
| 328 | + 'kind': 'pipeline', |
| 329 | + 'type': 'docker', |
| 330 | + 'name': 'gitter', |
| 331 | + 'clone': { |
| 332 | + 'disable': True, |
| 333 | + }, |
| 334 | + 'steps': [ |
| 335 | + { |
| 336 | + 'name': 'gitter', |
| 337 | + 'image': 'plugins/gitter', |
| 338 | + 'pull': 'always', |
| 339 | + 'settings': { |
| 340 | + 'webhook': { |
| 341 | + 'from_secret': 'gitter_webhook', |
| 342 | + } |
| 343 | + }, |
| 344 | + }, |
| 345 | + ], |
| 346 | + 'depends_on': [ |
| 347 | + 'manifest', |
| 348 | + ], |
| 349 | + 'trigger': { |
| 350 | + 'ref': [ |
| 351 | + 'refs/heads/master', |
| 352 | + 'refs/tags/**', |
| 353 | + ], |
| 354 | + 'status': [ |
| 355 | + 'failure', |
| 356 | + ], |
| 357 | + }, |
| 358 | + }] |
0 commit comments