diff --git a/.github/workflows/terraform.lint.pull_request.yaml b/.github/workflows/terraform.lint.pull_request.yaml new file mode 100644 index 0000000..5da9791 --- /dev/null +++ b/.github/workflows/terraform.lint.pull_request.yaml @@ -0,0 +1,24 @@ +name: Terraform HCL validation (PRs only) + +on: + pull_request: + paths: ["**.tf"] + +jobs: + terraform-module-k3s: + name: Validate main Terraform module + uses: ./.github/worflows/terraform.lint.yaml + + examples_hcloud-k3s: + name: Hetzner Cloud + needs: [terraform-module-k3s] + uses: ./.github/worflows/terraform.lint.yaml + with: + terraform_workdir: examples/hcloud-k3s + + examples_civo-k3s: + name: CIVO + needs: [terraform-module-k3s] + uses: ./.github/worflows/terraform.lint.yaml + with: + terraform_workdir: examples/civo-k3s diff --git a/.github/workflows/terraform.lint.yaml b/.github/workflows/terraform.lint.yaml index 53b0963..41ddfc9 100644 --- a/.github/workflows/terraform.lint.yaml +++ b/.github/workflows/terraform.lint.yaml @@ -1,27 +1,50 @@ -name: Terraform HCL validation (PRs only) +--- +name: Terraform HCL validation on: - pull_request: - paths: ["**.tf"] + workflow_call: + inputs: + terraform_workdir: + description: Working directory where Terraform files are + required: false + default: "." + type: string -permissions: - pull-requests: write +env: + TF_IN_AUTOMATION: "yes" jobs: - terraform-module-k3s: - name: Terraform module - uses: xunleii/github-actions-grimoire/.github/workflows/terraform.pull_requests.lint.yaml@0ab2cd93e86642397ecdfb2da1e5b97594ab9905 + # Checks if your TF files are in a canonical format and without HCL issues + terraform_validate: + name: Terraform files validation + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3.1.0 + - uses: hashicorp/setup-terraform@bbe167fbdaa1a3bd046bdd70eba9dd3dddcca99c # tag=v2.0.2 + with: + terraform_version: ${{ inputs.terraform_version }} + - uses: terraform-linters/setup-tflint@ba6bb2989f94daf58a4cc6eac2c1ca7398a678bf # tag=v3.0.0 - examples_hcloud-k3s: - name: Hetzner Cloud - needs: [terraform-module-k3s] - uses: xunleii/github-actions-grimoire/.github/workflows/terraform.pull_requests.lint.yaml@0ab2cd93e86642397ecdfb2da1e5b97594ab9905 - with: - terraform_workdir: examples/hcloud-k3s + # --- `terraform fmt` + - name: Check if all Terraform configuration files are in a canonical format ${{ steps.pre.outputs.workdir }} + id: fmt + run: terraform fmt -check -recursive -diff -no-color + working-directory: ${{ inputs.terraform_workdir }} - examples_civo-k3s: - name: CIVO - needs: [terraform-module-k3s] - uses: xunleii/github-actions-grimoire/.github/workflows/terraform.pull_requests.lint.yaml@0ab2cd93e86642397ecdfb2da1e5b97594ab9905 - with: - terraform_workdir: examples/civo-k3s + # --- `terraform init` + - name: Initialize Terraform working directory ${{ steps.pre.outputs.workdir }} + id: init + run: terraform init -no-color -backend=false + working-directory: ${{ inputs.terraform_workdir }} + + # --- `terraform validate` + - name: Validate the configuration files ${{ steps.pre.outputs.workdir }} + id: validate + run: terraform validate -no-color + working-directory: ${{ inputs.terraform_workdir }} + + # --- `terraform lint` + - name: Lint the configuration files ${{ steps.pre.outputs.workdir }} + id: lint + run: tflint --format compact + working-directory: ${{ inputs.terraform_workdir }} diff --git a/.github/workflows/terraform.plan.yaml b/.github/workflows/terraform.plan.pull_request.yaml similarity index 100% rename from .github/workflows/terraform.plan.yaml rename to .github/workflows/terraform.plan.pull_request.yaml diff --git a/.github/workflows/terraform.test.yaml b/.github/workflows/terraform.test.yaml new file mode 100644 index 0000000..e91f608 --- /dev/null +++ b/.github/workflows/terraform.test.yaml @@ -0,0 +1,25 @@ +name: vagrant-up + +on: [push] + +defaults: + run: + working-directory: examples/vagrant-k3s + +jobs: + vagrant-up: + runs-on: macos-12 + + steps: + - name: "Checkout" + uses: actions/checkout@v3 + - name: Show Vagrant version + run: vagrant --version + - uses: hashicorp/setup-terraform@v2 + - run: vagrant up + # - run: terraform init + # - run: terraform apply -auto-approve + # env: + # TF_LOG: TRACE + # - run: terraform output -json + # - run: terraform destroy -auto-approve diff --git a/examples/vagrant-k3s/VagrantFile b/examples/vagrant-k3s/VagrantFile new file mode 100644 index 0000000..2acf254 --- /dev/null +++ b/examples/vagrant-k3s/VagrantFile @@ -0,0 +1,26 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : +# + +Vagrant.configure("2") do |config| + config.vm.box = 'generic/ubuntu2204' + config.vm.boot_timeout = ENV['TEST_VM_BOOT_TIMEOUT'] || 600 # seconds + config.vm.synced_folder '.', '/vagrant', disabled: true + + ['k3s-server-01', 'k3s-server-02', 'k3s-server-03', 'k3s-agent-01', 'k3s-agent-02'].each do |name| + config.vm.define name do |vm| + vm.vm.hostname = name + end + end + + %w[libvirt virtualbox vmware_desktop].each do |p| + config.vm.provider p do |v| + v.cpus = ENV['TEST_VM_CPUS'] || 1 + v.memory = ENV['TEST_VM_MEMORY'] || 512 + end + end + config.vm.provider :virtualbox do |v,o| + v.gui = false + v.check_guest_additions = false + end +end \ No newline at end of file diff --git a/examples/vagrant-k3s/main.tf b/examples/vagrant-k3s/main.tf new file mode 100644 index 0000000..4c3a8d1 --- /dev/null +++ b/examples/vagrant-k3s/main.tf @@ -0,0 +1,11 @@ +resource "vagrant_vm" "k3s_nodes" { + get_ports = true +} + +output "debug_sshconfig" { + value = vagrant_vm.k3s_nodes.ssh_config +} + +output "debug_ports" { + value = vagrant_vm.k3s_nodes.ports +} diff --git a/examples/vagrant-k3s/versions.tf b/examples/vagrant-k3s/versions.tf new file mode 100644 index 0000000..4fc4e5b --- /dev/null +++ b/examples/vagrant-k3s/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + vagrant = { + source = "bmatcuk/vagrant" + version = "4.1.0" + } + } +}