Makefile 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. PKG?=github.com/smallstep/certificates/cmd/step-ca
  2. BINNAME?=step-ca
  3. # Set V to 1 for verbose output from the Makefile
  4. Q=$(if $V,,@)
  5. PREFIX?=
  6. SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
  7. GOOS_OVERRIDE ?=
  8. OUTPUT_ROOT=output/
  9. # Set shell to bash for `echo -e`
  10. SHELL := /bin/bash
  11. all: build test lint
  12. .PHONY: all
  13. #########################################
  14. # Bootstrapping
  15. #########################################
  16. bootstra%:
  17. $Q which dep || go get github.com/golang/dep/cmd/dep
  18. $Q dep ensure
  19. $Q GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.17.1
  20. vendor: Gopkg.lock
  21. $Q dep ensure
  22. define VENDOR_BIN_TMPL
  23. vendor/bin/$(notdir $(1)): vendor
  24. $Q go build -o $$@ ./vendor/$(1)
  25. VENDOR_BINS += vendor/bin/$(notdir $(1))
  26. endef
  27. .PHONY: bootstra% vendor
  28. #################################################
  29. # Determine the type of `push` and `version`
  30. #################################################
  31. # Version flags to embed in the binaries
  32. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  33. # If we are not in an active git dir then try reading the version from .VERSION.
  34. # .VERSION contains a slug populated by `git archive`.
  35. VERSION := $(or $(VERSION),$(shell ./.version.sh .VERSION))
  36. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  37. NOT_RC := $(shell echo $(VERSION) | grep -v -e -rc)
  38. # If TRAVIS_TAG is set then we know this ref has been tagged.
  39. ifdef TRAVIS_TAG
  40. ifeq ($(NOT_RC),)
  41. PUSHTYPE=release-candidate
  42. else
  43. PUSHTYPE=release
  44. endif
  45. else
  46. PUSHTYPE=master
  47. endif
  48. #########################################
  49. # Build
  50. #########################################
  51. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  52. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  53. GOFLAGS := CGO_ENABLED=0
  54. build: $(PREFIX)bin/$(BINNAME)
  55. @echo "Build Complete!"
  56. $(PREFIX)bin/$(BINNAME): vendor $(call rwildcard,*.go)
  57. $Q mkdir -p $(@D)
  58. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  59. # Target for building without calling dep ensure
  60. simple:
  61. $Q mkdir -p bin/
  62. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o bin/$(BINNAME) $(LDFLAGS) $(PKG)
  63. @echo "Build Complete!"
  64. .PHONY: build simple
  65. #########################################
  66. # Go generate
  67. #########################################
  68. generate:
  69. $Q go generate ./...
  70. .PHONY: generate
  71. #########################################
  72. # Test
  73. #########################################
  74. test:
  75. $Q $(GOFLAGS) go test -short -coverprofile=coverage.out ./...
  76. vtest:
  77. $(Q)for d in $$(go list ./... | grep -v vendor); do \
  78. echo -e "TESTS FOR: for \033[0;35m$$d\033[0m"; \
  79. $(GOFLAGS) go test -v -bench=. -run=. -short -coverprofile=vcoverage.out $$d; \
  80. out=$$?; \
  81. if [[ $$out -ne 0 ]]; then ret=$$out; fi;\
  82. rm -f profile.coverage.out; \
  83. done; exit $$ret;
  84. .PHONY: test vtest
  85. integrate: integration
  86. integration: bin/$(BINNAME)
  87. $Q $(GOFLAGS) go test -tags=integration ./integration/...
  88. .PHONY: integrate integration
  89. #########################################
  90. # Linting
  91. #########################################
  92. fmt:
  93. $Q gofmt -l -w $(SRC)
  94. lint:
  95. $Q LOG_LEVEL=error golangci-lint run
  96. .PHONY: $(LINTERS) lint fmt
  97. #########################################
  98. # Install
  99. #########################################
  100. INSTALL_PREFIX?=/usr/
  101. install: $(PREFIX)bin/$(BINNAME)
  102. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  103. uninstall:
  104. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  105. .PHONY: install uninstall
  106. #########################################
  107. # Clean
  108. #########################################
  109. clean:
  110. @echo "You will need to run 'make bootstrap' or 'dep ensure' directly to re-download any dependencies."
  111. $Q rm -rf vendor
  112. ifneq ($(BINNAME),"")
  113. $Q rm -f bin/$(BINNAME)
  114. endif
  115. .PHONY: clean
  116. #########################################
  117. # Building Docker Image
  118. #
  119. # Builds a dockerfile for step by building a linux version of the step-cli and
  120. # then copying the specific binary when building the container.
  121. #
  122. # This ensures the container is as small as possible without having to deal
  123. # with getting access to private repositories inside the container during build
  124. # time.
  125. #########################################
  126. # XXX We put the output for the build in 'output' so we don't mess with how we
  127. # do rule overriding from the base Makefile (if you name it 'build' it messes up
  128. # the wildcarding).
  129. DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
  130. DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(2)
  131. DOCKER_BUILD=$Q docker build -t smallstep/$(1):latest -f docker/$(2) --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(1) .
  132. docker: docker-make docker/Dockerfile.step-ca
  133. $(call DOCKER_BUILD,step-ca,Dockerfile.step-ca)
  134. docker-make:
  135. mkdir -p $(DOCKER_OUTPUT)
  136. $(call DOCKER_MAKE,$(DOCKER_OUTPUT),step-ca)
  137. .PHONY: docker docker-make
  138. #################################################
  139. # Releasing Docker Images
  140. #
  141. # Using the docker build infrastructure, this section is responsible for
  142. # logging into docker hub and pushing the built docker containers up with the
  143. # appropriate tags.
  144. #################################################
  145. DOCKER_TAG=docker tag smallstep/$(1):latest smallstep/$(1):$(2)
  146. DOCKER_PUSH=docker push smallstep/$(1):$(2)
  147. docker-tag:
  148. $(call DOCKER_TAG,step-ca,$(VERSION))
  149. docker-push-tag: docker-tag
  150. $(call DOCKER_PUSH,step-ca,$(VERSION))
  151. docker-push-tag-latest:
  152. $(call DOCKER_PUSH,step-ca,latest)
  153. # Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
  154. # equivalent environment
  155. docker-login:
  156. $Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
  157. .PHONY: docker-login docker-tag docker-push-tag docker-push-tag-latest
  158. #################################################
  159. # Targets for pushing the docker images
  160. #################################################
  161. # For all builds we build the docker container
  162. docker-master: docker
  163. # For all builds with a release candidate tag
  164. docker-release-candidate: docker-master docker-login docker-push-tag
  165. # For all builds with a release tag
  166. docker-release: docker-release-candidate docker-push-tag-latest
  167. .PHONY: docker-master docker-release-candidate docker-release
  168. #########################################
  169. # Debian
  170. #########################################
  171. changelog:
  172. $Q echo "step-certificates ($(VERSION)) unstable; urgency=medium" > debian/changelog
  173. $Q echo >> debian/changelog
  174. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  175. $Q echo >> debian/changelog
  176. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  177. debian: changelog
  178. $Q mkdir -p $(RELEASE); \
  179. OUTPUT=../step-certificates_*.deb; \
  180. rm $$OUTPUT; \
  181. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  182. distclean: clean
  183. .PHONY: changelog debian distclean
  184. #################################################
  185. # Build statically compiled step binary for various operating systems
  186. #################################################
  187. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  188. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/$(BINNAME)
  189. RELEASE=./.travis-releases
  190. binary-linux:
  191. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  192. binary-darwin:
  193. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  194. define BUNDLE
  195. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  196. stepName=step-certificates_$(2); \
  197. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  198. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  199. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  200. newdir=$$TMP/$$stepName; \
  201. mkdir -p $$newdir/bin; \
  202. cp $(BINARY_OUTPUT)$(1)/bin/$(BINNAME) $$newdir/bin/; \
  203. cp README.md $$newdir/; \
  204. NEW_BUNDLE=$(RELEASE)/step-certificates_$(2)_$(1)_$(3).tar.gz; \
  205. rm -f $$NEW_BUNDLE; \
  206. tar -zcvf $$NEW_BUNDLE -C $$TMP $$stepName;
  207. endef
  208. bundle-linux: binary-linux
  209. $(call BUNDLE,linux,$(VERSION),amd64)
  210. bundle-darwin: binary-darwin
  211. $(call BUNDLE,darwin,$(VERSION),amd64)
  212. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  213. #################################################
  214. # Targets for creating OS specific artifacts and archives
  215. #################################################
  216. artifacts-linux-tag: bundle-linux debian
  217. artifacts-darwin-tag: bundle-darwin
  218. artifacts-archive-tag:
  219. $Q mkdir -p $(RELEASE)
  220. $Q git archive v$(VERSION) | gzip > $(RELEASE)/step-certificates_$(VERSION).tar.gz
  221. artifacts-tag: artifacts-linux-tag artifacts-darwin-tag artifacts-archive-tag
  222. .PHONY: artifacts-linux-tag artifacts-darwin-tag artifacts-archive-tag artifacts-tag
  223. #################################################
  224. # Targets for creating step artifacts
  225. #################################################
  226. # For all builds that are not tagged
  227. artifacts-master:
  228. # For all builds with a release-candidate (-rc) tag
  229. artifacts-release-candidate: artifacts-tag
  230. # For all builds with a release tag
  231. artifacts-release: artifacts-tag
  232. # This command is called by travis directly *after* a successful build
  233. artifacts: artifacts-$(PUSHTYPE) docker-$(PUSHTYPE)
  234. .PHONY: artifacts-master artifacts-release-candidate artifacts-release artifacts