|
1 | | -# Makefile for building and pushing OPA policies to a registry |
2 | | - |
3 | | -# Variables |
4 | | -REGISTRY_URL := ghcr.io |
5 | | -NAMESPACE := compliance-framework |
6 | | -POLICY_NAME := plugin-github-settings-policies |
7 | | -VERSION := latest |
8 | | -POLICY_DIR := ./policies # Directory containing your .rego files |
9 | | - |
10 | | -# Build and Push Commands |
11 | | -.PHONY: all build bundle push clean |
12 | | - |
13 | | -# Default action |
14 | | -all: test check build push clean |
| 1 | +# The help target prints out all targets with their descriptions organized |
| 2 | +# beneath their categories. The categories are represented by '##@' and the |
| 3 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 4 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 5 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 6 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 7 | +# More info on the usage of ANSI catalog characters for terminal formatting: |
| 8 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 9 | +# More info on the awk command: |
| 10 | +# http://linuxcommand.org/lc3_adv_awk.php |
15 | 11 |
|
16 | 12 | # Check if OPA CLI is installed |
17 | 13 | OPA := $(shell command -v opa 2> /dev/null) |
18 | 14 | ifeq ($(OPA),) |
19 | 15 | $(error "opa CLI not found. Please install it: https://www.openpolicyagent.org/docs/latest/cli/") |
20 | 16 | endif |
21 | 17 |
|
22 | | -# Check if Docker CLI is installed |
| 18 | +##@ Help |
| 19 | +help: ## Display this concise help, ie only the porcelain target |
| 20 | + @awk 'BEGIN {FS = ":.*##"; printf "\033[1mUsage\033[0m\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-30s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
23 | 21 |
|
24 | | -CONTAINER_CLI := "" |
25 | | -DOCKER := $(shell command -v docker 2> /dev/null) |
26 | | -PODMAN := $(shell command -v podman 2> /dev/null) |
27 | | -ifeq ($(DOCKER),) |
28 | | - PODMAN := := $(shell command -v podman 2> /dev/null) |
29 | | - ifeq ($(PODMAN),) |
30 | | - $(error "either docker or podman CLI is required.") |
31 | | - else |
32 | | - CONTAINER_CLI = PODMAN |
33 | | - endif |
34 | | -else |
35 | | - CONTAINER_CLI = DOCKER |
36 | | -endif |
| 22 | +help-all: ## Display all help items, ie including plumbing targets |
| 23 | + @awk 'BEGIN {FS = ":.*#"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?#/ { printf " \033[36m%-25s\033[0m %s\n", $$1, $$2 } /^#@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
37 | 24 |
|
38 | | -test: |
39 | | - @echo "Testing policies..." |
| 25 | +##@ Policies |
| 26 | +test: ## Test policy files |
40 | 27 | @OPA test policies |
41 | 28 |
|
42 | | -# Build the policies |
43 | | -check: |
44 | | - @echo "Checking policies..." |
| 29 | +validate: ## Validate policy files |
45 | 30 | @opa check policies |
46 | 31 |
|
| 32 | +clean: # Cleanup build artifacts |
| 33 | + @rm -f dist/* |
| 34 | + |
47 | 35 | # Bundle the policies into a tarball for OCI registry |
48 | | -build: clean |
49 | | - @echo "Bundling policies..." |
| 36 | +build: clean ## Build the policy bundle |
50 | 37 | @mkdir -p dist/ |
51 | 38 | @opa build -b policies -o dist/bundle.tar.gz |
52 | 39 |
|
53 | | -# Push the bundled policies to an OCI-compliant registry |
54 | | -push: build |
55 | | - @echo "Pushing bundle to registry..." |
56 | | - @# Log in to the registry if necessary |
57 | | - @$(CONTAINER_CLI) login $(REGISTRY_URL) |
58 | | - @# Push the bundle as an OCI artifact |
59 | | - @$(CONTAINER_CLI) cp dist/bundle.tar.gz $(REGISTRY_URL)/$(NAMESPACE)/$(POLICY_NAME):$(VERSION) |
60 | | - @echo "Bundle pushed successfully to $(REGISTRY_URL)/$(NAMESPACE)/$(POLICY_NAME):$(VERSION)" |
61 | | - |
62 | | -# Clean up build artifacts |
63 | | -clean: |
64 | | - @echo "Cleaning up..." |
65 | | - @rm -f dist/bundle.tar.gz |
0 commit comments