Skip to content

Commit

Permalink
adding docker automation (#184)
Browse files Browse the repository at this point in the history
* adding docker automation

* updated changelog

* bump travis go version
  • Loading branch information
kcajmagic authored Mar 10, 2020
1 parent 267de6c commit d445e08
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 61 deletions.
17 changes: 15 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.13.x
- 1.14.x
- tip

os:
Expand All @@ -19,6 +19,9 @@ script:
- make style codecov

jobs:
fast_finish: true
allow_failures:
- go: tip
include:
# - stage: integration
# name: "Integration Tests"
Expand Down Expand Up @@ -52,4 +55,14 @@ jobs:
api_key: "$GH_TOKEN"
file_glob: true
file: ./.ignore/*
skip_cleanup: true
skip_cleanup: true
- stage: docker-release
name: "Make Docker Release"
if: branch != master
script: skip
deploy:
- provider: script
script: bash deploy/docker_push
on:
all_branches: true
tags: true
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- adding docker automation [#184](https://github.com/xmidt-org/caduceus/pull/184)

## [v0.2.3]
- updated release pipeline to use travis [#174](https://github.com/xmidt-org/caduceus/pull/174)
Expand Down
36 changes: 19 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ GOFMT ?= $(GO)fmt
APP := caduceus
DOCKER_ORG := xmidt

PROGVER = $(shell git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
VERSION ?= $(shell git describe --tag --always --dirty)
PROGVER ?= $(shell git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
BUILDTIME = $(shell date -u '+%Y-%m-%d %H:%M:%S')
GITCOMMIT = $(shell git rev-parse --short HEAD)
GOBUILDFLAGS = -a -ldflags "-w -s -X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)" -o $(APP)

.PHONY: go-mod-vendor
go-mod-vendor:
GO111MODULE=on $(GO) mod vendor
.PHONY: vendor
vendor:
$(GO) mod vendor

.PHONY: build
build: go-mod-vendor
$(GO) build -o $(APP) -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)"
build: vendor
CGO_ENABLED=0 $(GO) build $(GOBUILDFLAGS)

.PHONY: version
version:
Expand All @@ -36,38 +38,38 @@ update-version:


.PHONY: install
install: go-mod-vendor
$(GO) install -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)"
install: vendor
$(GO) install -ldflags "-w -s -X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"

.PHONY: release-artifacts
release-artifacts: go-mod-vendor
release-artifacts: vendor
mkdir -p ./.ignore
GOOS=darwin GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).darwin-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)"
GOOS=linux GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).linux-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)"
GOOS=darwin GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).darwin-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"
GOOS=linux GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).linux-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"

.PHONY: docker
docker:
docker build \
--build-arg VERSION=$(PROGVER) \
--build-arg VERSION=$(VERSION) \
--build-arg GITCOMMIT=$(GITCOMMIT) \
--build-arg BUILDTIME='$(BUILDTIME)' \
-f ./deploy/Dockerfile -t $(DOCKER_ORG)/$(APP):$(PROGVER) .

.PHONY: local-docker
local-docker:
docker build \
--build-arg VERSION=$(PROGVER)+local \
--build-arg VERSION=$(VERSION) \
--build-arg GITCOMMIT=$(GITCOMMIT) \
--build-arg BUILDTIME='$(BUILDTIME)' \
-f ./deploy/Dockerfile.local -t $(DOCKER_ORG)/$(APP):local .
-f ./deploy/Dockerfile -t $(DOCKER_ORG)/$(APP):local .

.PHONY: style
style:
style: vendor
! $(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'

.PHONY: test
test: go-mod-vendor
GO111MODULE=on $(GO) test -v -race -coverprofile=cover.out ./...
test: vendor
$(GO) test -v -race -coverprofile=cover.out ./...

.PHONY: test-cover
test-cover: test
Expand Down
54 changes: 38 additions & 16 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
FROM golang:alpine as builder
FROM docker.io/library/golang:1.14-alpine as builder

MAINTAINER Jack Murdock <[email protected]>

WORKDIR /go/src/github.com/xmidt-org/caduceus
ARG VERSION=undefined
ARG GITCOMMIT=undefined
ARG BUILDTIME=undefined

RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh
ARG VERSION=unknown
ARG GITCOMMIT=unknown
ARG BUILDTIME=unknown

ADD https://github.com/geofffranks/spruce/releases/download/v1.25.2/spruce-linux-amd64 /usr/local/bin/spruce
RUN chmod +x /usr/local/bin/spruce

RUN apk add --no-cache --no-progress \
ca-certificates \
make \
git \
openssh \
gcc \
libc-dev \
upx

COPY . .
RUN GO111MODULE=on go build -ldflags "-X 'main.BuildTime=${BUILDTIME}' -X main.GitCommit=${GITCOMMIT} -X main.Version=${VERSION}" -o caduceus_linux_amd64
RUN make build

FROM alpine:latest

COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/src/github.com/xmidt-org/caduceus/caduceus.yaml /caduceus.yaml
COPY --from=builder /go/src/github.com/xmidt-org/caduceus/caduceus /caduceus
COPY --from=builder /go/src/github.com/xmidt-org/caduceus/deploy/Dockerfile /go/src/github.com/xmidt-org/caduceus/NOTICE /go/src/github.com/xmidt-org/caduceus/LICENSE /go/src/github.com/xmidt-org/caduceus/CHANGELOG.md /
COPY --from=builder /go/src/github.com/xmidt-org/caduceus/deploy/packaging/entrypoint.sh /entrypoint.sh
COPY --from=builder /go/src/github.com/xmidt-org/caduceus/deploy/packaging/caduceus_spruce.yaml /tmp/caduceus_spruce.yaml
COPY --from=builder /usr/local/bin/spruce /spruce

RUN mkdir /etc/caduceus/ && touch /etc/caduceus/caduceus.yaml && chmod 666 /etc/caduceus/caduceus.yaml

FROM alpine
USER nobody

RUN apk --no-cache add ca-certificates
RUN mkdir -p /etc/caduceus
VOLUME /etc/caduceus
ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 8080
EXPOSE 6000
EXPOSE 6001
EXPOSE 6002
EXPOSE 6003

COPY --from=builder /go/src/github.com/xmidt-org/caduceus/caduceus_linux_amd64 /
COPY caduceus.yaml /
COPY deploy/Dockerfile NOTICE LICENSE CHANGELOG.md /
ENTRYPOINT ["/caduceus_linux_amd64"]
CMD ["/caduceus"]
26 changes: 0 additions & 26 deletions deploy/Dockerfile.local

This file was deleted.

16 changes: 16 additions & 0 deletions deploy/docker_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

# upload docker as $TRAVIS_TAG or latest
echo "$DOCKER_TOKEN" | docker login -u "$DOCKER_USERNAME" --password-stdin

LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
VERSION_TAG=$(echo "$TRAVIS_TAG" | sed 's/v\(.*\)/\1/')

docker build --build-arg VERSION="$VERSION_TAG" --build-arg GITCOMMIT="`git rev-parse --short HEAD`" --build-arg BUILDTIME="`date -u '+%Y-%m-%d %H:%M:%S'`" -f ./deploy/Dockerfile -t xmidt/caduceus:$VERSION_TAG .

docker push xmidt/caduceus:$VERSION_TAG

if [[ "$VERSION_TAG" == "$LATEST_TAG" ]]; then
docker tag xmidt/caduceus:$VERSION_TAG xmidt/caduceus:latest
docker push xmidt/caduceus:latest
fi
Loading

0 comments on commit d445e08

Please sign in to comment.