-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathMakefile
More file actions
97 lines (82 loc) · 4.47 KB
/
Copy pathMakefile
File metadata and controls
97 lines (82 loc) · 4.47 KB
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
CONTAINER_VERSION := $(shell tr -d '[:space:]' < src/version.txt)
# Container image repository.
IMAGE := ghcr.io/felddy/weewx
# GitHub repository and the directory of branch/tag rulesets managed as code.
# Ruleset IDs are resolved at run time by name, so they are not hardcoded.
# See .github/rulesets/README.md for what the JSON contains (and a decoder
# for the GitHub App IDs it references).
REPO := felddy/weewx-docker
RULESET_DIR := .github/rulesets
.PHONY: guard-version guard-gh guard-jq build test version github-output help release apply-ruleset export-ruleset
## guard-version: fail loudly if the version source is missing or empty.
guard-version:
@test -n "$(CONTAINER_VERSION)" || { echo "ERROR: src/version.txt missing or empty" >&2; exit 1; }
## build: build the container image tagged with the CONTAINER_VERSION.
build: guard-version
docker buildx build --build-arg CONTAINER_VERSION=$(CONTAINER_VERSION) --load --tag $(IMAGE):$(CONTAINER_VERSION) .
## test: run the test suite against the built container image.
test: guard-version
uv run --group dev pytest tests/ --image-tag $(IMAGE):$(CONTAINER_VERSION)
## README.md: render the documentation from its template using the version.
README.md: README.md.j2 src/version.txt guard-version
uv run --group dev python render_docs.py README.md.j2 README.md $(CONTAINER_VERSION)
## version: print the derived CONTAINER_VERSION.
version: guard-version
@echo "Container : $(CONTAINER_VERSION)"
## github-output: print key=value lines for appending to $GITHUB_OUTPUT.
github-output: guard-version
@echo "container_version=$(CONTAINER_VERSION)"
## release: set an explicit version, re-render docs, and commit (make release VERSION=x.y.z).
release: guard-version
@test -n "$(VERSION)" || { echo "ERROR: VERSION is required (make release VERSION=x.y.z)" >&2; exit 1; }
./bump-version set $(VERSION)
$(MAKE) README.md
git add README.md
git commit --message "Render docs for $(VERSION)"
## guard-gh: fail loudly if the GitHub CLI is unavailable.
guard-gh:
@command -v gh >/dev/null 2>&1 || { echo "ERROR: gh (GitHub CLI) is required" >&2; exit 1; }
## guard-jq: fail loudly if jq is unavailable.
guard-jq:
@command -v jq >/dev/null 2>&1 || { echo "ERROR: jq is required" >&2; exit 1; }
## apply-ruleset: create or update every ruleset in RULESET_DIR (IDs resolved by name).
apply-ruleset: guard-gh guard-jq
@test -n "$$(ls $(RULESET_DIR)/*.json 2>/dev/null)" || { echo "ERROR: no ruleset files in $(RULESET_DIR)" >&2; exit 1; }
@set -e; \
for f in $(RULESET_DIR)/*.json; do \
name=$$(jq -r '.name' "$$f"); \
id=$$(gh api "repos/$(REPO)/rulesets" --jq "[.[] | select(.name == \"$$name\") | .id][0] // empty"); \
if [ -n "$$id" ]; then \
echo "Updating ruleset '$$name' (id $$id) in $(REPO)"; \
gh api --method PUT "repos/$(REPO)/rulesets/$$id" --input "$$f" >/dev/null; \
else \
echo "Creating ruleset '$$name' in $(REPO)"; \
gh api --method POST "repos/$(REPO)/rulesets" --input "$$f" >/dev/null; \
fi; \
done; \
echo "Done."
## export-ruleset: overwrite each file in RULESET_DIR from its live ruleset.
export-ruleset: guard-gh guard-jq
@test -n "$$(ls $(RULESET_DIR)/*.json 2>/dev/null)" || { echo "ERROR: no ruleset files in $(RULESET_DIR)" >&2; exit 1; }
@set -e; \
for f in $(RULESET_DIR)/*.json; do \
name=$$(jq -r '.name' "$$f"); \
id=$$(gh api "repos/$(REPO)/rulesets" --jq "[.[] | select(.name == \"$$name\") | .id][0] // empty"); \
if [ -z "$$id" ]; then echo "ERROR: no ruleset named '$$name' in $(REPO)" >&2; exit 1; fi; \
raw=$$(gh api "repos/$(REPO)/rulesets/$$id"); \
printf '%s\n' "$$raw" | jq '{name, target, enforcement, conditions, bypass_actors, rules}' > "$$f.tmp"; \
mv "$$f.tmp" "$$f"; \
echo "Wrote $$f from ruleset '$$name' (id $$id)"; \
done
## help: list the developer-invocable targets.
help:
@echo "Available targets:"
@echo " apply-ruleset Create/update the branch and tag protection rulesets (resolved by name)."
@echo " build Build the container image tagged with the CONTAINER_VERSION."
@echo " export-ruleset Overwrite the ruleset JSON files from the live rulesets."
@echo " github-output Print key=value lines for CI."
@echo " help Show this help message."
@echo " README.md Render README.md from README.md.j2 using the version."
@echo " release Set VERSION, re-render docs, and commit (make release VERSION=x.y.z)."
@echo " test Run the test suite (uv run pytest tests/)."
@echo " version Print the derived CONTAINER_VERSION."