feat: docker build and tag from ci
- build in docker in ci so we can see failures. - tag and push to dockerhub from ci so we have more control and visibility on the process. Note, docker workflows run on all branches and no tags by default. You need to opt in to having builds trigger when a git tag is pushed. The filter definition to opt in to tags needs to be present on your job and all dependendent jobs, which is dull. See https://circleci.com/docs/2.0/workflows/#executing-workflows-for-a-git-tag I've recreated the dockerhub autobuild rules we have currently, but it is worthing taking a moment to review them. License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
Oli Evans committed
Mar 4, 2020 at 12:12 UTC
58c240033ec2fd8ee1d0136718ef58ea318b3637
2 files changed
+152
-1
.circleci/config.yml
+72
-1
@@ -28,7 +28,7 @@ default_environment: &default_environment
28
executors:
29
golang:
30
docker:
31
- - image: circleci/golang:1.13
31
+ - image: circleci/golang:1.14
32
working_directory: ~/ipfs/go-ipfs
33
environment:
34
<<: *default_environment
@@ -53,6 +53,12 @@ executors:
53
IPFS_REUSEPORT: false
54
LIBP2P_ALLOW_WEAK_RSA_KEYS: 1
55
E2E_IPFSD_TYPE: go
56
+ dockerizer:
57
+ docker:
58
+ - image: circleci/golang:1.14
59
+ environment:
60
+ IMAGE_NAME: olizilla/test-go-ipfs
61
+ WIP_IMAGE_TAG: wip
62
63
jobs:
64
gobuild:
@@ -295,8 +301,47 @@ jobs:
301
key: v1-ipfs-webui-{{ checksum "~/ipfs/go-ipfs/ipfs-webui/package-lock.json" }}
302
paths:
303
- ~/ipfs/go-ipfs/ipfs-webui/node_modules
304
+ docker-build:
305
+ executor: dockerizer
306
+ steps:
307
+ - checkout
308
+ - setup_remote_docker
309
+ - run:
310
+ name: Build Docker image
311
+ command: |
312
+ IPFS_PLUGINS=""
313
+ if (branch === feat/stabilize-dht) {
314
+ IPFS_PLUGINS="peerlog"
315
+ }
316
+ docker build -t --build-arg IPFS_PLUGINS=$IPFS_PLUGINS $IMAGE_NAME:$WIP_IMAGE_TAG .
317
+ - run:
318
+ name: Archive Docker image
319
+ command: docker save -o go-ipfs-image.tar $IMAGE_NAME
320
+ - persist_to_workspace:
321
+ root: .
322
+ paths:
323
+ - ./go-ipfs-image.tar
324
+ docker-push:
325
+ executor: dockerizer
326
+ steps:
327
+ - checkout
328
+ - setup_remote_docker
329
+ - attach_workspace:
330
+ at: /tmp/workspace
331
+ - run:
332
+ name: Load archived Docker image
333
+ command: docker load -i /tmp/workspace/go-ipfs-image.tar
334
+ - run:
335
+ name: Publish Docker Image to Docker Hub
336
+ command: |
337
+ echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
338
+ ./bin/push-docker-tags.sh "$CIRCLE_BUILD_NUM" "$CIRCLE_SHA1" "$CIRCLE_BRANCH" "$CIRCLE_TAG" dryrun
339
+
340
workflows:
341
version: 2
342
+
343
+ # Runs for all branches, but not on tags
344
+ # see: https://circleci.com/docs/2.0/workflows/#executing-workflows-for-a-git-tag
345
test:
346
jobs:
347
- gobuild
@@ -316,3 +361,29 @@ workflows:
361
- ipfs-webui:
362
requires:
363
- build
364
+ - docker-build
365
+ - docker-push:
366
+ requires:
367
+ - docker-build
368
+ - golint
369
+ - gotest
370
+ - sharness
371
+ - interop
372
+ - go-ipfs-api
373
+ - go-ipfs-http-client
374
+ - ipfs-webui
375
+
376
+ # NOTE: As we need to pass the tag filter to all dependent jobs, this is pulled out into a seperate workflow.
377
+ # see: https://circleci.com/docs/2.0/workflows/#executing-workflows-for-a-git-tag
378
+ docker-on-tag:
379
+ jobs:
380
+ - docker-build:
381
+ filters:
382
+ tags:
383
+ only: /^v[0-9].*|^cluster.+/
384
+ - docker-push:
385
+ requires:
386
+ - docker-build
387
+ filters:
388
+ tags:
389
+ only: /^v[0-9].*|^cluster.+/
bin/push-docker-tags.sh
new
+80
@@ -0,0 +1,80 @@
1
+#!/usr/bin/env bash
2
+
3
+# push-docker-tags.sh
4
+#
5
+# Run from ci to tag images based on the current branch or tag name.
6
+# A bit like dockerhub autobuild config, but somewhere we can version control it.
7
+#
8
+# The `docker-build` job in .circleci/config.yml builds the current commit
9
+# in docker and tags it as ipfs/go-ipfs:wip
10
+#
11
+# Then the `docker-publish` job runs this script to decide what tag, if any,
12
+# to publish to dockerhub.
13
+#
14
+# Usage:
15
+# ./push-docker-tags.sh <build number> <git commit sha1> <git branch name> [git tag name] [dry run]
16
+#
17
+# Example:
18
+# # dry run. pass a 5th arg to have it print what it would do rather than do it.
19
+# ./push-docker-tags.sh 1 testingsha mybranch v1.0 dryrun
20
+#
21
+# # push tag for the master branch
22
+# ./push-docker-tags.sh 1 testingsha master
23
+#
24
+# # push tag for a release tag
25
+# ./push-docker-tags.sh 1 testingsha release v0.5.0
26
+#
27
+# # Surving suggestion in circle ci - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
28
+# ./push-docker-tags.sh "$CIRCLE_BUILD_NUM" "$CIRCLE_SHA1" "$CIRCLE_BRANCH" "$CIRCLE_TAG"
29
+#
30
+set -euo pipefail
31
+
32
+if [[ $# -lt 3 ]] ; then
33
+ echo 'At least 3 args required. Pass 5 args for a dry run.'
34
+ echo 'Usage:'
35
+ echo './push-docker-tags.sh <build number> <git commit sha1> <git branch name> [git tag name] [dry run]'
36
+ exit 1
37
+fi
38
+
39
+BUILD_NUM=$1
40
+GIT_SHA1=$2
41
+GIT_SHA1_SHORT=$(echo "$GIT_SHA1" | cut -c 1-7)
42
+GIT_BRANCH=$3
43
+GIT_TAG=${4:-""}
44
+DRY_RUN=${5:-false}
45
+
46
+WIP_IMAGE_TAG=${WIP_IMAGE_TAG:-wip}
47
+IMAGE_NAME=${IMAGE_NAME:-ipfs/go-ipfs}
48
+
49
+pushTag () {
50
+ local IMAGE_TAG=$1
51
+ if [ "$DRY_RUN" != false ]; then
52
+ echo "DRY RUN! I would have tagged and pushed the following..."
53
+ echo docker tag "$IMAGE_NAME:$WIP_IMAGE_TAG" "$IMAGE_NAME:$IMAGE_TAG"
54
+ echo docker push "$IMAGE_NAME:$IMAGE_TAG"
55
+ else
56
+ docker tag "$IMAGE_NAME:$WIP_IMAGE_TAG" "$IMAGE_NAME:$IMAGE_TAG"
57
+ docker push "$IMAGE_NAME:$IMAGE_TAG"
58
+ fi
59
+}
60
+
61
+if [[ $GIT_TAG =~ ^v[0-9]+ ]]; then
62
+ pushTag "$GIT_TAG"
63
+
64
+elif [[ $GIT_TAG =~ ^cluster ]]; then
65
+ pushTag "$GIT_TAG"
66
+
67
+elif [ "$GIT_BRANCH" = "feat/stabilize-dht" ]; then
68
+ pushTag "bifrost-${BUILD_NUM}-${GIT_SHA1_SHORT}"
69
+
70
+elif [ "$GIT_BRANCH" = "release" ]; then
71
+ pushTag "release"
72
+ pushTag "latest"
73
+
74
+elif [ "$GIT_BRANCH" = "master" ]; then
75
+ pushTag "master"
76
+
77
+else
78
+ echo "Nothing to do. No docker tag defined for branch: $GIT_BRANCH, tag: $GIT_TAG"
79
+
80
+fi