Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Merge #361
Browse files Browse the repository at this point in the history
361: BuildKite r=elliotcourant a=elliotcourant



Co-authored-by: Elliot Courant <[email protected]>
  • Loading branch information
elliotcourant committed Sep 25, 2021
2 parents 54c1a12 + f3b2e78 commit 7be36ef
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 38 deletions.
9 changes: 9 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
steps:
- command:
- make dry
- make deploy
label: "Deployment"
env:
ENVIRONMENT: ${BUILDKITE_GITHUB_DEPLOYMENT_ENVIRONMENT}
agents:
environment: "${BUILDKITE_GITHUB_DEPLOYMENT_ENVIRONMENT}"
6 changes: 6 additions & 0 deletions .github/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

staging:
environment: staging
production_environment: false
required_contexts: []
auto_merge: false
66 changes: 43 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ COVERAGE_TXT = $(PWD)/coverage.txt
ARCH=amd64
OS=$(shell uname -s | tr A-Z a-z)

ifndef ENVIRONMENT
ENVIRONMENT = Staging
endif

ENVIRONMENT ?= $(shell echo $${BUIlDKITE_GITHUB_DEPLOYMENT_ENVIRONMENT:-Local})
ENV_LOWER = $(shell echo $(ENVIRONMENT) | tr A-Z a-z)

GENERATED_YAML=$(PWD)/generated/$(ENV_LOWER)

PATH+=\b:$(GOPATH)/bin:$(LOCAL_BIN):$(NODE_MODULES_BIN)


ifndef POSTGRES_DB
POSTGRES_DB=postgres
endif
Expand All @@ -33,13 +33,23 @@ endif

# Just a shorthand to print some colored text, makes it easier to read and tell the developer what all the makefile is
# doing since its doing a ton.
ifndef BUILDKITE
define infoMsg
@echo "\033[0;32m[$@] $(1)\033[0m"
endef

define warningMsg
@echo "\033[1;33m[$@] $(1)\033[0m"
endef
else
define infoMsg
@echo "INFO [$@] $(1)"
endef

define warningMsg
@echo "WARN [$@] $(1)"
endef
endif

GO_SRC_DIR=$(PWD)/pkg
ALL_GO_FILES=$(wildcard $(GO_SRC_DIR)/**/*.go)
Expand All @@ -52,21 +62,21 @@ include $(PWD)/scripts/*.mk

default: build

dependencies: $(GO_DEPS)
dependencies: $(GO) $(GO_DEPS)
$(call infoMsg,Installing dependencies for monetrs rest-api)
go get ./...
$(GO) get $(GO_SRC_DIR)/...

build: dependencies $(APP_GO_FILES)
build: $(GO) dependencies $(APP_GO_FILES)
$(call infoMsg,Building rest-api binary)
go build -o $(LOCAL_BIN)/monetr $(MONETR_CLI_PACKAGE)
$(GO) build -o $(LOCAL_BIN)/monetr $(MONETR_CLI_PACKAGE)

test: dependencies $(ALL_GO_FILES)
test: $(GO) dependencies $(ALL_GO_FILES)
$(call infoMsg,Running go tests for monetr rest-api)
ifndef CI
go run $(MONETR_CLI_PACKAGE) database migrate -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST)
$(GO) run $(MONETR_CLI_PACKAGE) database migrate -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST)
endif
go test -race -v -coverprofile=$(COVERAGE_TXT) -covermode=atomic ./...
go tool cover -func=$(COVERAGE_TXT)
$(GO) test -race -v -coverprofile=$(COVERAGE_TXT) -covermode=atomic ./...
$(GO) tool cover -func=$(COVERAGE_TXT)

clean:
-rm -rf $(LOCAL_BIN)
Expand Down Expand Up @@ -107,20 +117,26 @@ license:
$(call warningMsg,GITHUB_TOKEN is required to check licenses)
endif

generate: OUTPUT_DIR = $(PWD)/generated/$(ENV_LOWER)
generate: IMAGE_TAG=$(shell git rev-parse HEAD)
generate: VALUES_FILE=$(PWD)/values.$(ENV_LOWER).yaml
generate: $(HELM) $(SPLIT_YAML) $(VALUES_FILE) $(wildcard $(PWD)/templates/*)
$(call infoMsg,Generating Kubernetes yaml using Helm output to: $(OUTPUT_DIR))
VALUES_FILE=$(PWD)/values.$(ENV_LOWER).yaml
VALUES_FILES=$(PWD)/values.yaml $(VALUES_FILE)

TEMPLATE_FILES=$(PWD)/templates/*

$(GENERATED_YAML): $(VALUES_FILES) $(TEMPLATE_FILES)
$(GENERATED_YAML): IMAGE_TAG=$(shell git rev-parse HEAD)
$(GENERATED_YAML): $(HELM) $(SPLIT_YAML)
$(call infoMsg,Generating Kubernetes yaml using Helm output to: $(GENERATED_YAML))
$(call infoMsg,Environment: $(ENVIRONMENT))
$(call infoMsg,Using values file: $(VALUES_FILE))
-rm -rfd $(OUTPUT_DIR) # Clean up the output dir beforehand.
-rm -rf $(GENERATED_YAML)
$(HELM) template rest-api $(PWD) \
--dry-run \
--set image.tag="$(IMAGE_TAG)" \
--set podAnnotations."monetr\.dev/date"="$(BUILD_TIME)" \
--set podAnnotations."monetr\.dev/sha"="$(IMAGE_TAG)" \
--values=values.$(ENV_LOWER).yaml | $(SPLIT_YAML) --outdir $(OUTPUT_DIR) -
--values=values.$(ENV_LOWER).yaml | $(SPLIT_YAML) --outdir $(GENERATED_YAML) -

generate: $(GENERATED_YAML)

ifdef GITLAB_CI
include Makefile.gitlab-ci
Expand Down Expand Up @@ -149,16 +165,20 @@ include Makefile.docker

ifndef CI
include Makefile.tinker

ifeq ($(ENV_LOWER),local)
include Makefile.local
endif

endif

ifndef POSTGRES_PORT
POSTGRES_PORT=5432
endif

migrate:
@go run $(MONETR_CLI_PACKAGE) database migrate -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST) -P $(POSTGRES_PORT) -W $(POSTGRES_PASSWORD)
migrate: $(GO)
@$(GO) run $(MONETR_CLI_PACKAGE) database migrate -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST) -P $(POSTGRES_PORT) -W $(POSTGRES_PASSWORD)

beta-code: migrate
@go run $(MONETR_CLI_PACKAGE) beta new-code -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST) -P $(POSTGRES_PORT) -W $(POSTGRES_PASSWORD)
beta-code: $(GO) migrate
@$(GO) run $(MONETR_CLI_PACKAGE) beta new-code -d $(POSTGRES_DB) -U $(POSTGRES_USER) -H $(POSTGRES_HOST) -P $(POSTGRES_PORT) -W $(POSTGRES_PASSWORD)

126 changes: 111 additions & 15 deletions scripts/Dependencies.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,72 @@ $(LOCAL_BIN):
$(LOCAL_TMP):
@if [ ! -f "$(LOCAL_TMP)" ]; then mkdir -p $(LOCAL_TMP); fi

CURL=$(shell which curl)

# If curl is not installed then we have some basic stuff to install it.
ifeq ("$(strip $(CURL))","")
# If we are on linux.
ifeq ($(OS),linux)
# If the flavor of linux we are on is debian.
ifeq ("$(shell cat /etc/os-release | grep 'ID_LIKE=')","ID_LIKE=debian")
# Install CURL for debian.define
CURL=/usr/bin/curl
$(CURL):
@if [ ! -f "$(CURL)" ]; then $(MAKE) install-$(CURL); fi

install-$(CURL):
$(call warningMsg,curl needs to be installed in order to continue)
apt-get update -y
apt-get install -y curl
else
# If we are not on debian then we need a different script. I'm not sure what it would need to be.
$(error I dont know how to install curl on your OS)
endif
else
# If we are not on linux then I have no idea what I'd need to do to get curl.
$(error Cannot install curl on something other than linux)
endif
endif

GO_BINARY=$(shell which go)

# Check to see if the user has golang installed. If they don't then install it locally just for this project.
ifeq ("$(strip $(GO_BINARY))","")
GOROOT=$(LOCAL_BIN)/go
GOVERSION=go1.17.1
GO_BINARY=$(GOROOT)/bin/go

GO=$(GO_BINARY)
$(GO):
$(call infoMsg,Go needs to be installed)
@if [ ! -f "$(GO)" ]; then $(MAKE) install-$(GO); fi

install-$(GO): GO_URL = "https://golang.org/dl/$(GOVERSION).$(OS)-$(ARCH).tar.gz"
install-$(GO): GO_TAR=$(LOCAL_TMP)/$(GOVERSION).$(OS)-$(ARCH).tar.gz
install-$(GO): $(LOCAL_BIN) $(LOCAL_TMP) $(CURL)
$(call infoMsg,Installing $(GOVERSION) to $(GOROOT))
-rm -rf $(GO_TAR)
curl -L $(GO_URL) --output $(GO_TAR)
tar -xzf $(GO_TAR) -C $(LOCAL_BIN)
rm -rf $(GO_TAR)

GO111MODULE=on
GOROOT=$(LOCAL_BIN)/go
endif

GO=$(GO_BINARY)

LICENSE=$(LOCAL_BIN)/golicense
$(LICENSE):
@if [ ! -f "$(LICENSE)" ]; then $(MAKE) install-$(LICENSE); fi

install-$(LICENSE): LICENSE_REPO = "https://github.com/mitchellh/golicense.git"
install-$(LICENSE): LICENSE_TMP=$(LOCAL_TMP)/golicense
install-$(LICENSE): $(LOCAL_BIN) $(LOCAL_TMP)
install-$(LICENSE): $(LOCAL_BIN) $(LOCAL_TMP) $(GO)
$(call infoMsg,Installing golicense to $(LICENSE))
rm -rf $(LICENSE_TMP) || true
git clone $(LICENSE_REPO) $(LICENSE_TMP)
cd $(LICENSE_TMP) && go build -o $(LICENSE) .
cd $(LICENSE_TMP) && $(GO) build -o $(LICENSE) .
rm -rf $(LICENSE_TMP) || true

HELM_VERSION=3.5.4
Expand All @@ -26,7 +81,7 @@ install-$(HELM): HELM_DIR=$(LOCAL_TMP)/helm
install-$(HELM): HELM_TAR=$(HELM_DIR)/helm.tar.gz
install-$(HELM): HELM_BIN_NAME=$(OS)-$(ARCH)
install-$(HELM): HELM_URL = "https://get.helm.sh/helm-v$(HELM_VERSION)-$(HELM_BIN_NAME).tar.gz"
install-$(HELM): $(LOCAL_BIN) $(LOCAL_TMP)
install-$(HELM): $(LOCAL_BIN) $(LOCAL_TMP) $(CURL)
$(call infoMsg,Installing helm v$(HELM_VERSION) at $(HELM))
-rm -rf $(HELM_DIR)
mkdir -p $(HELM_DIR)
Expand All @@ -42,38 +97,38 @@ $(SPLIT_YAML):

install-$(SPLIT_YAML): SPLIT_YAML_REPO = "https://github.com/mogensen/kubernetes-split-yaml.git"
install-$(SPLIT_YAML): SPLIT_YAML_DIR=$(LOCAL_TMP)/kubernetes-split-yaml
install-$(SPLIT_YAML): $(LOCAL_TMP) $(LOCAL_BIN)
install-$(SPLIT_YAML): $(LOCAL_TMP) $(LOCAL_BIN) $(GO)
$(call infoMsg,Installing kubernetes-split-yaml from $(SPLIT_YAML_REPO) at $(SPLIT_YAML))
-rm -rfd $(SPLIT_YAML_DIR)
-rm -rf $(SPLIT_YAML_DIR)
git clone $(SPLIT_YAML_REPO) $(SPLIT_YAML_DIR)
cd $(SPLIT_YAML_DIR) && go build -o $(SPLIT_YAML) ./...
rm -rfd $(SPLIT_YAML_DIR)
cd $(SPLIT_YAML_DIR) && $(GO) build -o $(SPLIT_YAML) ./...
rm -rf $(SPLIT_YAML_DIR)

SWAG=$(LOCAL_BIN)/swag
$(SWAG):
@if [ ! -f "$(SWAG)" ]; then $(MAKE) install-$(SWAG); fi

install-$(SWAG): SWAG_REPO = "https://github.com/swaggo/swag.git"
install-$(SWAG): SWAG_DIR=$(LOCAL_TMP)/swag
install-$(SWAG): $(LOCAL_BIN) $(LOCAL_TMP)
install-$(SWAG): $(LOCAL_BIN) $(LOCAL_TMP) $(GO)
$(call infoMsg,Installing swag from $(SWAG_REPO) at $(SWAG_DIR))
-rm -rfd $(SWAG_DIR)
-rm -rf $(SWAG_DIR)
git clone $(SWAG_REPO) $(SWAG_DIR)
cd $(SWAG_DIR) && go build -o $(SWAG) github.com/swaggo/swag/cmd/swag
cd $(SWAG_DIR) && $(GO) build -o $(SWAG) github.com/swaggo/swag/cmd/swag
chmod +x $(SWAG)
rm -rfd $(SWAG_DIR)
rm -rf $(SWAG_DIR)

HOSTESS=$(LOCAL_BIN)/hostess
$(HOSTESS):
@if [ ! -f "$(HOSTESS)" ]; then $(MAKE) install-$(HOSTESS); fi

install-$(HOSTESS): HOSTESS_REPO = "https://github.com/cbednarski/hostess.git"
install-$(HOSTESS): HOSTESS_DIR=$(LOCAL_TMP)/hostess
install-$(HOSTESS): $(LOCAL_BIN) $(LOCAL_TMP)
install-$(HOSTESS): $(LOCAL_BIN) $(LOCAL_TMP) $(GO)
$(call infoMsg,Installing hostess to $(HOSTESS))
-rm -rf $(HOSTESS_DIR)
git clone $(HOSTESS_REPO) $(HOSTESS_DIR)
cd $(HOSTESS_DIR) && go build -o $(HOSTESS) .
cd $(HOSTESS_DIR) && $(GO) build -o $(HOSTESS) .
rm -rf $(HOSTESS_DIR)

MKCERT=$(LOCAL_BIN)/mkcert
Expand All @@ -82,9 +137,50 @@ $(MKCERT):

install-$(MKCERT): MKCERT_REPO = "https://github.com/FiloSottile/mkcert.git"
install-$(MKCERT): MKCERT_DIR=$(LOCAL_TMP)/mkcert
install-$(MKCERT): $(LOCAL_BIN) $(LOCAL_TMP)
install-$(MKCERT): $(LOCAL_BIN) $(LOCAL_TMP) $(GO)
$(call infoMsg,Installing mkcert to $(MKCERT))
-rm -rf $(MKCERT_DIR)
git clone $(MKCERT_REPO) $(MKCERT_DIR)
cd $(MKCERT_DIR) && go build -o $(MKCERT) .
cd $(MKCERT_DIR) && $(GO) build -o $(MKCERT) .
rm -rf $(MKCERT_DIR)

GCLOUD=$(LOCAL_BIN)/google-cloud-sdk/bin/gcloud
$(GCLOUD):
@if [ ! -f "$(GCLOUD)" ]; then $(MAKE) install-$(GCLOUD); fi

install-$(GCLOUD): GCLOUD_VERSION=358.0.0
install-$(GCLOUD): GCLOUD_ARCH=$(shell uname -m)
install-$(GCLOUD): GCLOUD_URL = "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-$(GCLOUD_VERSION)-$(OS)-$(GCLOUD_ARCH).tar.gz"
install-$(GCLOUD): GCLOUD_TAR=$(LOCAL_TMP)/gcloud.tar.gz
install-$(GCLOUD): $(LOCAL_BIN) $(LOCAL_TMP) $(CURL)
$(call infoMsg,Installing gcloud SDK v$(GCLOUD_VERSION) to $(GCLOUD))
-rm -rf $(GCLOUD_TAR)
curl -SsL $(GCLOUD_URL) --output $(GCLOUD_TAR)
tar -xzf $(GCLOUD_TAR) -C $(LOCAL_BIN)
rm -rf $(GCLOUD_TAR)

ifneq ($(ENV_LOWER),local)
KUBECTL=$(LOCAL_BIN)/kubectl
$(KUBECTL):
@if [ ! -f "$(KUBECTL)" ]; then $(MAKE) install-$(KUBECTL); fi

install-$(KUBECTL): $(CURL)
install-$(KUBECTL): KUBECTL_STABLE=$(shell curl -L -s https://dl.k8s.io/release/stable.txt)
install-$(KUBECTL): KUBECTL_URL = "https://dl.k8s.io/release/$(KUBECTL_STABLE)/bin/$(OS)/$(ARCH)/kubectl"
install-$(KUBECTL): $(LOCAL_BIN)
$(call infoMsg,Installing kubectl to $(KUBECTL))
curl -L $(KUBECTL_URL) --output $(KUBECTL)
chmod +x $(KUBECTL)
endif


buildkite: $(KUBECTL)
$(KUBECTL) get po

gcloud: $(GCLOUD)
$(GCLOUD) info

PROJECT_ID=acceptance-320515
cloud-build: $(GCLOUD)
cloud-build: NAME=$(shell basename `git rev-parse --show-toplevel`)
cloud-build:; $(GCLOUD) builds submit --project=$(PROJECT_ID) --tag=gcr.io/$(PROJECT_ID)/github.com/monetr/$(NAME):$(RELEASE_REVISION)
23 changes: 23 additions & 0 deletions scripts/Deployment.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ifndef ENVIRONMENT
dry:
$(error ENVIRONMENT is not specified)

deploy:
$(error ENVIRONMENT is not specified)
else

ifeq ($(ENV_LOWER),local)
DEPLOY_NAMESPACE=default
else
DEPLOY_NAMESPACE=monetr-$(ENV_LOWER)
endif

dry: $(KUBECTL) $(GENERATED_YAML)
$(call infoMsg,Dry running deployment of monetr to $(DEPLOY_NAMESPACE))
$(KUBECTL) apply -f $(GENERATED_YAML) -n $(DEPLOY_NAMESPACE) --dry-run=server

deploy: $(KUBECTL) $(GENERATED_YAML)
$(call infoMsg,Deploying monetr to $(DEPLOY_NAMESPACE))
$(KUBECTL) apply -f $(GENERATED_YAML) -n $(DEPLOY_NAMESPACE)
$(KUBECTL) rollout status deploy/rest-api -n $(DEPLOY_NAMESPACE) --timeout=120s
endif
Empty file added scripts/Golang.mk
Empty file.
Empty file added scripts/Helm.mk
Empty file.

0 comments on commit 7be36ef

Please sign in to comment.