I got free credit from AWS and create some EC2, one of them is for my mini projects in https://urusan.id. Set up a server for so many projects is an activity that does the same thing over and over again. So, automation will come to play.
I already set up my main domain in cloudflare. Every new project will get subdomain. I also already setup server using dokku (PaaS like heroku). Let's see how I make a new project in a manual way:
- Search idea and name
- Write some code
- Create new dokku app
- Git init and push to dokku
- Add domain to dokku app
- Register domain in cloudflare
- Add SSL from cloudflare to dokku app
The first step obviously will be manual, it is human's job. The second step, we can use boilerplate code to initialize. The rest, I will use make
command to do it automatically. Why not using bash script
? Well, makefile simplifies how we define every step in target. In bash script, we need to define every target in parameter, and if in any case an error occurred, we can't just jump to specific step easily. So let see how it works
.PHONY: help
help: ## make project project_name=pendidikan domain=pendidikan.urusan.id
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
DOKKU_SERVER := SERVER_IP_ADDRESS
create-dokku-project: ## Create new dokku apps on server
ssh -i server_key.pem dokku@$(DOKKU_SERVER) apps:create $(project_name)
ssh -i server_key.pem dokku@$(DOKKU_SERVER) buildpacks:add $(project_name) https://github.com/heroku/heroku-buildpack-python.git\#remove-sqlite
create-new-project: ## Create local project
cp -R boilerplate ../$(project_name)
cd ../$(project_name); git init
cd ../$(project_name); git remote add dokku dokku@$(DOKKU_SERVER):$(project_name)
cd ../$(project_name); git add .
cd ../$(project_name); git commit -m "initial commit"
push-new-project: create-new-project ## Push new project to test if it works
cd ../$(project_name); git push dokku master
add-domain: ## add domain
ssh -i server_key.pem dokku@$(DOKKU_SERVER) domains:add $(project_name) $(domain)
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer TOKEN_CLOUDFLARE" \
-H "Content-Type: application/json" \
--data '{"type":"CNAME","name":"'$(domain)'","content":"urusan.id","ttl":1,"proxied":true}'
ssl: ## add ssl for this domain
# Only works for domain and subdomain
# Sub-subdomain will not work
ssh -i server_key.pem ubuntu@$(DOKKU_SERVER) sh dokku_give_ssl.sh $(project_name)
project: create-dokku-project push-new-project add-domain ssl ## Create ready-to-use new project
It only takes less than 5 minutes to create a live project with SSL. Actually, I also create one like this at Alterra, but with more features: User management, github or gitlab integration, multiple CI integration, private Docker registry and deploy to kubernetes. If we see some repetition in a process, then there is a possibility to implement automation.