-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
144 lines (116 loc) · 4.88 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Makefile to bootstrap local kubernetes
## Minikube
CERTS_PATH=./src/certificates
K8S_PATH=./src/.k8s
.PHONY: init
init: start
minikube addons enable metrics-server
sh $(CERTS_PATH)/generate-certs.sh
kubectl -n kube-system create secret tls mkcert --key $(CERTS_PATH)/key.pem --cert $(CERTS_PATH)/cert.pem
# Custom cert(format is "namespace/secret") is: "kube-system/mkcert"
minikube addons configure ingress
minikube addons disable ingress
minikube addons enable ingress
minikube addons configure registry-creds
minikube addons enable registry-creds
.PHONY: start
start:
minikube start --cpus 2 --memory 4g --driver=virtualbox
kubectl cluster-info
sh $(K8S_PATH)/replace-ip.sh
.PHONY: tunnel
tunnel:
# Enter system user password (if asked for)
minikube tunnel
.PHONY: dashboard
dashboard:
minikube dashboard
## Kubernetes
ENVS = production staging
TARGET_ENV = default
DB_CONFIG_PATH=./src/.k8s/database
APP_CONFIG_PATH=./src/.k8s/app
CONTROLLER_CONFIG_PATH=./src/.k8s/controller
.PHONY: $(ENVS)
$(ENVS): TARGET_ENV=$(MAKECMDGOALS)
$(ENVS): namespace database app
.PHONY: namespace
namespace:
@echo "Setting up $@ for $(TARGET_ENV) environment..."
kubectl apply -f ./src/.k8s/namespaces/$(TARGET_ENV).yaml
.PHONY: database
database:
@echo "Setting up $@ for $(TARGET_ENV) environment..."
kubectl create secret generic database-secret --from-env-file=.env.db --namespace=$(TARGET_ENV)
kubectl apply -f $(DB_CONFIG_PATH)/db.pvc.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(DB_CONFIG_PATH)/db.config.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(DB_CONFIG_PATH)/db.deployment.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(DB_CONFIG_PATH)/db.service.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(CONTROLLER_CONFIG_PATH)/db.load-balancer.yaml --namespace=$(TARGET_ENV)
.PHONY: app
app:
@echo "Setting up $@ for $(TARGET_ENV) environment..."
kubectl create secret generic app-secret --from-env-file=.env.app --namespace=$(TARGET_ENV)
kubectl apply -f $(APP_CONFIG_PATH)/app.config.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(APP_CONFIG_PATH)/app.deployment.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(APP_CONFIG_PATH)/app.service.yaml --namespace=$(TARGET_ENV)
kubectl apply -f $(CONTROLLER_CONFIG_PATH)/$(TARGET_ENV).ingress.yaml --namespace=$(TARGET_ENV)
### Deploy
.PHONY: deploy-app
deploy-app:
@echo "Running $@ for $(TARGET_ENV) environment..."
kubectl set image deployment/devops-app -n=$(TARGET_ENV) devops-app=ghcr.io/kelzenberg/devops-app:${TAG}
kubectl rollout status -n=$(TARGET_ENV) --timeout=15m deployment/devops-app
.PHONY: deploy-app-staging
deploy-app-staging: TAG=latest
deploy-app-staging: TARGET_ENV=staging
deploy-app-staging: deploy-app
.PHONY: deploy-app-production
deploy-app-production: TAG=latest
deploy-app-production: TARGET_ENV=production
deploy-app-production: deploy-app
### Clean
.PHONY: clean
clean:
@echo "Deleting every resource in $(TARGET_ENV) environment..."
kubectl delete deployments --namespace=$(TARGET_ENV) --all
kubectl delete services --namespace=$(TARGET_ENV) --all
kubectl delete pods --namespace=$(TARGET_ENV) --all
kubectl delete daemonset --namespace=$(TARGET_ENV) --all
kubectl delete pvc --namespace=$(TARGET_ENV) --all
kubectl delete ingress --namespace=$(TARGET_ENV) --all
kubectl delete configmaps --namespace=$(TARGET_ENV) --all
kubectl delete secrets --namespace=$(TARGET_ENV) --all
.PHONY: clean-staging
clean-staging: TARGET_ENV=staging
clean-staging: clean
.PHONY: clean-production
clean-production: TARGET_ENV=production
clean-production: clean
.PHONY: clean-all
clean-all: clean-staging clean-production
kubectl delete secret -n kube-system mkcert
kubectl delete namespaces $(ENVS)
## GitHub Self-hosted Runner
RUNNER_TOKEN = $(error Please provide the generated GitHub token for the runner, e.g "make runner-install RUNNER_TOKEN=foo")
RUNNER_PATH=./src/.github
RUNNER_VERSION=2.302.1
RUNNER_PACKAGE=actions-runner-osx-x64-$(RUNNER_VERSION).tar.gz
RUNNER_HASH=cc061fc4ae62afcbfab1e18f1b2a7fc283295ca3459345f31a719d36480a8361
.PHONY: runner-install
runner-install:
@echo "Installing GitHub Actions self-hosted runner..."
mkdir -p $(RUNNER_PATH)/runner
[ -f $(RUNNER_PATH)/$(RUNNER_PACKAGE) ] && echo "Actions-runner package exists. Skipping download." || curl -o $(RUNNER_PATH)/$(RUNNER_PACKAGE) -L https://github.com/actions/runner/releases/download/v$(RUNNER_VERSION)/$(RUNNER_PACKAGE)
echo "$(RUNNER_HASH) $(RUNNER_PATH)/$(RUNNER_PACKAGE)" | shasum -a 256 -c
tar xzf $(RUNNER_PATH)/$(RUNNER_PACKAGE) -C $(RUNNER_PATH)/runner
@echo "Configuring self-hosted runner with supplied token..."
$(RUNNER_PATH)/runner/config.sh --url https://github.com/kelzenberg/devops-app --token $(RUNNER_TOKEN)
.PHONY: runner-remove
runner-remove:
@echo "Removing GitHub Actions self-hosted runner..."
rm -rf $(RUNNER_PATH)/runner
.PHONY: runner-start
runner-start:
@echo "Starting GitHub Actions self-hosted runner..."
$(RUNNER_PATH)/runner/run.sh