Makefile 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. PKG?=github.com/smallstep/certificates/cmd/step-ca
  2. BINNAME?=step-ca
  3. CLOUDKMS_BINNAME?=step-cloudkms-init
  4. CLOUDKMS_PKG?=github.com/smallstep/certificates/cmd/step-cloudkms-init
  5. # Set V to 1 for verbose output from the Makefile
  6. Q=$(if $V,,@)
  7. PREFIX?=
  8. SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
  9. GOOS_OVERRIDE ?=
  10. OUTPUT_ROOT=output/
  11. all: build test lint
  12. .PHONY: all
  13. #########################################
  14. # Bootstrapping
  15. #########################################
  16. bootstra%:
  17. # Using a released version of golangci-lint to take into account custom replacements in their go.mod
  18. $Q curl -sSfL https://raw.githubusercontent.com/smallstep/cli/master/make/golangci-install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.23.8
  19. .PHONY: bootstra%
  20. #################################################
  21. # Determine the type of `push` and `version`
  22. #################################################
  23. # Version flags to embed in the binaries
  24. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  25. # If we are not in an active git dir then try reading the version from .VERSION.
  26. # .VERSION contains a slug populated by `git archive`.
  27. VERSION := $(or $(VERSION),$(shell ./.version.sh .VERSION))
  28. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  29. NOT_RC := $(shell echo $(VERSION) | grep -v -e -rc)
  30. # If TRAVIS_TAG is set then we know this ref has been tagged.
  31. ifdef TRAVIS_TAG
  32. ifeq ($(NOT_RC),)
  33. PUSHTYPE=release-candidate
  34. else
  35. PUSHTYPE=release
  36. endif
  37. else
  38. PUSHTYPE=master
  39. endif
  40. #########################################
  41. # Build
  42. #########################################
  43. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  44. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  45. GOFLAGS := CGO_ENABLED=0
  46. download:
  47. $Q go mod download
  48. build: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME)
  49. @echo "Build Complete!"
  50. $(PREFIX)bin/$(BINNAME): download $(call rwildcard,*.go)
  51. $Q mkdir -p $(@D)
  52. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  53. $(PREFIX)bin/$(CLOUDKMS_BINNAME): download $(call rwildcard,*.go)
  54. $Q mkdir -p $(@D)
  55. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(LDFLAGS) $(CLOUDKMS_PKG)
  56. # Target to force a build of step-ca without running tests
  57. simple:
  58. $Q mkdir -p $(PREFIX)bin
  59. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  60. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(LDFLAGS) $(CLOUDKMS_PKG)
  61. @echo "Build Complete!"
  62. .PHONY: download 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) $(PREFIX)bin/$(CLOUDKMS_BINNAME)
  92. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  93. $Q install -D $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(CLOUDKMS_BINNAME)
  94. uninstall:
  95. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  96. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(CLOUDKMS_BINNAME)
  97. .PHONY: install uninstall
  98. #########################################
  99. # Clean
  100. #########################################
  101. clean:
  102. ifneq ($(BINNAME),"")
  103. $Q rm -f bin/$(BINNAME)
  104. endif
  105. ifneq ($(CLOUDKMS_BINNAME),"")
  106. $Q rm -f bin/$(CLOUDKMS_BINNAME)
  107. endif
  108. .PHONY: clean
  109. #########################################
  110. # Dev
  111. #########################################
  112. run:
  113. $Q go run cmd/step-ca/main.go $(shell step path)/config/ca.json
  114. .PHONY: run
  115. #########################################
  116. # Building Docker Image
  117. #
  118. # Builds a dockerfile for step by building a linux version of the step-cli and
  119. # then copying the specific binary when building the container.
  120. #
  121. # This ensures the container is as small as possible without having to deal
  122. # with getting access to private repositories inside the container during build
  123. # time.
  124. #########################################
  125. # XXX We put the output for the build in 'output' so we don't mess with how we
  126. # do rule overriding from the base Makefile (if you name it 'build' it messes up
  127. # the wildcarding).
  128. DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
  129. DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(2)
  130. DOCKER_BUILD=$Q docker build -t smallstep/$(1):latest -f docker/$(2) --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(1) .
  131. docker: docker-make docker/Dockerfile.step-ca
  132. $(call DOCKER_BUILD,step-ca,Dockerfile.step-ca)
  133. docker-make:
  134. mkdir -p $(DOCKER_OUTPUT)
  135. $(call DOCKER_MAKE,$(DOCKER_OUTPUT),step-ca)
  136. .PHONY: docker docker-make
  137. #################################################
  138. # Releasing Docker Images
  139. #
  140. # Using the docker build infrastructure, this section is responsible for
  141. # logging into docker hub and pushing the built docker containers up with the
  142. # appropriate tags.
  143. #################################################
  144. DOCKER_TAG=docker tag smallstep/$(1):latest smallstep/$(1):$(2)
  145. DOCKER_PUSH=docker push smallstep/$(1):$(2)
  146. docker-tag:
  147. $(call DOCKER_TAG,step-ca,$(VERSION))
  148. docker-push-tag: docker-tag
  149. $(call DOCKER_PUSH,step-ca,$(VERSION))
  150. docker-push-tag-latest:
  151. $(call DOCKER_PUSH,step-ca,latest)
  152. # Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
  153. # equivalent environment
  154. docker-login:
  155. $Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
  156. .PHONY: docker-login docker-tag docker-push-tag docker-push-tag-latest
  157. #################################################
  158. # Targets for pushing the docker images
  159. #################################################
  160. # For all builds we build the docker container
  161. docker-master: docker
  162. # For all builds with a release candidate tag
  163. docker-release-candidate: docker-master docker-login docker-push-tag
  164. # For all builds with a release tag
  165. docker-release: docker-release-candidate docker-push-tag-latest
  166. .PHONY: docker-master docker-release-candidate docker-release
  167. #########################################
  168. # Debian
  169. #########################################
  170. changelog:
  171. $Q echo "step-certificates ($(VERSION)) unstable; urgency=medium" > debian/changelog
  172. $Q echo >> debian/changelog
  173. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  174. $Q echo >> debian/changelog
  175. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  176. debian: changelog
  177. $Q mkdir -p $(RELEASE); \
  178. OUTPUT=../step-certificates_*.deb; \
  179. rm $$OUTPUT; \
  180. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  181. distclean: clean
  182. .PHONY: changelog debian distclean
  183. #################################################
  184. # Build statically compiled step binary for various operating systems
  185. #################################################
  186. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  187. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/$(BINNAME) $(3)bin/$(CLOUDKMS_BINNAME)
  188. RELEASE=./.travis-releases
  189. binary-linux:
  190. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  191. binary-darwin:
  192. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  193. define BUNDLE
  194. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  195. stepName=step-certificates_$(2); \
  196. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  197. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  198. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  199. newdir=$$TMP/$$stepName; \
  200. mkdir -p $$newdir/bin; \
  201. cp $(BINARY_OUTPUT)$(1)/bin/$(BINNAME) $$newdir/bin/; \
  202. cp $(BINARY_OUTPUT)$(1)/bin/$(CLOUDKMS_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