Skip to content

Commit

Permalink
Add Terraform template (#18)
Browse files Browse the repository at this point in the history
* feat: added terraform template

* feat(workflow): add manual trigger for Terraform GitHub Actions workflow

- Added `workflow_dispatch` to allow manual triggering of the workflow.
- This change enhances the flexibility of the CI/CD pipeline by enabling on-demand execution.

* refactor: update build configurations and flake.nix for Go module

- Removed `goPackage` and `buildGoModule` parameters from `build-configs.yaml`.
- Updated `flake.nix` to use `buildGoModule` instead of `buildGo122Module`.
- Added `version` and `CgoEnabled` fields to `GoCobraCliConfig` in `go_cobra_cli.go`.
- Updated `internal/templates/templates/go-cobra-cli/flake.nix` to include `version` and `CgoEnabled` fields.

* feat: added terraform-module template
  • Loading branch information
erikreinert authored Jul 13, 2024
1 parent 2380f63 commit bb656e7
Show file tree
Hide file tree
Showing 24 changed files with 619 additions and 104 deletions.
16 changes: 16 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base",
":semanticCommitTypeAll(chore)"
],
"lockFileMaintenance": {
"enabled": true,
"extends": [
"schedule:weekly"
]
},
"nix": {
"enabled": true
}
}
8 changes: 6 additions & 2 deletions .github/workflows/flake.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: DeterminateSystems/nix-installer-action@main
- uses: cachix/install-nix-action@v25
with:
enable_kvm: true
- uses: cachix/cachix-action@v14
with:
authToken: ${{ secrets.ALTF4LLC_CACHIX_AUTH_TOKEN }}
Expand All @@ -26,7 +28,9 @@ jobs:
- check
runs-on: ubuntu-latest
steps:
- uses: DeterminateSystems/nix-installer-action@main
- uses: cachix/install-nix-action@v25
with:
enable_kvm: true
- uses: cachix/cachix-action@v14
with:
authToken: ${{ secrets.ALTF4LLC_CACHIX_AUTH_TOKEN }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.direnv
/.direnv
/build-configs
/result
/target
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
issues:
exclude:
- Error return value of `\(github.com/go-kit/log.Logger\).Log` is not checked

2 changes: 0 additions & 2 deletions build-configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ parameters:
cachix:
binaryCache: altf4llc-os
vendorHash: sha256-6B9O6ho4COpJy4HlkzQ0lk+ieezRO3xg9LyLHzoxYzc=
goPackage: go_1_22
buildGoModule: buildGo122Module
65 changes: 25 additions & 40 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";

outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
outputs = inputs @ {flake-parts, ...}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];

perSystem = { config, pkgs, ... }:
let
inherit (pkgs)
go_1_22
just;

name = "build-configs";
version = "0.1.0";
CGO_ENABLED = "0";
in
{
devShells.default = pkgs.mkShell {
buildInputs = [ just ];
inputsFrom = [ config.packages.default ];
};

packages = {
default = pkgs.buildGo122Module {
inherit name version;
GOFLAGS = [
"-ldflags=github.com/ALT-F4-LLC/build-configs/internal/cli.Version=${version}"
];
src = ./.;
vendorHash = "sha256-6B9O6ho4COpJy4HlkzQ0lk+ieezRO3xg9LyLHzoxYzc=";
buildModules = [ "cmd/${name}" ];
};
perSystem = {
config,
pkgs,
...
}: let
inherit (pkgs) just;
name = "build-configs";
version = "0.1.0";
CGO_ENABLED = "0";
in {
devShells.default = pkgs.mkShell {
buildInputs = [just];
inputsFrom = [config.packages.default];
};

docker = pkgs.dockerTools.buildImage {
inherit name;
tag = "latest";
config = {
Entrypoint = [ "${config.packages.default}/bin/${name}" ];
Env = [
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
};
packages = {
default = pkgs.buildGoModule {
inherit CGO_ENABLED name version;
src = ./.;
subPackages = ["cmd/${name}"];
vendorHash = "sha256-6B9O6ho4COpJy4HlkzQ0lk+ieezRO3xg9LyLHzoxYzc=";
};
};
};
};
};
}
36 changes: 36 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ func (c Config) GetTemplater() (Templater, error) {
return tpl, err
}

// Then convert them back into the type for the templater selected
if err := json.Unmarshal(b, &tpl); err != nil {
return tpl, err
}
return tpl, nil

case "terraform":
if Debug {
fmt.Println("loading terraform templater")
}
tpl := NewTerraformConfig(c)

// Convert the parameters (map type) to JSON
b, err := json.Marshal(c.Parameters)
if err != nil {
return tpl, err
}

// Then convert them back into the type for the templater selected
if err := json.Unmarshal(b, &tpl); err != nil {
return tpl, err
}
return tpl, nil

case "terraform-module":
if Debug {
fmt.Println("loading terraform module templater")
}
tpl := NewTerraformModuleConfig(c)

// Convert the parameters (map type) to JSON
b, err := json.Marshal(c.Parameters)
if err != nil {
return tpl, err
}

// Then convert them back into the type for the templater selected
if err := json.Unmarshal(b, &tpl); err != nil {
return tpl, err
Expand Down
17 changes: 10 additions & 7 deletions internal/config/go_cobra_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ const GoCobraCliName = "go-cobra-cli"

type GoCobraCliConfig struct {
Config
CgoEnabled bool `json:"cgoEnabled,omitempty" yaml:"cgoEnabled,omitempty"`
GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
Lint GolangCILintConfig `json:"lint,omitempty" yaml:"lint,omitempty"`
Nix NixGoConfig `json:"nix,omitempty" yaml:"nix,omitempty"`
PrivateModules string `json:"privateModules,omitempty" yaml:"privateModules,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
}

func NewGoCobraCliConfig(c Config) GoCobraCliConfig {
return GoCobraCliConfig{
Config: c,

GoVersion: "1.22",

Lint: NewGolangCiLintConfig(),
CgoEnabled: false,
Config: c,
GoVersion: "1.22",
Lint: NewGolangCiLintConfig(),
Nix: NixGoConfig{
NixConfig: NewNixConfig(),
GoPackage: "go",
BuildGoModule: "buildGoModule",
GoPackage: "go",
NixConfig: NewNixConfig(),
},
Version: "0.1.0",
}
}

func (c GoCobraCliConfig) Render() error {
files, err := templates.RenderTemplates(templates.RenderMap{
templates.AllCommonTemplates: {
".envrc",
".github/renovate.json",
},
templates.GoCommonTemplates: {
".editorconfig",
Expand Down
1 change: 1 addition & 0 deletions internal/config/go_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c GoLambdaConfig) Render() error {
renderMap := templates.RenderMap{
templates.AllCommonTemplates: {
".envrc",
".github/renovate.json",
},
templates.GoCommonTemplates: {
".editorconfig",
Expand Down
62 changes: 62 additions & 0 deletions internal/config/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

import (
"fmt"

"github.com/ALT-F4-LLC/build-configs/internal/templates"
)

const TerraformName = "terraform"

type TerraformConfigRole struct {
PlanARN string `json:"planArn,omitempty" yaml:"planArn,omitempty"`
ApplyARN string `json:"applyArn,omitempty" yaml:"applyArn,omitempty"`
}

type TerraformConfig struct {
Config
Nix NixConfig `json:"nix,omitempty" yaml:"nix,omitempty"`
Region string `json:"region,omitempty" yaml:"region,omitempty"`
Role TerraformConfigRole `json:"role,omitempty" yaml:"role,omitempty"`
Schedule *string `json:"schedule,omitempty" yaml:"schedule,omitempty"`
Providers []string `json:"providers,omitempty" yaml:"providers,omitempty"`
}

func NewTerraformConfigRole(name string) TerraformConfigRole {
return TerraformConfigRole{
ApplyARN: fmt.Sprintf("arn:aws:iam::677459762413:role/altf4llc-gha-%s-apply", name),
PlanARN: fmt.Sprintf("arn:aws:iam::677459762413:role/altf4llc-gha-%s-plan", name),
}
}

func NewTerraformConfig(c Config) TerraformConfig {
return TerraformConfig{
Config: c,
Nix: NewNixConfig(),
Region: "us-west-2",
Role: NewTerraformConfigRole(c.Name),
Schedule: nil,
}
}

func (c TerraformConfig) Render() error {
renderMap := templates.RenderMap{
templates.AllCommonTemplates: {
".envrc",
".github/renovate.json",
},
templates.TerraformTemplates: {
".github/workflows/terraform.yaml",
".gitignore",
"flake.nix",
"justfile",
},
}

files, err := templates.RenderTemplates(renderMap, c)
if err != nil {
return err
}

return templates.WriteFiles(files)
}
42 changes: 42 additions & 0 deletions internal/config/terraform_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"github.com/ALT-F4-LLC/build-configs/internal/templates"
)

const TerraformModuleName = "terraform-module"

type TerraformModuleConfig struct {
Config
Nix NixConfig `json:"nix,omitempty" yaml:"nix,omitempty"`
Providers []string `json:"providers,omitempty" yaml:"providers,omitempty"`
}

func NewTerraformModuleConfig(c Config) TerraformModuleConfig {
return TerraformModuleConfig{
Config: c,
Nix: NewNixConfig(),
}
}

func (c TerraformModuleConfig) Render() error {
renderMap := templates.RenderMap{
templates.AllCommonTemplates: {
".envrc",
".github/renovate.json",
},
templates.TerraformModuleTemplates: {
".github/workflows/terraform.yaml",
".gitignore",
"flake.nix",
"justfile",
},
}

files, err := templates.RenderTemplates(renderMap, c)
if err != nil {
return err
}

return templates.WriteFiles(files)
}
20 changes: 15 additions & 5 deletions internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ var (
//go:embed all:templates/go-lambda/*
goLambdaFS embed.FS

AllCommonTemplates *template.Template
GoCommonTemplates *template.Template
GoCobraCliTemplates *template.Template
GoLambdaTemplates *template.Template
//go:embed all:templates/terraform/*
terraformFS embed.FS

//go:embed all:templates/terraform-module/*
terraformModuleFS embed.FS

AllCommonTemplates *template.Template
GoCommonTemplates *template.Template
GoCobraCliTemplates *template.Template
GoLambdaTemplates *template.Template
TerraformTemplates *template.Template
TerraformModuleTemplates *template.Template
)

// RenderMap maps a template set to the filenames* that should be written.
Expand All @@ -38,6 +46,8 @@ func init() {
GoCommonTemplates = template.Must(template.ParseFS(goCommonFS, "templates/common/go/*"))
GoCobraCliTemplates = template.Must(template.ParseFS(goCobraCliFS, "templates/go-cobra-cli/*"))
GoLambdaTemplates = template.Must(template.ParseFS(goLambdaFS, "templates/go-lambda/*"))
TerraformTemplates = template.Must(template.ParseFS(terraformFS, "templates/terraform/*"))
TerraformModuleTemplates = template.Must(template.ParseFS(terraformModuleFS, "templates/terraform-module/*"))
}

func RenderTemplates(in RenderMap, context any) (map[string]string, error) {
Expand Down Expand Up @@ -81,7 +91,7 @@ func WriteFiles(in map[string]string) error {
continue
}

if err := os.WriteFile(filename, []byte(contents), 0644); err != nil {
if err := os.WriteFile(filename, []byte(contents), 0o644); err != nil {
return err
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/templates/templates/common/all/.github__renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base",
":semanticCommitTypeAll(chore)"
],
"lockFileMaintenance": {
"enabled": true,
"extends": [
"schedule:weekly"
]
},
"nix": {
"enabled": true
}
}
Loading

0 comments on commit bb656e7

Please sign in to comment.