Skip to content

Commit

Permalink
chore: add terraform scripts for membership management
Browse files Browse the repository at this point in the history
  • Loading branch information
tboerger committed Oct 30, 2023
1 parent a8f06a2 commit e975fff
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# http://editorconfig.org

root = true

[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[Makefile]
indent_style = tab
indent_size = 4

[*.{html,tmpl,py,go}]
indent_style = tab
indent_size = 4

[*.{yml,yaml,toml,json,css,less,js,vue,rb,nix}]
indent_style = space
indent_size = 2

[*.sh]
indent_style = space
indent_size = 4
1 change: 1 addition & 0 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ branches:
restrictions:
apps:
- renovate
- promhippie
users: []
teams:
- admins
Expand Down
67 changes: 67 additions & 0 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
name: general

"on":
workflow_dispatch:
schedule:
- cron: "0 5 * * *"
push:
branches:
- master
pull_request:
branches:
- master

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- name: Checkout source
id: source
uses: actions/checkout@v3

- name: Setup terraform
id: terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest

- name: Terraform format
id: fmt
run: bin/terraform fmt -no-color -check

- name: Terraform init
id: init
env:
GITHUB_TOKEN: ${{ secrets.INFRA_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: bin/terraform init -no-color -input=false

- name: Terraform validate
id: validate
env:
GITHUB_TOKEN: ${{ secrets.INFRA_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: bin/terraform validate -no-color

- name: Terraform plan
id: plan
env:
GITHUB_TOKEN: ${{ secrets.INFRA_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: bin/terraform plan -no-color -input=false

- name: Terraform apply
id: apply
env:
GITHUB_TOKEN: ${{ secrets.INFRA_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request'
run: bin/terraform apply -no-color -input=false -auto-approve

...
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.direnv
.terraform

*.retry
*.tfvars
1 change: 1 addition & 0 deletions .terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.5.4
13 changes: 10 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
source "https://rubygems.org"
gem "octokit", "~> 7.0"
gem "faraday-retry", "~> 2.2"
# frozen_string_literal: true

source 'https://rubygems.org'
gem 'faraday-retry', '~> 2.2'
gem 'octokit', '~> 7.0'

group :development, :test do
gem 'rake', '~> 13.0'
gem 'rubocop', '~> 1.28'
end
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
# Promhippie

[![General Workflow](https://github.com/promhippie/.github/actions/workflows/general.yml/badge.svg)](https://github.com/promhippie/.github/actions/workflows/general.yml) [![Join the Matrix chat at https://matrix.to/#/#webhippie:matrix.org](https://img.shields.io/badge/matrix-%23webhippie%3Amatrix.org-7bc9a4.svg)](https://matrix.to/#/#webhippie:matrix.org)

TBD

## Development

TBD

## Security

If you find a security issue please contact
[[email protected]](mailto:[email protected]) first.

## Contributing

Fork -> Patch -> Push -> Pull Request

## Authors

* [Thomas Boerger](https://github.com/tboerger)

## License

MIT
Apache-2.0

## Author
## Copyright

[Thomas Boerger](https://github.com/tboerger)
```console
Copyright (c) 2018 Thomas Boerger <[email protected]>
```
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop)

task default: %i[rubocop]
14 changes: 14 additions & 0 deletions bin/terraform
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

if [ -L "$0" ]; then
ROOT=$(cd "$(dirname "$(readlink -e "$0")")/../terraform" || exit; pwd)
else
ROOT=$(cd "$(dirname "$0")/../terraform" || exit; pwd)
fi

if ! which terraform >/dev/null; then
echo "Failed to detect terraform!"
exit 1
fi

terraform -chdir="${ROOT}" "$@"
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
{
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
terraform
ruby_3_2
shellcheck
];
};
}
Expand Down
24 changes: 24 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions terraform/members.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "github_membership" "general" {
for_each = { for row in var.members : row.name => row }

username = each.value.name
role = each.value.role
}
28 changes: 28 additions & 0 deletions terraform/repositories.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
data "github_repositories" "available" {
query = "org:promhippie"
}

locals {
repository_labels = flatten([
for repository in data.github_repositories.available.names : [
for index, label in var.labels : [
{
name = "${repository}-${label.name}"
repository = repository
label = label.name
description = label.description
color = label.color
}
]
]
])
}

resource "github_issue_label" "general" {
for_each = { for row in local.repository_labels : row.name => row }

repository = each.value.repository
name = each.value.label
description = each.value.description
color = each.value.color
}
Empty file added terraform/secrets.tf
Empty file.
43 changes: 43 additions & 0 deletions terraform/teams.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
locals {
team_repositories = flatten([
for repository in data.github_repositories.available.names : [
for index, team in var.teams : [
{
name = "${team.name}-${repository}"
team = team.name
repository = repository
permission = team.permission
}
]
]
])
}

resource "github_team" "general" {
for_each = { for row in var.teams : row.name => row }

name = each.value.name
privacy = each.value.privacy
}

resource "github_team_members" "general" {
for_each = { for row in var.teams : row.name => row }
team_id = github_team.general[each.value.name].id

dynamic "members" {
for_each = each.value.members

content {
username = members.value.name
role = members.value.role
}
}
}

resource "github_team_repository" "general" {
for_each = { for row in local.team_repositories : row.name => row }

team_id = github_team.general[each.value.team].id
repository = each.value.repository
permission = each.value.permission
}
20 changes: 20 additions & 0 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
backend "s3" {
bucket = "promhippie-terraform"
key = "github"
region = "eu-central-1"
}

required_providers {
github = {
source = "integrations/github"
version = "~> 5.32.0"
}
}

required_version = ">= 1.0"
}

provider "github" {
owner = "promhippie"
}
Loading

0 comments on commit e975fff

Please sign in to comment.