Skip to content

Instantly share code, notes, and snippets.

@so-jelly
Last active February 9, 2023 17:35
Show Gist options
  • Save so-jelly/7a5a3e3cb0cbb5f57af181ea95124baf to your computer and use it in GitHub Desktop.
Save so-jelly/7a5a3e3cb0cbb5f57af181ea95124baf to your computer and use it in GitHub Desktop.
terraform
variable "project_id" {
description = "The name of the Google Project ID where the keys will be stored. eg: 'my-project'"
type = string
}
data "google_projects" "prj" {
filter = "id:${var.project_id}"
lifecycle {
postcondition {
condition = length(self.projects) == 1
error_message = "invalid project id"
}
}
}
#!/usr/bin/env bash
set -eExo pipefail
GITHUB_BASE_REF=${GITHUB_BASE_REF:-master}
# we want a list of changed directories
# expected output: 'value=["dir1","dir2"]'
# start a list of changed directories
changed_dirs=()
# read directories into the array
# find out which project folders were updated
# GITHUB_BASE_REF is the name of the base ref or target branch of the pull request
# unique dirs with terraform files
# TODO diff-filter=ACMRT don't display removed files
# would orphan infrastructure, need to destroy
readarray -t -O "${#changed_dirs[@]}" changed_dirs < <(
git diff \
--name-only \
"origin/${GITHUB_BASE_REF:-master}" |
grep -E ".tf$" |
xargs dirname |
uniq
)
# build a json array [ "dir1" , "dir2" ]
output=('value=[')
while [ ${#changed_dirs[@]} -gt 0 ]; do
# make sure to add quotes
output+=(\""${changed_dirs[0]}"\")
# add commas if not the last item
[ ${#changed_dirs[@]} -gt 1 ] && output+=(',')
# remove the item we just added
changed_dirs=("${changed_dirs[@]:1}")
done
# finalize the json
output+=(']')
printf "%s" "${output[@]}" >>"${GITHUB_OUTPUT:-/dev/stdout}"
mystring = yamldecode(<<-EOT
x: >-
fgr
grg
gr
EOT
).x
locals {
inputs = [
"foo",
"bar",
"baz"
]
}
variable "input" {
type = string
description = "the input. eg: 'foo' " # would be nice to do 'join(",",local.inputs)'
default = "foo"
}
resource "null_resource" "valid_inputs" {
lifecycle {
precondition {
condition = contains(var.input, locals.inputs)
error_message = "input is unacceptable, please select from: ${join(",", local.inputs)}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment