Makefile 10 KB

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