Skip to content

Commit

Permalink
use consistent TF version when invoking TF
Browse files Browse the repository at this point in the history
- ensure the generation of the `example` directory data is done in a
  consistent, reproducible fashion
- ensure GH Actions uses the same version of TF expected by the `example` directory
- add plan and plan JSON files to source control, for testing purposes
  • Loading branch information
mdb committed Jan 15, 2024
1 parent f99306a commit 40f8d1b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 47 deletions.
97 changes: 51 additions & 46 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,54 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false

- name: Install terraform-plan-summary
run: |
REPO="dineshba/tf-summarize"
curl -LO https://github.com/$REPO/releases/latest/download/tf-summarize_linux_amd64.zip
tmpDir=$(mktemp -d -t tmp.XXXXXXXXXX)
mv tf-summarize_linux_amd64.zip $tmpDir
cd $tmpDir
unzip tf-summarize_linux_amd64.zip
chmod +x tf-summarize
echo $PWD >> $GITHUB_PATH
- name: Print tf-summarize version and help
run: |
tf-summarize -v
tf-summarize -h
- name: Terraform Init
run: terraform init
working-directory: ./example

- name: Terraform Plan
run: terraform plan -out=tfplan -refresh=false # -refresh=false is only for demo workflow
working-directory: ./example

- name: summary in table format
run: terraform show -json tfplan | tf-summarize
working-directory: ./example

- name: summary in tree format
run: terraform show -json tfplan | tf-summarize -tree
working-directory: ./example

- name: summary in separate tree format
run: terraform show -json tfplan | tf-summarize -separate-tree
working-directory: ./example

- name: summary in draw visual tree format
run: terraform show -json tfplan | tf-summarize -tree -draw
working-directory: ./example
- uses: actions/checkout@v3

- name: Set Terraform version
id: set-terraform-version
run: echo "terraform-version=$(cat example/.terraform-version)" >> $GITHUB_OUTPUT

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
terraform_version: ${{ steps.set-terraform-version.outputs.terraform-version }}

- name: Install terraform-plan-summary
run: |
REPO="dineshba/tf-summarize"
curl -LO https://github.com/$REPO/releases/latest/download/tf-summarize_linux_amd64.zip
tmpDir=$(mktemp -d -t tmp.XXXXXXXXXX)
mv tf-summarize_linux_amd64.zip $tmpDir
cd $tmpDir
unzip tf-summarize_linux_amd64.zip
chmod +x tf-summarize
echo $PWD >> $GITHUB_PATH
- name: Print tf-summarize version and help
run: |
tf-summarize -v
tf-summarize -h
- name: Terraform Init
run: terraform init
working-directory: ./example

- name: Terraform Plan
run: terraform plan -out=tfplan -refresh=false # -refresh=false is only for demo workflow
working-directory: ./example

- name: summary in table format
run: terraform show -json tfplan | tf-summarize
working-directory: ./example

- name: summary in tree format
run: terraform show -json tfplan | tf-summarize -tree
working-directory: ./example

- name: summary in separate tree format
run: terraform show -json tfplan | tf-summarize -separate-tree
working-directory: ./example

- name: summary in draw visual tree format
run: terraform show -json tfplan | tf-summarize -tree -draw
working-directory: ./example
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
TERRAFORM_VERSION:=$(shell cat example/.terraform-version)

.PHONY: help
help: ## prints help (only for tasks with comment)
@grep -E '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand All @@ -17,4 +19,22 @@ test: lint
i: install ## build and install to /usr/local/bin/

lint:
golangci-lint run --timeout 10m -v
golangci-lint run --timeout 10m -v

define generate-example
docker run \
--interactive \
--tty \
--volume $(shell pwd):/src \
--workdir /src/example \
--entrypoint /bin/sh \
hashicorp/terraform:$(1) \
-c \
"terraform init && \
terraform plan -out tfplan && \
terraform show -json tfplan > tfplan.json"
endef

example:
$(call generate-example,$(TERRAFORM_VERSION))
.PHONY: example
1 change: 1 addition & 0 deletions example/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.1.4
Binary file added example/tfplan
Binary file not shown.
Loading

0 comments on commit 40f8d1b

Please sign in to comment.