Skip to content

Outputs and tests #20

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions .github/workflows/release.yml

This file was deleted.

101 changes: 101 additions & 0 deletions .github/workflows/terragrunt-cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: 'Terraform Module CI'

on:
push:
branches:
- main
paths:
- '**/*.hcl'
- '**/*.tf'
pull_request:
branches:
- main
paths:
- '**/*.hcl'
- '**/*.tf'

permissions:
contents: read
pull-requests: write

jobs:
validate:
name: 'Validate Module'
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.0

- name: Terraform Init
run: |
terraform init -backend=false
- name: Terraform Format
run: |
terraform fmt -check
- name: Terraform Validate
run: |
terraform validate
- name: Run tflint
uses: terraform-linters/setup-tflint@v3
if: github.event_name == 'pull_request'

- name: Lint Terraform
if: github.event_name == 'pull_request'
run: |
tflint --format compact
release:
name: 'Create Release'
needs: validate
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: self-hosted
permissions:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install Commitizen
run: |
pip install commitizen
- name: Configure Git
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Bump Version and Generate Changelog
id: cz
run: |
cz bump --yes
echo "new_version=$(cz version --project)" >> $GITHUB_OUTPUT
echo "changelog=$(cz changelog --dry-run)" >> $GITHUB_OUTPUT
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.cz.outputs.new_version }}
release_name: Release v${{ steps.cz.outputs.new_version }}
draft: false
prerelease: false
body: ${{ steps.cz.outputs.changelog }}
14 changes: 13 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ repos:
- id: check-vcs-permalinks
- id: forbid-new-submodules
- id: no-commit-to-branch
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-vcs-permalinks
- id: check-toml
- id: check-xml
- id: detect-private-key
- id: requirements-txt-fixer
- id: sort-simple-yaml

# Common errors
- id: end-of-file-fixer
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
exclude: CHANGELOG.md

# - id: check-yaml
- id: check-merge-conflict
- id: check-executables-have-shebangs
Expand All @@ -38,7 +50,7 @@ repos:

# Terraform Hooks
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.1 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
rev: v1.97.3 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
hooks:
- id: terraform_fmt
args:
Expand Down
26 changes: 13 additions & 13 deletions .tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ config {
# variables = ["foo=bar", "bar=[\"baz\"]"]
}

rule "aws_instance_invalid_type" {
enabled = true
}
# rule "aws_instance_invalid_type" {
# enabled = true
# }

plugin "aws" {
enabled = true
version = "0.32.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
# plugin "aws" {
# enabled = true
# version = "0.32.0"
# source = "github.com/terraform-linters/tflint-ruleset-aws"
# }

plugin "terraform" {
enabled = true
version = "0.9.0"
source = "github.com/terraform-linters/tflint-ruleset-terraform"
}
# plugin "terraform" {
# enabled = true
# version = "0.9.0"
# source = "github.com/terraform-linters/tflint-ruleset-terraform"
# }
117 changes: 117 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
.PHONY: all init validate test docs clean fmt pre-commit help

# Variables
SHELL := /bin/bash
TERRAFORM := terraform_current
EXAMPLES_DIR := examples
TEST_DIR := tests
MODULE_NAME := $(shell basename $(CURDIR))

# Default target
all: init validate test

# Initialize Terraform
init:
@echo "Initializing Terraform..."
$(TERRAFORM) init -upgrade
@for dir in $(EXAMPLES_DIR)/*/ ; do \
if [ -d "$$dir" ]; then \
echo "Initializing $$dir..."; \
cd "$$dir" && $(TERRAFORM) init -upgrade && cd ../..; \
fi \
done

# Format Terraform code
fmt:
@echo "Formatting Terraform code..."
$(TERRAFORM) fmt -recursive
@for dir in $(EXAMPLES_DIR)/*/ ; do \
if [ -d "$$dir" ]; then \
echo "Formatting $$dir..."; \
cd "$$dir" && $(TERRAFORM) fmt -recursive && cd ../..; \
fi \
done

# Validate Terraform code
validate: fmt
@echo "Validating Terraform code..."
$(TERRAFORM) validate
@for dir in $(EXAMPLES_DIR)/*/ ; do \
if [ -d "$$dir" ]; then \
echo "Validating $$dir..."; \
cd "$$dir" && $(TERRAFORM) validate && cd ../..; \
fi \
done

# Run tflint
lint:
@echo "Running tflint..."
tflint
@for dir in $(EXAMPLES_DIR)/*/ ; do \
if [ -d "$$dir" ]; then \
echo "Linting $$dir..."; \
cd "$$dir" && tflint && cd ../..; \
fi \
done

# Run tests
test: init-go
@echo "Running tests..."
cd $(TEST_DIR) && go test -v ./unit/... ./integration/... -timeout 30m

# Initialize Go modules
init-go:
@echo "Initializing Go modules..."
cd $(TEST_DIR) && go mod tidy

# Generate documentation
docs:
@echo "Generating documentation..."
terraform-docs markdown table --output-file README.md --output-mode inject .
@for dir in $(EXAMPLES_DIR)/*/ ; do \
if [ -d "$$dir" ]; then \
echo "Generating docs for $$dir..."; \
terraform-docs markdown table --output-file README.md --output-mode inject "$$dir"; \
fi \
done

# Clean up
clean:
@echo "Cleaning up..."
find . -type d -name ".terraform" -exec rm -rf {} +
find . -type f -name ".terraform.lock.hcl" -delete
find . -type f -name "terraform-debug..tfstate*" -delete
find . -type f -name "*.log" -delete

# Run pre-commit hooks
pre-commit:
@echo "Running pre-commit hooks..."
pre-commit run --all-files

# Version management
version:
@if [ -z "$(VERSION)" ]; then \
echo "Please specify VERSION=x.x.x"; \
exit 1; \
fi
@echo "Updating version to $(VERSION)..."
sed -i 's/module_version = "[0-9]*\.[0-9]*\.[0-9]*"/module_version = "$(VERSION)"/' version.tf
git add version.tf
git commit -m "🔖 chore(release): v$(VERSION)"
git tag -a "v$(VERSION)" -m "Version $(VERSION)"

# Help
help:
@echo "Available targets:"
@echo " all : Run init, validate, and test"
@echo " init : Initialize Terraform"
@echo " fmt : Format Terraform code"
@echo " validate : Validate Terraform code"
@echo " lint : Run tflint"
@echo " test : Run unit and integration tests"
@echo " docs : Generate documentation"
@echo " clean : Clean up Terraform files"
@echo " pre-commit : Run pre-commit hooks"
@echo " version : Update module version (usage: make version VERSION=x.x.x)"
@echo " help : Show this help message"
@echo " init-go : Initialize Go modules for testing"
Loading
Loading