Makefile 8.6 KB

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