Skip to content

Instantly share code, notes, and snippets.

@chrispruitt
Last active February 18, 2020 13:15
Show Gist options
  • Save chrispruitt/d0eb9ee562bcb4560230c84e7cca4fb2 to your computer and use it in GitHub Desktop.
Save chrispruitt/d0eb9ee562bcb4560230c84e7cca4fb2 to your computer and use it in GitHub Desktop.
Auto generate atlantis.yaml file for terraform repository
#!/bin/bash
# Run this file at the root level of your terraform repo to generate your basic atlantis.yaml file
# This file is somewhat specific to my needs so it will probably need some tweaking for your own terraform setup
ATLANTIS_YAML="atlantis.yaml"
TERRAFORM_VERSION="v0.11.11"
WORKSPACES="dev tst stg prd"
touch ${ATLANTIS_YAML}
echo "version: 3" > ${ATLANTIS_YAML}
echo "automerge: false" >> ${ATLANTIS_YAML}
echo "projects:" >> ${ATLANTIS_YAML}
function get_module_sources() {
grep -ris source "${1}"/*.tf | grep -os "source *= .*" | sed 's/source *= //g' | sort | uniq | xargs
}
add_sources() {
local PARENT_MODULE_PATH=$1
local CHILD_MODULE_PATHS=$(get_module_sources "${PARENT_MODULE_PATH}")
for CHILD_MODULE_PATH in ${CHILD_MODULE_PATHS}; do
# TODO: 1. condense paths for cleanliness
# TODO: 2. check if PATH already exists and do not add duplicates.
SOURCES+="\"${PARENT_MODULE_PATH}/${CHILD_MODULE_PATH}/**/*.tf\","
add_sources "${PARENT_MODULE_PATH}/${CHILD_MODULE_PATH}"
done
}
find workspaces | grep "tf$" | xargs -I % dirname % | sort | uniq | grep -v .terraform | while read line; do
SOURCES_RAW=$(get_module_sources "${line}")
export SOURCES=""
add_sources "${line}"
substring="${line}/"
SOURCES=${SOURCES//${substring}/}
SOURCES+="\"*.tf*\""
if [[ -z "$var" ]]
then
for env in ${WORKSPACES}; do
if [[ ${line} =~ /${env}/ ]]
then
echo "Adding entry for ${line} with sources: $SOURCES"
{
echo " - dir: ${line}"
echo " workspace: ${env}"
echo " terraform_version: ${TERRAFORM_VERSION}"
echo " autoplan:"
echo " when_modified: [${SOURCES}]"
echo " enabled: true"
} >> ${ATLANTIS_YAML}
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment