Makefile 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.19.1
  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. # Building Docker Image
  111. #
  112. # Builds a dockerfile for step by building a linux version of the step-cli and
  113. # then copying the specific binary when building the container.
  114. #
  115. # This ensures the container is as small as possible without having to deal
  116. # with getting access to private repositories inside the container during build
  117. # time.
  118. #########################################
  119. # XXX We put the output for the build in 'output' so we don't mess with how we
  120. # do rule overriding from the base Makefile (if you name it 'build' it messes up
  121. # the wildcarding).
  122. DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
  123. DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(2)
  124. DOCKER_BUILD=$Q docker build -t smallstep/$(1):latest -f docker/$(2) --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(1) .
  125. docker: docker-make docker/Dockerfile.step-ca
  126. $(call DOCKER_BUILD,step-ca,Dockerfile.step-ca)
  127. docker-make:
  128. mkdir -p $(DOCKER_OUTPUT)
  129. $(call DOCKER_MAKE,$(DOCKER_OUTPUT),step-ca)
  130. .PHONY: docker docker-make
  131. #################################################
  132. # Releasing Docker Images
  133. #
  134. # Using the docker build infrastructure, this section is responsible for
  135. # logging into docker hub and pushing the built docker containers up with the
  136. # appropriate tags.
  137. #################################################
  138. DOCKER_TAG=docker tag smallstep/$(1):latest smallstep/$(1):$(2)
  139. DOCKER_PUSH=docker push smallstep/$(1):$(2)
  140. docker-tag:
  141. $(call DOCKER_TAG,step-ca,$(VERSION))
  142. docker-push-tag: docker-tag
  143. $(call DOCKER_PUSH,step-ca,$(VERSION))
  144. docker-push-tag-latest:
  145. $(call DOCKER_PUSH,step-ca,latest)
  146. # Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
  147. # equivalent environment
  148. docker-login:
  149. $Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
  150. .PHONY: docker-login docker-tag docker-push-tag docker-push-tag-latest
  151. #################################################
  152. # Targets for pushing the docker images
  153. #################################################
  154. # For all builds we build the docker container
  155. docker-master: docker
  156. # For all builds with a release candidate tag
  157. docker-release-candidate: docker-master docker-login docker-push-tag
  158. # For all builds with a release tag
  159. docker-release: docker-release-candidate docker-push-tag-latest
  160. .PHONY: docker-master docker-release-candidate docker-release
  161. #########################################
  162. # Debian
  163. #########################################
  164. changelog:
  165. $Q echo "step-certificates ($(VERSION)) unstable; urgency=medium" > debian/changelog
  166. $Q echo >> debian/changelog
  167. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  168. $Q echo >> debian/changelog
  169. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  170. debian: changelog
  171. $Q mkdir -p $(RELEASE); \
  172. OUTPUT=../step-certificates_*.deb; \
  173. rm $$OUTPUT; \
  174. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  175. distclean: clean
  176. .PHONY: changelog debian distclean
  177. #################################################
  178. # Build statically compiled step binary for various operating systems
  179. #################################################
  180. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  181. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/$(BINNAME) $(3)bin/$(CLOUDKMS_BINNAME)
  182. RELEASE=./.travis-releases
  183. binary-linux:
  184. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  185. binary-darwin:
  186. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  187. define BUNDLE
  188. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  189. stepName=step-certificates_$(2); \
  190. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  191. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  192. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  193. newdir=$$TMP/$$stepName; \
  194. mkdir -p $$newdir/bin; \
  195. cp $(BINARY_OUTPUT)$(1)/bin/$(BINNAME) $$newdir/bin/; \
  196. cp $(BINARY_OUTPUT)$(1)/bin/$(CLOUDKMS_BINNAME) $$newdir/bin/; \
  197. cp README.md $$newdir/; \
  198. NEW_BUNDLE=$(RELEASE)/step-certificates_$(2)_$(1)_$(3).tar.gz; \
  199. rm -f $$NEW_BUNDLE; \
  200. tar -zcvf $$NEW_BUNDLE -C $$TMP $$stepName;
  201. endef
  202. bundle-linux: binary-linux
  203. $(call BUNDLE,linux,$(VERSION),amd64)
  204. bundle-darwin: binary-darwin
  205. $(call BUNDLE,darwin,$(VERSION),amd64)
  206. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  207. #################################################
  208. # Targets for creating OS specific artifacts and archives
  209. #################################################
  210. artifacts-linux-tag: bundle-linux debian
  211. artifacts-darwin-tag: bundle-darwin
  212. artifacts-archive-tag:
  213. $Q mkdir -p $(RELEASE)
  214. $Q git archive v$(VERSION) | gzip > $(RELEASE)/step-certificates_$(VERSION).tar.gz
  215. artifacts-tag: artifacts-linux-tag artifacts-darwin-tag artifacts-archive-tag
  216. .PHONY: artifacts-linux-tag artifacts-darwin-tag artifacts-archive-tag artifacts-tag
  217. #################################################
  218. # Targets for creating step artifacts
  219. #################################################
  220. # For all builds that are not tagged
  221. artifacts-master:
  222. # For all builds with a release-candidate (-rc) tag
  223. artifacts-release-candidate: artifacts-tag
  224. # For all builds with a release tag
  225. artifacts-release: artifacts-tag
  226. # This command is called by travis directly *after* a successful build
  227. artifacts: artifacts-$(PUSHTYPE) docker-$(PUSHTYPE)
  228. .PHONY: artifacts-master artifacts-release-candidate artifacts-release artifacts