Makefile 9.9 KB

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