Skip to content

sergii-nosachenko - 1/merge #1

sergii-nosachenko - 1/merge

sergii-nosachenko - 1/merge #1

name: Terraform Validate
run-name: ${{ github.actor }} - ${{ github.ref_name }}
on:
pull_request:
branches:
- main
workflow_dispatch:
permissions:
id-token: write
contents: read
issues: write
env:
ARM_CLIENT_ID: "${{ secrets.AZURE_CLIENT_ID }}"
ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}"
ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}"
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Check Required Files
run: |
required_files=("main.tf" "resource_group.tf" "network.tf" "load_balancer.tf" "vmss.tf" "vars.tf" "outputs.tf")
for file in "${required_files[@]}"; do
if [ ! -f "$file" ]; then
echo "Error: $file is missing"
exit 1
fi
done
echo "All required files are present."
- name: Check Resource Group Configuration
run: |
if ! grep -q 'azurerm_resource_group' resource_group.tf; then
echo "Error: Resource group is not defined in resource_group.tf"
exit 1
fi
if ! grep -q 'location.*=.*"West Europe"' resource_group.tf; then
echo "Error: Resource group location should be set to West Europe"
exit 1
fi
echo "Resource group configuration is correct."
- name: Check Network Configuration
run: |
if ! grep -q 'azurerm_virtual_network' network.tf; then
echo "Error: Virtual network is not defined in network.tf"
exit 1
fi
if ! grep -q 'address_space.*=.*\["10.0.0.0/16"\]' network.tf; then
echo "Error: Virtual network CIDR block should be 10.0.0.0/16"
exit 1
fi
if ! grep -q 'azurerm_subnet' network.tf; then
echo "Error: Subnet is not defined in network.tf"
exit 1
fi
if ! grep -q 'address_prefixes.*=.*\["10.0.2.0/24"\]' network.tf; then
echo "Error: Subnet CIDR block should be 10.0.2.0/24"
exit 1
fi
echo "Network configuration is correct."
- name: Check Load Balancer Configuration
run: |
if ! grep -q 'azurerm_public_ip' load_balancer.tf; then
echo "Error: Public IP is not defined in load_balancer.tf"
exit 1
fi
if ! grep -q 'allocation_method.*=.*"Static"' load_balancer.tf; then
echo "Error: Public IP allocation method should be Static"
exit 1
fi
if ! grep -q 'azurerm_lb' load_balancer.tf; then
echo "Error: Load balancer is not defined in load_balancer.tf"
exit 1
fi
if ! grep -q 'azurerm_lb_backend_address_pool' load_balancer.tf; then
echo "Error: Backend address pool is not defined in load_balancer.tf"
exit 1
fi
if ! grep -q 'azurerm_lb_probe' load_balancer.tf; then
echo "Error: Health probe is not defined in load_balancer.tf"
exit 1
fi
if ! grep -q 'azurerm_lb_rule' load_balancer.tf; then
echo "Error: Load balancer rule is not defined in load_balancer.tf"
exit 1
fi
echo "Load balancer configuration is correct."
- name: Check VMSS Configuration
run: |
if ! grep -q 'azurerm_linux_virtual_machine_scale_set' vmss.tf; then
echo "Error: Virtual Machine Scale Set is not defined in vmss.tf"
exit 1
fi
if ! grep -q 'sku.*=.*"Standard_B1s"' vmss.tf; then
echo "Error: VMSS SKU should be Standard_B1s"
exit 1
fi
if ! grep -q 'instances.*=.*2' vmss.tf; then
echo "Error: VMSS should have 2 instances"
exit 1
fi
if ! grep -q 'azurerm_virtual_machine_scale_set_extension' vmss.tf; then
echo "Error: VMSS extension for custom script is not defined in vmss.tf"
exit 1
fi
echo "VMSS configuration is correct."
- name: Check Variables and Outputs
run: |
if ! grep -q 'variable.*"server_port"' vars.tf; then
echo "Error: server_port variable is not defined in vars.tf"
exit 1
fi
if ! grep -q 'default.*=.*80' vars.tf; then
echo "Error: server_port default value should be 80"
exit 1
fi
if ! grep -q 'data.*"azurerm_public_ip"' outputs.tf; then
echo "Error: Public IP data source is not defined in outputs.tf"
exit 1
fi
if ! grep -q 'output.*"public_ip_address"' outputs.tf; then
echo "Error: Public IP address output is not defined in outputs.tf"
exit 1
fi
echo "Variables and outputs are correctly defined."
- name: Terraform Fmt
run: terraform fmt -check -recursive -diff
- name: Terraform Init
run: terraform init -backend=false
- name: Terraform Validate
run: terraform validate