Makefile 9.9 KB

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