Last active
February 18, 2018 21:20
-
-
Save danieljimenez/353ba906e4dca83c5988ee6dbb6d3a1a to your computer and use it in GitHub Desktop.
Makefile for Cloudformation templates with Jinja2 support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY : prereqs clean build upload | |
.DELETE_ON_ERROR: | |
.SUFFIXES: | |
# methods to verified variables are definied | |
check_defined = \ | |
$(strip $(foreach 1,$1, \ | |
$(call _check_defined,$1,$(strip $(value 2))))) | |
_check_defined = \ | |
$(if $(value $1),, \ | |
$(error Undefined argument $1$(if $2, ($2)))) | |
MAKEFILE_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) | |
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) | |
# Directory containing source (YAML) files | |
source_dir := $(MAKEFILE_DIR)/src/main/yaml | |
# Directory containing templated YAML files | |
output_dir := $(MAKEFILE_DIR)/build/${BUILD_NUMBER} | |
# All markdown files in src/main/yaml are considered sources | |
sources := $(wildcard $(source_dir)/*.yaml $(source_dir)/**/*.yaml) | |
# Convert the list of source files | |
objects := $(patsubst %.yaml,%.yaml,$(subst $(source_dir),$(output_dir),$(sources))) | |
# Prerequsite tools, we use these variables to check they exist. | |
j2 := $(shell command -v j2 2> /dev/null) | |
yamllint := $(shell command -v yamllint 2> /dev/null) | |
$(call check_defined, j2, install j2 via 'pip install j2cli') | |
$(call check_defined, yamllint, install yamllint via 'pip install yamllint') | |
# Check required variables are set. | |
$(call check_defined, BUCKET, specify the bucket youd like to store the templates in) | |
$(call check_defined, BUILD_NUMBER, specify the build or git revision number) | |
# Targets | |
build: $(objects) | |
clean: | |
$(info Cleaning $(MAKEFILE_DIR)/build...) | |
@ rm -rf $(MAKEFILE_DIR)/build/ | |
$(output_dir)/%.yaml: $(source_dir)/%.yaml | |
$(info Processing $< with Jinja2...) | |
@ mkdir -p $(abspath $(dir $@)) | |
@ yamllint $< | |
@ j2 \ | |
-e "bucket='${BUCKET}'" \ | |
-e "build_number='${BUILD_NUMBER}'" \ | |
$< > $@ | |
$(info Validating $@...) | |
@ aws cloudformation validate-template --template-body file://$@ >/dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | |
BUILD_NUMBER=$(git -C ${DIR} describe --tags --always --dirty ) | |
export BUILD_NUMBER | |
export BUCKET | |
# First build the templates out. | |
make -j 16 -C ${DIR} build | |
# Now upload them to S3. | |
aws s3 sync "${DIR}/build/${BUILD_NUMBER}" "s3://${BUCKET}/cloudformation/${BUILD_NUMBER}" \ | |
| sed s/s3:../https:\\/\\/s3.amazonaws.com\\//g \ | |
| grep -v '/resources/' \ | |
| sed s/upload..//g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment