Merge pull request 'neoloc/actions_update' (#2) from neoloc/actions_update into master

Reviewed-on: https://git.query.consul/unkin/packer-images/pulls/2
This commit is contained in:
Ben Vincent 2025-01-12 16:27:09 +11:00
commit 0f019826e2
5 changed files with 78 additions and 44 deletions

View File

@ -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..."

31
ci/build.sh Executable file
View File

@ -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

View File

@ -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",

View File

@ -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",

View File

@ -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