4
0

Makefile 8.3 KB

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