Initial template
This commit is contained in:
commit
9f2788e4cf
9 files changed
+138
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# blank
|
||||||
|
|
||||||
|
Copier template for a part in any stack that has no template of its own, either as a `backend/` or `frontend/` part next to other parts or alone at the root of the project (`layout`). It brings nothing but the part contract, the code and the `Dockerfile` are yours.
|
||||||
|
|
||||||
|
Ships `docker-compose.yml` with root-relative paths and one service under its own profile and `services`, `docker-compose.override.yml.example` (the `caddy` network alias, copied by `make env`), a `Makefile` with empty `fmt` and `check`, `build`, `pre-deploy` that rebuilds the image only when the part changed and an empty `post-deploy`, and `.env.example`. Applied on top of the `infra` template, `layout: parts` or `layout: single` respectively. The `caddy` template expects the backend to listen on 8080 and the frontend on 3000.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
copier copy --trust -a .copier/backend.yml -d project_name="My Thing" -d part=backend <this-repo> <dest>
|
||||||
|
copier update -a .copier/backend.yml
|
||||||
|
```
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
_subdirectory: template
|
||||||
|
_templates_suffix: .jinja
|
||||||
|
_answers_file: .copier/part.yml
|
||||||
|
_envops:
|
||||||
|
keep_trailing_newline: true
|
||||||
|
|
||||||
|
project_name:
|
||||||
|
type: str
|
||||||
|
help: Project name
|
||||||
|
|
||||||
|
project_slug:
|
||||||
|
type: str
|
||||||
|
help: Project slug, snake_case
|
||||||
|
default: "{{ project_name | lower | replace(' ', '_') | replace('-', '_') | replace('.', '_') }}"
|
||||||
|
|
||||||
|
part:
|
||||||
|
type: str
|
||||||
|
help: Which part this is
|
||||||
|
choices:
|
||||||
|
backend: backend
|
||||||
|
frontend: frontend
|
||||||
|
default: backend
|
||||||
|
|
||||||
|
layout:
|
||||||
|
type: str
|
||||||
|
help: Where the part lives
|
||||||
|
choices:
|
||||||
|
In its own directory next to other parts: part
|
||||||
|
At the root of the project: root
|
||||||
|
default: part
|
||||||
|
|
||||||
|
service:
|
||||||
|
type: str
|
||||||
|
help: Compose service name, also the profile and the caddy alias suffix
|
||||||
|
default: "{{ 'api' if part == 'backend' else 'frontend' }}"
|
||||||
|
|
||||||
|
part_dir:
|
||||||
|
type: str
|
||||||
|
when: false
|
||||||
|
default: "{{ part if layout == 'part' else '.' }}"
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{{ _copier_answers|to_nice_yaml -}}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
{% if layout == 'root' %}COMPOSE_PROFILES=services
|
||||||
|
{% endif %}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
COMPOSE := {% if layout == 'part' %}cd .. && {% endif %}docker compose
|
||||||
|
IMAGE := {{ project_slug }}/{{ part }}
|
||||||
|
changed = ! git diff --quiet $(1) -- $(2) 2>/dev/null
|
||||||
|
stamp = { git update-ref $(1) HEAD 2>/dev/null || true; }
|
||||||
|
|
||||||
|
.PHONY: {% if layout == 'root' %}env recreate rebuild restart logs down deploy {% endif %}fmt check build pre-deploy post-deploy
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
@:
|
||||||
|
|
||||||
|
check:
|
||||||
|
@:
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(COMPOSE) build {{ service }}
|
||||||
|
|
||||||
|
pre-deploy:
|
||||||
|
@if $(call changed,refs/deploy/{{ part }},.) || ! docker image inspect $(IMAGE) >/dev/null 2>&1; then \
|
||||||
|
$(MAKE) build && $(call stamp,refs/deploy/{{ part }}); \
|
||||||
|
fi
|
||||||
|
|
||||||
|
post-deploy:
|
||||||
|
@:
|
||||||
|
{% if layout == 'root' %}
|
||||||
|
env:
|
||||||
|
@test -f .env || cp .env.example .env
|
||||||
|
@test -f docker-compose.override.yml || cp docker-compose.override.yml.example docker-compose.override.yml
|
||||||
|
|
||||||
|
recreate:
|
||||||
|
$(COMPOSE) up -d --force-recreate
|
||||||
|
|
||||||
|
rebuild: build
|
||||||
|
$(COMPOSE) up -d
|
||||||
|
|
||||||
|
restart:
|
||||||
|
$(COMPOSE) restart
|
||||||
|
|
||||||
|
logs:
|
||||||
|
$(COMPOSE) logs -f --tail=100
|
||||||
|
|
||||||
|
down:
|
||||||
|
$(COMPOSE) down
|
||||||
|
|
||||||
|
deploy: pre-deploy
|
||||||
|
$(COMPOSE) up -d
|
||||||
|
$(MAKE) post-deploy
|
||||||
|
{% endif %}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{%- set dash = project_slug | replace('_', '-') -%}
|
||||||
|
services:
|
||||||
|
{{ service }}:
|
||||||
|
networks:
|
||||||
|
default: {}
|
||||||
|
caddy:
|
||||||
|
aliases:
|
||||||
|
- {{ dash }}-{{ service }}
|
||||||
|
|
||||||
|
networks:
|
||||||
|
caddy:
|
||||||
|
external: true
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
services:
|
||||||
|
{{ service }}:
|
||||||
|
build: {{ part_dir }}
|
||||||
|
image: {{ project_slug }}/{{ part }}
|
||||||
|
profiles: [{{ service }}, services]
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- path: .env
|
||||||
|
required: false
|
||||||
|
{% if layout == 'part' %} - path: {{ part }}/.env
|
||||||
|
required: false
|
||||||
|
{% endif %}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# {{ project_name }}
|
||||||
|
|
||||||
|
Runs in Docker, locally the same way as in production. `COMPOSE_PROFILES` in `.env` selects the services that run on this machine. `docker-compose.override.yml` holds what differs between machines: published ports and the aliases on the external `caddy` network.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
make env
|
||||||
|
make recreate
|
||||||
|
```
|
||||||
|
|
||||||
|
`make env` creates `.env` and `docker-compose.override.yml` from their examples. `make recreate` starts the selected profiles, `make rebuild` builds the image first, `make logs`, `make restart` and `make down` do what they say.
|
||||||
|
|
||||||
|
To deploy, `git pull && make deploy`. It runs `make pre-deploy`, which rebuilds the image only when something in the repository changed, then `docker compose up -d`. A deploy system with its own `up` step calls `make pre-deploy` before it and `make post-deploy` after. What was built is remembered as `refs/deploy/*` in the clone.
|
||||||
Reference in new issue
Block a user