|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + validate: |
| 14 | + name: Validate (analyzer, fmt, validate, test, tflint, trivy) |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - uses: hashicorp/setup-terraform@v3 |
| 20 | + with: |
| 21 | + terraform_version: "~> 1.9" |
| 22 | + |
| 23 | + - name: PSScriptAnalyzer |
| 24 | + shell: pwsh |
| 25 | + run: | |
| 26 | + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted |
| 27 | + Install-Module PSScriptAnalyzer -MinimumVersion 1.21.0 -Force -Scope CurrentUser |
| 28 | + $results = Invoke-ScriptAnalyzer -Path ./Sort-LdoTerraform.ps1 -Settings ./PSScriptAnalyzerSettings.psd1 |
| 29 | + if ($results) { $results | Format-Table -AutoSize | Out-String | Write-Host } |
| 30 | + $errors = @($results | Where-Object { $_.Severity -eq 'Error' }) |
| 31 | + if ($errors.Count -gt 0) { throw "PSScriptAnalyzer found $($errors.Count) error(s)." } |
| 32 | + Write-Host 'PSScriptAnalyzer: clean.' |
| 33 | +
|
| 34 | + - name: Terraform fmt |
| 35 | + run: terraform fmt -check -recursive |
| 36 | + |
| 37 | + - name: Validate and test the module and examples |
| 38 | + run: | |
| 39 | + set -euo pipefail |
| 40 | + for d in . examples/minimal examples/complete; do |
| 41 | + echo "== ${d} ==" |
| 42 | + terraform -chdir="${d}" init -backend=false -input=false >/dev/null |
| 43 | + terraform -chdir="${d}" validate |
| 44 | + done |
| 45 | + terraform test |
| 46 | +
|
| 47 | + - name: Setup TFLint |
| 48 | + uses: terraform-linters/setup-tflint@b480b8fcdaa6f2c577f8e4fa799e89e756bb7c93 # v6.2.2 |
| 49 | + |
| 50 | + - name: TFLint (examples) |
| 51 | + run: | |
| 52 | + set -euo pipefail |
| 53 | + for d in examples/*/; do |
| 54 | + echo "== ${d%/} ==" |
| 55 | + ( cd "${d%/}" && tflint --init && tflint --recursive ) |
| 56 | + done |
| 57 | +
|
| 58 | + # The trivyignores input is avoided on purpose: trivy-action concatenates the listed files |
| 59 | + # into an extension-less temp file, and trivy decides the ignore-file format by extension, |
| 60 | + # so a YAML waiver file silently stops matching. The TRIVY_IGNOREFILE env var hands trivy |
| 61 | + # the file directly, extension intact. Trivy hard-fails when the file is missing, so the |
| 62 | + # repo always carries at least the stub .trivyignore.yaml. |
| 63 | + - name: Trivy config scan (report CRITICAL to LOW, non-blocking) |
| 64 | + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
| 65 | + env: |
| 66 | + TRIVY_IGNOREFILE: .trivyignore.yaml |
| 67 | + with: |
| 68 | + scan-type: config |
| 69 | + scan-ref: . |
| 70 | + severity: CRITICAL,HIGH,MEDIUM,LOW |
| 71 | + exit-code: "0" |
| 72 | + |
| 73 | + - name: Trivy config gate (fail on HIGH or CRITICAL) |
| 74 | + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
| 75 | + env: |
| 76 | + TRIVY_IGNOREFILE: .trivyignore.yaml |
| 77 | + with: |
| 78 | + scan-type: config |
| 79 | + scan-ref: . |
| 80 | + severity: HIGH,CRITICAL |
| 81 | + exit-code: "1" |
| 82 | + |
| 83 | + # Live apply-then-destroy of an example via the terraform-azure action. Runs only on a manual |
| 84 | + # dispatch, since it needs the org OIDC variables, the firewalled state account, and the org |
| 85 | + # secrets that the tenant bootstrap publishes. Trigger with: gh workflow run ci.yml |
| 86 | + self-test: |
| 87 | + name: Self-test (plan, apply, destroy) |
| 88 | + if: ${{ github.event_name == 'workflow_dispatch' }} |
| 89 | + needs: validate |
| 90 | + runs-on: ubuntu-latest |
| 91 | + permissions: |
| 92 | + id-token: write |
| 93 | + contents: read |
| 94 | + steps: |
| 95 | + - uses: actions/checkout@v4 |
| 96 | + |
| 97 | + - name: Plan and apply the example stacks |
| 98 | + uses: libre-devops/terraform-azure@v1 |
| 99 | + with: |
| 100 | + terraform-code-location: examples |
| 101 | + terraform-stack-to-run-json: '["minimal","complete"]' |
| 102 | + run-terraform-plan: true |
| 103 | + run-terraform-apply: true |
| 104 | + arm-client-id: ${{ vars.AZURE_CLIENT_ID }} |
| 105 | + arm-tenant-id: ${{ vars.AZURE_TENANT_ID }} |
| 106 | + arm-subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }} |
| 107 | + firewall-storage-account-name: ${{ secrets.TFSTATE_STORAGE_ACCOUNT }} |
| 108 | + firewall-storage-resource-group: ${{ secrets.TFSTATE_RESOURCE_GROUP }} |
| 109 | + terraform-init-extra-args-json: '["-reconfigure","-upgrade","-backend-config=resource_group_name=${{ secrets.TFSTATE_RESOURCE_GROUP }}","-backend-config=storage_account_name=${{ secrets.TFSTATE_STORAGE_ACCOUNT }}","-backend-config=container_name=${{ secrets.TFSTATE_BLOB_CONTAINER }}"]' |
| 110 | + |
| 111 | + # ALWAYS runs, even when the apply step failed: a partial apply must still be torn down |
| 112 | + # rather than left for a human to notice. A destroy against a half-applied or empty state |
| 113 | + # is safe (it removes whatever landed). |
| 114 | + - name: Plan-destroy and destroy the example stacks |
| 115 | + if: ${{ always() }} |
| 116 | + uses: libre-devops/terraform-azure@v1 |
| 117 | + with: |
| 118 | + terraform-code-location: examples |
| 119 | + terraform-stack-to-run-json: '["minimal","complete"]' |
| 120 | + run-terraform-plan: false |
| 121 | + run-terraform-plan-destroy: true |
| 122 | + run-terraform-destroy: true |
| 123 | + arm-client-id: ${{ vars.AZURE_CLIENT_ID }} |
| 124 | + arm-tenant-id: ${{ vars.AZURE_TENANT_ID }} |
| 125 | + arm-subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }} |
| 126 | + firewall-storage-account-name: ${{ secrets.TFSTATE_STORAGE_ACCOUNT }} |
| 127 | + firewall-storage-resource-group: ${{ secrets.TFSTATE_RESOURCE_GROUP }} |
| 128 | + terraform-init-extra-args-json: '["-reconfigure","-upgrade","-backend-config=resource_group_name=${{ secrets.TFSTATE_RESOURCE_GROUP }}","-backend-config=storage_account_name=${{ secrets.TFSTATE_STORAGE_ACCOUNT }}","-backend-config=container_name=${{ secrets.TFSTATE_BLOB_CONTAINER }}"]' |
0 commit comments