Skip to content

Commit 5080276

Browse files
committed
feat: make the terraform workflow reusable
1 parent 0a01cae commit 5080276

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

.github/workflows/terraform.yaml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,45 @@ on:
55
push:
66
branches:
77
- main
8+
workflow_call:
9+
inputs:
10+
aws_endpoint_url_s3:
11+
type: string
12+
required: true
13+
gh_owner:
14+
type: string
15+
required: true
16+
gh_app_id:
17+
type: string
18+
required: true
19+
gh_app_installation_id:
20+
type: string
21+
required: true
22+
path:
23+
type: string
24+
required: true
25+
secrets:
26+
aws_access_key_id:
27+
required: true
28+
aws_secret_access_key:
29+
required: true
30+
gh_app_pem_file:
31+
required: true
832

933
env:
1034
# S3 backend configuration
11-
AWS_ENDPOINT_URL_S3: ${{ vars.AWS_ENDPOINT_URL_S3 }}
12-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
13-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
35+
AWS_ENDPOINT_URL_S3: ${{ inputs.aws_endpoint_url_s3 || vars.AWS_ENDPOINT_URL_S3 }}
36+
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id || secrets.AWS_ACCESS_KEY_ID }}
37+
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key || secrets.AWS_SECRET_ACCESS_KEY }}
1438
# GitHub App configuration
15-
GITHUB_OWNER: ${{ vars.GH_TF_OWNER }}
16-
GITHUB_APP_ID: ${{ vars.GH_TF_APP_ID }}
17-
GITHUB_APP_INSTALLATION_ID: ${{ vars.GH_TF_APP_INSTALLATION_ID }}
39+
GITHUB_OWNER: ${{ inputs.gh_owner || vars.GH_TF_OWNER }}
40+
GITHUB_APP_ID: ${{ inputs.gh_app_id || vars.GH_TF_APP_ID }}
41+
GITHUB_APP_INSTALLATION_ID: ${{ inputs.gh_app_installation_id || vars.GH_TF_APP_INSTALLATION_ID }}
1842
GITHUB_APP_PEM_FILE: |
19-
${{ secrets.GH_TF_APP_PEM_FILE }}
43+
${{ secrets.gh_app_pem_file || secrets.GH_TF_APP_PEM_FILE }}
2044
# Terraform configuration
21-
TF_WORKSPACE: ${{ vars.GH_TF_OWNER }}
22-
TF_VAR_config: "../test.yaml"
45+
TF_WORKSPACE: ${{ inputs.gh_owner || vars.GH_TF_OWNER }}
46+
TF_VAR_config: ${{ inputs.path && format('../config/{0}', inputs.path) || '../test.yaml' }}
2347
TF_IN_AUTOMATION: true
2448

2549
defaults:
@@ -31,8 +55,17 @@ jobs:
3155
name: Terraform Apply
3256
runs-on: ubuntu-latest
3357
steps:
34-
- name: Checkout the repository
58+
- name: Checkout the called repository
3559
uses: actions/checkout@v6
60+
with:
61+
repository: bruzit/github-organization-as-code
62+
- name: Checkout the caller repository
63+
if: github.repository != 'bruzit/github-organization-as-code'
64+
uses: actions/checkout@v6
65+
with:
66+
path: config
67+
sparse-checkout: ${{ inputs.path || 'test.yaml' }}
68+
sparse-checkout-cone-mode: false
3669
- name: Set up Terraform
3770
uses: hashicorp/setup-terraform@v4
3871
with:

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ GitHub organization managed as code.
55
## Features
66

77
- **Automated GitHub Organization management** - Define repositories using simple YAML file.
8-
- **GitOps Workflow** - Manage configurations using pull requests and automate updates using GitHub Actions.
8+
- **Reusable GitOps Workflow** - Manage configurations using pull requests and automate updates using GitHub Actions.
99
- **Terraform** - Uses Terraform under the hood to apply changes efficiently.
1010
- **Terraform State Management** - Stores Terraform state securely in AWS S3.
1111
- **GitHub App Integration** - Uses a GitHub App for authentication and API interactions.
@@ -46,7 +46,7 @@ To create a GitHub App and a GitHub App Installation:
4646

4747
Create GitHub organization YAML configuration file. See [GitHub Organization YAML](#github-organization-yaml) below.
4848

49-
For example:
49+
For example, `config.yaml`:
5050

5151
```yaml
5252
---
@@ -56,6 +56,30 @@ repositories:
5656
5757
### GitHub Workflow
5858
59+
Create the workflow:
60+
61+
```yaml
62+
---
63+
on:
64+
push:
65+
branches:
66+
- main
67+
68+
jobs:
69+
call-terraform:
70+
uses: bruzit/github-organization-as-code/.github/workflows/terraform.yaml@v0
71+
with:
72+
aws_endpoint_url_s3: ${{ vars.AWS_ENDPOINT_URL_S3 }}
73+
gh_owner: ${{ vars.GH_TF_OWNER }}
74+
gh_app_id: ${{ vars.GH_TF_APP_ID }}
75+
gh_app_installation_id: ${{ vars.GH_TF_APP_INSTALLATION_ID }}
76+
path: config.yaml
77+
secrets:
78+
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
79+
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
80+
gh_app_pem_file: ${{ secrets.GH_TF_APP_PEM_FILE }}
81+
```
82+
5983
Set up GitHub actions, variables and secrets:
6084
6185
- GitHub / *Repository* / Settings

0 commit comments

Comments
 (0)