Skip to content
Merged
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
46 changes: 46 additions & 0 deletions .github/workflows/terraform-qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
on:
push:
exclude-branches:
- main
pull_request:
branches:
- main

env:
AWS_REGION: ${{ vars.AWS_REGION }}
AWS_ENDPOINT_URL_S3: ${{ vars.AWS_ENDPOINT_URL_S3 }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
GITHUB_OWNER: ${{ vars.GH_OWNER }}
GITHUB_APP_ID: ${{ vars.GH_APP_ID }}
GITHUB_APP_INSTALLATION_ID: ${{ vars.GH_APP_INSTALLATION_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.GH_APP_PEM_FILE }}
TF_WORKSPACE: ${{ vars.GH_OWNER }}
TF_VAR_path: ${{ '../test.yaml' }}

jobs:
terraform-qa:
name: "Terraform QA"
runs-on: ubuntu-latest
defaults:
run:
shell: bash

steps:
- name: Checkout repository
uses: actions/checkout@v5

- name: Setup Terraform with specified version
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.13.3

- name: Terraform init
run: terraform -chdir=terraform init

- name: Terraform format check
run: terraform -chdir=terraform fmt -check -recursive -no-color

- name: Terraform run tests
run: terraform -chdir=terraform test -no-color
6 changes: 3 additions & 3 deletions .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ jobs:

steps:
- name: Checkout called repository
uses: actions/checkout@v4
uses: actions/checkout@v5
with:
repository: 'xebis/github-organization-as-code'

- name: Checkout caller YAML configuration
if: github.repository != 'xebis/github-organization-as-code'
uses: actions/checkout@v4
uses: actions/checkout@v5
with:
path: iac
sparse-checkout: ${{ inputs.path || 'test.yaml' }}
Expand All @@ -71,7 +71,7 @@ jobs:
- name: Setup Terraform with specified version
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.11.0
terraform_version: 1.13.3

- name: Terraform init
id: init
Expand Down
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Dotenv files
.env*
# Direnv files
.env
.envrc

# Private Keys
*.pem

# Local .terraform directories
**/.terraform/*
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ terraform -chdir=terraform plan
terraform -chdir=terraform apply
```

## Development

Format Terraform configuration by `terraform -chdir=terraform fmt -recursive`.

## Testing

### Terraform Test

Run test scenarios in [`terraform/tests`](terraform/tests/) by `terraform -chdir=terraform test`.

### End-to-end Testing

This repository is tested using [`test.yaml`](test.yaml) as the configuration file for the [Xebis Test GitHub Organization](https://github.com/xebis-test) settings and repositories.

The workflow is designed to post a Terraform plan as a pull request comment whenever a pull request to the main branch is created or whenever a new commit to the pull request is pushed. Once the pull request is merged into `main`, the plan is applied automatically.
Expand Down
8 changes: 4 additions & 4 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
locals {
config = yamldecode(file(var.path))
all_repositories = try(local.config.organization.all-repositories, null)
repositories = local.config.repositories
all_repositories = try(local.config.organization.all-repositories, [])
repositories = try(local.config.repositories, [])
all_repositories_rulesets = [
for pair in setproduct(local.repositories, local.all_repositories.rulesets) : {
repository = pair[0]
for pair in try(setproduct(local.repositories, local.all_repositories.rulesets), []) : {
repository = pair[0],
ruleset = pair[1]
}
]
Expand Down
12 changes: 12 additions & 0 deletions terraform/tests/empty.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
run "empty_file" {
command = plan

variables {
path = "tests/fixtures/empty.yaml"
}

assert {
condition = local.config == null
error_message = "Expected empty file to produce empty result."
}
}
1 change: 1 addition & 0 deletions terraform/tests/fixtures/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---