Skip to content

Commit c0af300

Browse files
committed
feat: use workspaces, file as an input, and test data
1 parent 3bd889a commit c0af300

File tree

7 files changed

+94
-28
lines changed

7 files changed

+94
-28
lines changed

.github/workflows/terraform.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ env:
1616
GITHUB_APP_ID: ${{ vars.APP_ID }}
1717
GITHUB_APP_INSTALLATION_ID: ${{ vars.APP_INSTALLATION_ID }}
1818
GITHUB_APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
19+
TF_WORKSPACE: ${{ vars.OWNER }}
20+
TF_VAR_path: test.yaml
1921

2022
jobs:
2123
terraform:

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GitHub Organization as Code
22

3-
Manage your GitHub organization repositories with GitOps principles using YAML configuration, GitHub Actions, AWS S3 storage, and GitHub App integration—powered by Terraform.
3+
Manage your GitHub organization's repositories using GitOps principles with a YAML-based configuration, GitHub Actions with reusable workflows, AWS S3 for storage, and GitHub App integration.
44

55
## Features
66

@@ -16,8 +16,31 @@ This repository was automatically created and is continuously managed using the
1616

1717
## Installation and Configuration
1818

19+
- Configure an AWS S3 bucket to store Terraform state files.
20+
- Set up a GitHub App and its installation to handle authentication and authorization for your GitHub Organization.
21+
- Implement GitOps by setting up a GitHub repository with:
22+
- YAML-based configuration
23+
- GitHub workflows
24+
- Repository variables and secrets
25+
26+
> [!caution]
27+
> The GitHub App PEM file, S3 API credentials, Terraform state, GitHub repository secrets, and configuration code are key security elements.
28+
29+
### Set Up AWS S3 Bucket
30+
1931
Set up an AWS S3 bucket or a compatible storage service.
2032

33+
> [!important]
34+
> Ensure you have the following details ready:
35+
>
36+
> - Bucket Name
37+
> - Access Key ID
38+
> - Secret Access Key
39+
> - Region
40+
> - S3 Endpoint URL (only required for non-AWS S3-compatible services)
41+
42+
### Set Up GitHub Organizations
43+
2144
Create a GitHub App:
2245

2346
- GitHub / *Organization* / Settings / Developer Settings / GitHub Apps / **New GitHub App**
@@ -48,6 +71,37 @@ Get the GitHub App credentials:
4871

4972
- GitHub / *Organization* / Settings / Developer Settings / GitHub Apps / *Your GitHub App name* / General / Private keys / **Generate a private key**
5073

74+
> [!important]
75+
> Ensure you have the following details ready:
76+
>
77+
> - GitHub Owner
78+
> - GitHub App ID
79+
> - GitHub App Installation ID
80+
> - GitHub App PEM File
81+
82+
### Set Up GitHub Repository for GitHub Organization Management
83+
84+
Create GitHub organization YAML configuration file. See [GitHub Organization YAML](#github-organization-yaml) below.
85+
86+
For example:
87+
88+
```yaml
89+
---
90+
repositories:
91+
- name: .github
92+
description: The organization profile.
93+
topics:
94+
- github-organization-profile
95+
- github-profile
96+
- github-profile-readme
97+
```
98+
99+
Create GitHub workflow planning and applying configuration changes to the GitHub Organization:
100+
101+
```yaml
102+
#TODO
103+
```
104+
51105
Set up GitHub actions, variables and secrets:
52106

53107
- GitHub / *Repository* / Settings
@@ -67,12 +121,14 @@ Set up GitHub actions, variables and secrets:
67121
- `AWS_REGION`
68122
- `OWNER` (`GITHUB_OWNER`)
69123

70-
> [!caution]
71-
> The GitHub App PEM file, S3 API credentials, Terraform state, GitHub repository secrets, and configuration code are key security elements.
72-
73124
## Usage
74125

75-
Edit the GitHub organization YAML configuration [`gh-org.yaml`](gh-org.yaml):
126+
The GitHub organization YAML configuration 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.
127+
128+
> [!note]
129+
> The state is stored as JSON object `github/<github owner>/terraform.tfstate` in the bucket.
130+
131+
### GitHub Organization YAML
76132

77133
```yaml
78134
---
@@ -96,7 +152,7 @@ repositories:
96152
97153
Defaults are the same as in the Terraform provider `github` resource `github_repository`, see [Terraform Registry / Providers / integrations / github / resources / github_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository#argument-reference).
98154

99-
Modify the Terraform backend configuration in [`config.tf`](config.tf) as needed.
155+
### Local Usage
100156

101157
Apply the configuration using Terraform:
102158

@@ -111,11 +167,20 @@ export GITHUB_APP_ID=<app-id>
111167
export GITHUB_APP_INSTALLATION_ID=<app-installation-id>
112168
export GITHUB_APP_PEM_FILE=$(cat <app-private-key.pem>)
113169
170+
export TF_WORKSPACE="$GITHUB_OWNER"
171+
export TF_VAR_path="test.yaml"
172+
114173
terraform init
115174
terraform plan
116175
terraform apply
117176
```
118177

178+
## Testing
179+
180+
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.
181+
182+
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.
183+
119184
## Credits and Acknowledgments
120185

121186
- Martin Bružina - Author

config.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ terraform {
88
}
99

1010
backend "s3" {
11-
bucket = "xebis-terraform"
12-
key = "github-xebis"
13-
use_lockfile = true # Set to false only for non-AWS S3 compatible APIs without "conditional object PUTs" capability
11+
bucket = "xebis-terraform"
12+
key = "terraform.tfstate"
13+
workspace_key_prefix = "github"
14+
use_lockfile = true # Set to false only for non-AWS S3 compatible APIs without "conditional object PUTs" capability
1415

1516
# Only for non-AWS S3 compatible APIs
1617
skip_credentials_validation = true

gh-org.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

input.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable "path" {
2+
type = string
3+
description = "GitHub Organization configuration YAML"
4+
validation {
5+
condition = fileexists(var.path)
6+
error_message = "File ${var.path} doesn't exist."
7+
}
8+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
config = yamldecode(file("gh-org.yaml"))
2+
config = yamldecode(file(var.path))
33
default_properties = try(local.config.default-properties, null)
44
repositories = local.config.repositories
55
}

test.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
repositories:
3+
- name: .github
4+
description: Xebis Test organization profile.
5+
topics:
6+
- github-organization-profile
7+
- github-profile
8+
- github-profile-readme

0 commit comments

Comments
 (0)