feat: update Makefile
- build containers on master if greater than 24 hours, or the commit hash changes
This commit is contained in:
parent
f497194dda
commit
2d1ee5e2c6
28
Makefile
28
Makefile
@ -6,9 +6,10 @@ 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)/||')
|
||||
@ -40,18 +41,19 @@ $(DIRS):
|
||||
# Export environment
|
||||
export VAULT_TOKEN=$$(vault write -field=token auth/approle/login role_id=$$VAULT_ROLEID)
|
||||
export VAULT_ADDR=https://vault.service.consul:8200
|
||||
@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
|
||||
@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
|
||||
|
||||
@ -73,18 +75,20 @@ $(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
|
||||
|
||||
# Clean all symlinks
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user