4
0

Makefile 8.4 KB

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