diff --git a/Makefile b/Makefile index e0e8185..639c691 100644 --- a/Makefile +++ b/Makefile @@ -6,17 +6,26 @@ SYMLINK_PREFIX := library_ # Docker registry variables REGISTRY := git.query.consul OWNER := unkin -#GIT_COMMIT := $(shell git rev-parse --short HEAD) DATE_TAG := $(shell date +%Y%m%d) -BRANCH=$(shell git branch --show-current) +SUFFIX=$(shell basename $(mktemp -u) | cut -d . -f 2) +GIT_BRANCH=$(shell git branch --show-current) +GIT_COMMIT := $(shell git rev-parse --short HEAD) # Find all subdirectories under the IMAGES_PATH DIRS := $(shell find $(IMAGES_PATH) -mindepth 3 -maxdepth 3 -type d | sed 's|$(IMAGES_PATH)/||') -TAGS := $(shell find $(IMAGES_PATH) -mindepth 3 -maxdepth 3 -type d | sed 's|$(IMAGES_PATH)/|tag-|') -PUSH := $(shell find $(IMAGES_PATH) -mindepth 3 -maxdepth 3 -type d | sed 's|$(IMAGES_PATH)/|push-|') .PHONY: list $(DIRS) +.DEFAULT_GOAL := default +default: + ./ci/build.sh + +# Make all images +all: + @for dir in $(DIRS); do \ + $(MAKE) $$dir; \ + done + # List all directories list: @echo "Images:" @@ -30,18 +39,21 @@ $(DIRS): @echo "Building for $@" # Export environment - @for kv in $$(vault kv get -format=json kv/service/packer/builder/env | jq -r '.data.data | to_entries[] | "\(.key)=\(.value)"'); do \ - export "$kv"; \ - done + export VAULT_TOKEN=$$(vault write -field=token auth/approle/login role_id=$$VAULT_ROLEID) + export VAULT_ADDR=https://vault.service.consul:8200 + @eval $$(vault kv get -format=json kv/service/packer/builder/env | jq -r '.data.data | to_entries[] | "export \(.key)=\(.value)"') # Check if on master branch - @if [ "$(BRANCH)" = "master" ]; then \ - echo "Current branch is $(BRANCH), checking latest timestamp in consul."; \ + @if [ "$(GIT_BRANCH)" = "master" ]; then \ + echo "Current branch is $(GIT_BRANCH), checking latest timestamp in consul."; \ LAST_BUILD_TIMESTAMP=$$(consul kv get infra/packer/$@/timestamp || echo "0"); \ CURRENT_TIME=$$(date +%s); \ if [ $$((CURRENT_TIME - LAST_BUILD_TIMESTAMP)) -lt 86400 ]; then \ - echo "Skipping build for $@ (built within the last 24 hours)"; \ - exit 0; \ + LAST_COMMIT=$$(consul kv get infra/packer/$@/commit || echo "0"); \ + if [ "$$((LAST_COMMIT))" == "$(GIT_COMMIT)" ]; then \ + echo "Skipping build for $@. Same commit as last, and less than 24 hours since last build."; \ + exit 0; \ + fi; \ fi; \ fi @@ -63,48 +75,22 @@ $(DIRS): export DOCKER_SOURCE=$$OS_NAME:$$OS_VERSION_FULL && \ export DOCKER_SERVER='git.query.consul' && \ export INCUS_SOURCE="images:$$OS_NAME/$$OS_VERSION_MAJOR" && \ - export SUFFIX=$$(basename $$(mktemp -u) | cut -d . -f 2) && \ - export GIT_BRANCH=$(BRANCH) && \ + export SUFFIX=$(SUFFIX) && \ + export GIT_COMMIT=$(GIT_COMMIT) && \ + export GIT_BRANCH=$(GIT_BRANCH) && \ packer init . && \ packer build . ) # Update build timestamp and date in Consul if on master branch @if [ "$(BRANCH)" = "master" ]; then \ - echo "Current branch is $(BRANCH), updating consul."; \ + echo "Current branch is $(GIT_BRANCH), updating consul."; \ CURRENT_TIMESTAMP=$$(date +%s); \ READABLE_DATE=$$(date '+%Y-%m-%d %H:%M:%S %Z'); \ consul kv put infra/packer/$@/timestamp $$CURRENT_TIMESTAMP; \ consul kv put infra/packer/$@/date "$$READABLE_DATE"; \ + consul kv put infra/packer/$@/commit "$$GIT_COMMIT"; \ fi -.PHONY: $(DIRS) $(TAGS) $(PUSH) - -# Tag Docker images -$(TAGS): - @echo "Tagging Docker image for $$(echo $@ | sed 's|tag-||')" - @OS_NAME=$$(echo "$@" | sed 's|tag-||' | cut -d'/' -f1); \ - OS_VERSION_FULL=$$(echo "$@" | sed 's|tag-||' | cut -d'/' -f2); \ - OS_IMAGE=$$(echo "$@" | sed 's|tag-||' | cut -d'/' -f3); \ - OS_VERSION_MAJOR=$$(echo $$OS_VERSION_FULL | cut -d'.' -f1); \ - IMAGE_NAME="$(REGISTRY)/$(OWNER)/$$OS_NAME$$OS_VERSION_MAJOR-$$OS_IMAGE"; \ - echo "Tagging Image Name: $$IMAGE_NAME:$(DATE_TAG)"; \ - docker tag $$IMAGE_NAME $$IMAGE_NAME:$(DATE_TAG); \ - echo "Tagging Image Name: $$IMAGE_NAME:latest"; \ - docker tag $$IMAGE_NAME $$IMAGE_NAME:latest - -# Push Docker images -$(PUSH): - @echo "Pushing Docker image for $$(echo $@ | sed 's|push-||')" - @OS_NAME=$$(echo "$@" | sed 's|push-||' | cut -d'/' -f1); \ - OS_VERSION_FULL=$$(echo "$@" | sed 's|push-||' | cut -d'/' -f2); \ - OS_IMAGE=$$(echo "$@" | sed 's|push-||' | cut -d'/' -f3); \ - OS_VERSION_MAJOR=$$(echo $$OS_VERSION_FULL | cut -d'.' -f1); \ - IMAGE_NAME="$(REGISTRY)/$(OWNER)/$$OS_NAME$$OS_VERSION_MAJOR-$$OS_IMAGE"; \ - echo "Pushing Image Name: $$IMAGE_NAME:$(DATE_TAG)"; \ - docker push $$IMAGE_NAME:$(DATE_TAG); \ - echo "Pushing Image Name: $$IMAGE_NAME:latest"; \ - docker push $$IMAGE_NAME:latest - # Clean all symlinks clean: @echo "Cleaning up symlinks..." diff --git a/ci/build.sh b/ci/build.sh new file mode 100755 index 0000000..55681ed --- /dev/null +++ b/ci/build.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Check for changes in builds/ folder +builds_changes=$(git diff --name-only master | grep -E '^builds/') + +# Check for changes in images/ folder +images_changes=$(git diff --name-only master | grep -E '^images/') + +# Run `make all` if there are changes in builds/ +if [ -n "$builds_changes" ]; then + echo "Changes detected in builds/. Running 'make build-all'..." + make build-all +fi + +# Run specific `make` commands for each changed file in images/ +if [ -n "$images_changes" ]; then + echo "Changes detected in images/. Running specific 'make' commands..." + + # Extract unique paths for `make` commands + for file in $images_changes; do + # Get the subdirectory path for the make command (e.g., almalinux/8.10/actionsdind) + target=$(echo "$file" | sed -E 's|images/||; s|/[^/]+$||') + echo "Running 'make $target'..." + make "$target" + done +fi + +# If no changes, output a message +if [ -z "$builds_changes" ] && [ -z "$images_changes" ]; then + echo "No relevant changes detected." +fi diff --git a/images/almalinux/8.10/actionsdind/variables.auto.pkrvars.hcl b/images/almalinux/8.10/actionsdind/variables.auto.pkrvars.hcl index dc0ba68..06f2a53 100644 --- a/images/almalinux/8.10/actionsdind/variables.auto.pkrvars.hcl +++ b/images/almalinux/8.10/actionsdind/variables.auto.pkrvars.hcl @@ -2,10 +2,16 @@ docker_source = "git.query.consul/unkin/almalinux8-base:latest" packages = [ "bash", + "consul", "docker-ce-cli", + "jq", "make", "nodejs", - "unzip" + "packer", + "terraform", + "terragrunt", + "unzip", + "vault" ] scripts_pre_packages = [ "dnf install -y yum-utils", diff --git a/images/almalinux/9.5/actionsdind/variables.auto.pkrvars.hcl b/images/almalinux/9.5/actionsdind/variables.auto.pkrvars.hcl index 812b45f..46e693e 100644 --- a/images/almalinux/9.5/actionsdind/variables.auto.pkrvars.hcl +++ b/images/almalinux/9.5/actionsdind/variables.auto.pkrvars.hcl @@ -2,10 +2,16 @@ docker_source = "git.query.consul/unkin/almalinux9-base:latest" packages = [ "bash", + "consul", "docker-ce-cli", + "jq", "make", "nodejs", - "unzip" + "packer", + "terraform", + "terragrunt", + "unzip", + "vault" ] scripts_pre_packages = [ "dnf install -y yum-utils", diff --git a/library/variables.pkr.hcl b/library/variables.pkr.hcl index f26fe58..9845aa9 100644 --- a/library/variables.pkr.hcl +++ b/library/variables.pkr.hcl @@ -111,6 +111,11 @@ variable "suffix" { type = string default = env("SUFFIX") } +variable "git_commit" { + description = "The current git commit." + type = string + default = env("GIT_COMMIT") +} variable "git_branch" { description = "The current git branch." type = string