Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Updated 10/17 Alec Cunningham
- # Required resources for kubernetes
- # kubectl create secret generic cloudsql-oauth-credentials --from-file=creds/db-access.json
- # kubectl create secret generic --from-literal=username=root --from-literal=password=root
- # kubectl create configmap settings --from-file=config/configmap.yaml
- # Used for PROJECT_ID
- GCLOUD_PROJECT = container-builder-186003
- # GRC URL
- REPO := gcr.io/$(GCLOUD_PROJECT)/
- # Docker image name
- IMAGE = gke-tut
- # Latest commit SHA for tagging
- COMMIT := $(shell git log -1 --format=%h)
- # The final release artifact docker image
- COMMIT_IMAGE := $(REPO)$(IMAGE):$(COMMIT)
- # GKE cluster name
- CLUSTER := $(IMAGE)
- # Variable of all manifests in all $DIRS
- DIRS = $(wildcard manifests/*)
- DPLY = manifests/deployment.yaml
- OBJS = $(notdir $(DIRS))
- # Holds envsubst'd templates
- TMPL = .generated
- CLEAN = rm -rf $(TMP)
- # Output colors
- GREEN := $(shell tput -Txterm setaf 2)
- WHITE := $(shell tput -Txterm setaf 7)
- YELLOW := $(shell tput -Txterm setaf 3)
- RESET := $(shell tput -Txterm sgr0)
- # Help script
- HELP_FUN = \
- %help; \
- while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
- print "usage: make [target]\n\n"; \
- print "see makefile for additional commands\n\n"; \
- for (sort keys %help) { \
- print "${WHITE}$$_:${RESET}\n"; \
- for (@{$$help{$$_}}) { $$sep = " " x (32 - length $$_->[0]); \
- print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
- }; print "\n"; }
- help: ## Show help (same if no target is specified).
- @perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)
- # Prints value of variable
- print-% : ; @echo $* = $($*)
- .PHONY: all
- all:
- make help
- .PHONY: authenticate
- authenticate: ## Authenticate with GCP and set kubectl context
- @echo You must be signed in for this to work!
- gcloud config set project $(GCLOUD_PROJECT)
- # Get kubectl context
- gcloud container clusters get-credentials $(CLUSTER)
- .PHONY: source
- source: ## Grab env variables
- . .env
- .PHONY: list.manifests
- list.manifests: ##@ List all manifests in $OBJS
- @echo $(OBJS);
- .PHONY: build
- build: ##@docker Build base $IMAGE
- docker build -t $(IMAGE) .
- .PHONY: tag
- tag: ##@docker Tag $IMAGE with 'latest' and $COMMIT
- docker tag $(IMAGE) $(COMMIT_IMAGE)
- docker tag $(IMAGE) $(REPO)$(IMAGE):latest
- push: build tag ##@docker Push release image to GCP registry
- gcloud docker -- push $(COMMIT_IMAGE)
- gcloud docker -- push $(REPO)$(IMAGE):latest
- template: template.reset ##@manifests Replace environment variables
- $(foreach var,$(OBJS),envsubst < manifests/$(var) > $(TMPL)/$(var);)
- echo manifests in $(TMPL)
- .PHONY: template.reset
- template.reset: ##@manifests Reset .generated
- if [ -d $(TMPL) ]; then rm -Rf $(TMPL); fi
- mkdir $(TMPL) && cd $(TMPL) && touch $(OBJS)
- update.deployment: template ##@manifests Only update manifests
- kubectl apply -f $(TMPL)
- $(CLEAN)
- update.image: push ##@kubectl Set deployments image to $COMMIT
- $(foreach var,$(DPLY),kubectl set image deployment/$(var) \
- $(IMAGE)=$(COMMIT_IMAGE);)
- $(CLEAN)
- .PHONY: update.config
- update.config: ##@kubectl Replace ini ConfigMap
- kubectl replace configmap gke-settings --from-file=$(CONFIG)
- .PHONY: delete.deployment
- delete.deployment: ##@kubectl Delete all deployments
- $(foreach var,$(OBJS),kubectl delete deployment $(var);)
- .PHONY: delete.service
- delete.service: ##@kubectl Delete all services
- $(foreach var,$(OBJS),kubectl delete service $(var);)
- delete: delete.deployment delete.service ##@kubectl Delete all resources
- kubectl delete pods --all
Add Comment
Please, Sign In to add comment