Skip to content

Commit 705b4f3

Browse files
authored
Merge pull request #102 from commitdev/GH-101-pipeline-for-dockerhub
Added CircleCI pipeline to test on branch and build and push on master.
2 parents 213e49b + 0c2b1d7 commit 705b4f3

File tree

5 files changed

+188
-3
lines changed

5 files changed

+188
-3
lines changed

.circleci/config.yml

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
version: 2.1
3+
orbs:
4+
slack-message: commitdev/slack-message@0.0.3
5+
version-tag: commitdev/version-tag@0.0.3
6+
7+
variables:
8+
- &workspace /home/circleci/project
9+
- &build-image circleci/golang:1.13.8
10+
11+
aliases:
12+
# Shallow Clone - this allows us to cut the 2 minute repo clone down to about 10 seconds for repos with 50,000 commits+
13+
- &checkout-shallow
14+
name: Checkout (Shallow)
15+
command: |
16+
#!/bin/sh
17+
set -e
18+
19+
# Workaround old docker images with incorrect $HOME
20+
# check https://github.com/docker/docker/issues/2968 for details
21+
if [ "${HOME}" = "/" ]
22+
then
23+
export HOME=$(getent passwd $(id -un) | cut -d: -f6)
24+
fi
25+
26+
mkdir -p ~/.ssh
27+
28+
echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
29+
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==' >> ~/.ssh/known_hosts
30+
31+
(umask 077; touch ~/.ssh/id_rsa)
32+
chmod 0600 ~/.ssh/id_rsa
33+
(cat \<<EOF > ~/.ssh/id_rsa
34+
$CHECKOUT_KEY
35+
EOF
36+
)
37+
38+
# use git+ssh instead of https
39+
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
40+
41+
if [ -e /home/circleci/project/.git ]
42+
then
43+
cd /home/circleci/project
44+
git remote set-url origin "$CIRCLE_REPOSITORY_URL" || true
45+
else
46+
mkdir -p /home/circleci/project
47+
cd /home/circleci/project
48+
git clone --depth=1 "$CIRCLE_REPOSITORY_URL" .
49+
fi
50+
51+
if [ -n "$CIRCLE_TAG" ]
52+
then
53+
git fetch --depth=10 --force origin "refs/tags/${CIRCLE_TAG}"
54+
elif [[ "$CIRCLE_BRANCH" =~ ^pull\/* ]]
55+
then
56+
# For PR from Fork
57+
git fetch --depth=10 --force origin "$CIRCLE_BRANCH/head:remotes/origin/$CIRCLE_BRANCH"
58+
else
59+
git fetch --depth=10 --force origin "$CIRCLE_BRANCH:remotes/origin/$CIRCLE_BRANCH"
60+
fi
61+
62+
if [ -n "$CIRCLE_TAG" ]
63+
then
64+
git reset --hard "$CIRCLE_SHA1"
65+
git checkout -q "$CIRCLE_TAG"
66+
elif [ -n "$CIRCLE_BRANCH" ]
67+
then
68+
git reset --hard "$CIRCLE_SHA1"
69+
git checkout -q -B "$CIRCLE_BRANCH"
70+
fi
71+
72+
git reset --hard "$CIRCLE_SHA1"
73+
pwd
74+
75+
jobs:
76+
checkout_code:
77+
docker:
78+
- image: *build-image
79+
steps:
80+
- run: *checkout-shallow
81+
- persist_to_workspace:
82+
root: /home/circleci/project
83+
paths:
84+
- .
85+
86+
unit_test:
87+
docker:
88+
- image: *build-image
89+
working_directory: *workspace
90+
steps: # steps that comprise the `build` job
91+
- attach_workspace:
92+
at: *workspace
93+
94+
- restore_cache: # restores saved cache if no changes are detected since last run
95+
keys:
96+
- v1-pkg-cache-{{ checksum "go.sum" }}
97+
- v1-pkg-cache-
98+
99+
- run:
100+
name: Run unit tests
101+
# store the results of our tests in the $TEST_RESULTS directory
102+
command: |
103+
go get -u github.com/jstemmer/go-junit-report
104+
mkdir -p test-reports
105+
PACKAGE_NAMES=$(go list ./... | circleci tests split --split-by=timings --timings-type=classname)
106+
echo "Tests: $PACKAGE_NAMES"
107+
go test -v $PACKAGE_NAMES | go-junit-report > test-reports/junit.xml
108+
109+
- save_cache: # Store cache in the /go/pkg directory
110+
key: v1-pkg-cache-{{ checksum "go.sum" }}
111+
paths:
112+
- "/go/pkg"
113+
114+
- store_test_results:
115+
path: test-reports
116+
117+
- store_artifacts:
118+
path: test-reports
119+
120+
# Requires the SLACK_WEBHOOK
121+
# - slack/notify-on-failure
122+
123+
build_and_push:
124+
machine:
125+
image: circleci/classic:latest
126+
# docker_layer_caching: true # only for performance plan circleci accounts
127+
steps:
128+
- attach_workspace:
129+
at: *workspace
130+
- run: *checkout-shallow
131+
- version-tag/create
132+
- run:
133+
name: Update Version command
134+
command: |
135+
./updateVersion.sh "$VERSION_TAG"
136+
- run:
137+
name: Build Docker image
138+
command: |
139+
make ci-docker-build
140+
- run:
141+
name: Push to Docker Hub
142+
command: |
143+
make ci-docker-push
144+
145+
146+
workflows:
147+
version: 2
148+
# The main workflow. Check out the code, build it, push it, deploy to staging, test, deploy to production
149+
build_test_and_deploy:
150+
jobs:
151+
- checkout_code
152+
153+
- unit_test:
154+
requires:
155+
- checkout_code
156+
157+
- build_and_push:
158+
requires:
159+
- unit_test
160+
filters:
161+
branches:
162+
only: # only branches matching the below regex filters will run
163+
- /^master$/
164+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ packrd
44
.history/
55
tmp
66
.vscode
7+
example/
8+
test-reports/
9+
.circleci/config-compiled.yml

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
VERSION:= 0.0.1
2-
31
check:
42
go test ./...
53

@@ -20,3 +18,11 @@ build:
2018
# Installs the CLI int your GOPATH
2119
install-go:
2220
go build -o ${GOPATH}/bin/commit0
21+
22+
# CI Commands used on CircleCI
23+
ci-docker-build:
24+
docker build . -t commitdev/commit0:${VERSION_TAG} -t commitdev/commit0:latest
25+
26+
ci-docker-push:
27+
echo "${DOCKERHUB_PASS}" | docker login -u commitdev --password-stdin
28+
docker push commitdev/commit0:${VERSION_TAG}

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
1414
Use: "version",
1515
Short: "Print the version number of commit0",
1616
Run: func(cmd *cobra.Command, args []string) {
17-
fmt.Println("v0.0.1")
17+
fmt.Println("v0.0.0") // Updated via updateVersion.sh
1818
},
1919
}

updateVersion.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
VERSION=$1
4+
5+
if [ -z "$VERSION" ]; then
6+
echo "Usage: ./updateVersion.sh <version>"
7+
exit 1
8+
fi
9+
10+
sed "s/\t\tfmt.Println(\"v0.0.0\")/\t\tfmt.Println(\"${VERSION}\")/g" cmd/version.go > cmd/version.go.update
11+
12+
mv cmd/version.go.update cmd/version.go

0 commit comments

Comments
 (0)