Skip to content

Commit

Permalink
fix: make deploy a slice, use it to config multiple environments (#12)
Browse files Browse the repository at this point in the history
* fix: make deploy a slice, use it to config multiple environments

* docs: add info on the .bcignore file

* fix: update deploy default role ARN to reflect standard GHA role naming
  • Loading branch information
hbjydev authored May 9, 2024
1 parent fdf9795 commit 2ec29ea
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 32 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ file present. See [Examples](#examples) for help with this.
$ build-configs generate
```

It is also possible to configure build-configs to leave otherwise templated be,
similarly to the `.gitignore` file in a Git repository. The file used is called
`.bcignore`, and takes a direct list of paths (globs are currently unsupported)
to skip templating:

```gitignore
# ignore the flake, we customize it further.
flake.nix
```

## Examples

Some example configurations for our template types exist in the
Expand Down
3 changes: 3 additions & 0 deletions examples/go-lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/*
!.gitignore
!build-configs.yaml
3 changes: 2 additions & 1 deletion examples/go-lambda/build-configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ parameters:
quirk:
service: lambdatest
deploy:
roleArn: 'arn:aws:iam::1234567890:role/gha-lambda-test-deploy-dev-role'
- environment: dev
if: github.ref == 'refs/heads/main'
nix:
vendorHash: ''
goPackage: go_1_22
Expand Down
4 changes: 0 additions & 4 deletions internal/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ var generateCmd = &cobra.Command{
return fmt.Errorf("could not create templater: %v", err)
}

if debug {
fmt.Printf("%+v\n", t)
}

return t.Render()
},
}
Expand Down
8 changes: 5 additions & 3 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cli

import "github.com/spf13/cobra"
import (
"github.com/ALT-F4-LLC/build-configs/internal/config"
"github.com/spf13/cobra"
)

var (
debug bool
configFile string
)

Expand All @@ -20,6 +22,6 @@ func Execute() error {
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "debug mode")
rootCmd.PersistentFlags().BoolVarP(&config.Debug, "debug", "D", false, "debug mode")
rootCmd.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "path to the config file")
}
50 changes: 38 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ type Config struct {
Parameters map[string]interface{} `json:"parameters" yaml:"parameters"`
}

var (
ErrUnsupportedFileType = errors.New("unsupported config file type")
ErrUnsupportedTemplate = errors.New("unsupported template selected")
)

func New(configPath string) (Config, error) {
b, err := os.ReadFile("./" + configPath)
if err != nil {
return Config{}, err
}

// Detect config file type and load it depending on extension
ext := path.Ext(configPath)
switch ext {
case ".json":
Expand All @@ -30,33 +36,49 @@ func New(configPath string) (Config, error) {
return loadConfigYaml(b)
}

return Config{}, errors.New("unsupported file type; supported types are 'json', 'yaml' and 'yml'")
return Config{}, ErrUnsupportedFileType
}

func (c Config) GetTemplater() (Templater, error) {
switch c.Template {
case "go-cobra-cli":
params := NewGoCobraCliConfig(c)
if Debug {
fmt.Println("loading go-cobra-cli templater")
}
tpl := NewGoCobraCliConfig(c)

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

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

case "go-lambda":
params := NewGoLambdaConfig(c)
if Debug {
fmt.Println("loading go-lambda templater")
}
tpl := NewGoLambdaConfig(c)

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

// Then convert them back into the type for the templater selected
if err := json.Unmarshal(b, &tpl); err != nil {
return tpl, err
}
return params, nil
return tpl, nil
}
return nil, fmt.Errorf("unsupported template: %v", c.Template)

return nil, ErrUnsupportedTemplate
}

func loadConfigJson(b []byte) (Config, error) {
Expand All @@ -66,6 +88,8 @@ func loadConfigJson(b []byte) (Config, error) {
return cfg, err
}

Cfg = cfg

return cfg, nil
}

Expand All @@ -76,5 +100,7 @@ func loadConfigYaml(b []byte) (Config, error) {
return cfg, err
}

Cfg = cfg

return cfg, nil
}
52 changes: 48 additions & 4 deletions internal/config/deploy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
package config

import (
"encoding/json"
"fmt"
)

const (
DefaultEnvironment = "unspecified"
DefaultAccount = "677459762413"
DefaultRegion = "us-west-2"
)

type DeployConfig struct {
// Environment is the name of the environment to deploy to.
Environment string `json:"environment" yaml:"environment"`

// Account is the AWS account ID used to deploy to this environment.
Account string `json:"account,omitempty" yaml:"account,omitempty"`

// RoleARN is the ARN of the role used to deploy to this environment.
RoleARN string `json:"roleArn,omitempty" yaml:"roleArn,omitempty"`
Region string `json:"region,omitempty" yaml:"region,omitempty"`

// Region is the AWS region this environment will be deployed into.
Region string `json:"region,omitempty" yaml:"region,omitempty"`

// If is the value of the `if` field for the GitHub Actions job that will
// deploy this application.
If string `json:"if,omitempty" yaml:"if,omitempty"`
}

func NewDeployConfig() DeployConfig {
return DeployConfig{
Region: "us-west-2",
// UnmarshalJSON unmarshals the JSON blob while adding default values
// contextually
func (c *DeployConfig) UnmarshalJSON(data []byte) error {
defaultRole := fmt.Sprintf(
"arn:aws:iam::%s:role/altf4llc-gha-%s-deploy-%s",
DefaultAccount,
Cfg.Name,
DefaultEnvironment,
)

type Alias DeployConfig
deploy := Alias{
Environment: DefaultEnvironment,
Account: DefaultAccount,
RoleARN: defaultRole,
Region: DefaultRegion,
}

if err := json.Unmarshal(data, &deploy); err != nil {
return err
}
*c = DeployConfig(deploy)

return nil
}
4 changes: 4 additions & 0 deletions internal/config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package config

var Cfg Config
var Debug bool = false
2 changes: 2 additions & 0 deletions internal/config/go_cobra_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/ALT-F4-LLC/build-configs/internal/templates"
)

const GoCobraCliName = "go-cobra-cli"

type GoCobraCliConfig struct {
Config
GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
Expand Down
6 changes: 4 additions & 2 deletions internal/config/go_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"github.com/ALT-F4-LLC/build-configs/internal/templates"
)

const GoLambdaName = "go-lambda"

type GoLambdaConfig struct {
Config
GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
Lint GolangCILintConfig `json:"lint,omitempty" yaml:"lint,omitempty"`
Nix NixGoConfig `json:"nix,omitempty" yaml:"nix,omitempty"`
Quirk QuirkConfig `json:"quirk,omitempty" yaml:"quirk,omitempty"`
Deploy DeployConfig `json:"deploy,omitempty" yaml:"deploy,omitempty"`
Deploy []DeployConfig `json:"deploy,omitempty" yaml:"deploy,omitempty"`
PrivateModules string `json:"privateModules,omitempty" yaml:"privateModules,omitempty"`
Lambdas []string `json:"lambdas,omitempty" yaml:"lambdas,omitempty"`
OpenAPI OpenAPIConfig `json:"openapi,omitempty" yaml:"openapi,omitempty"`
Expand All @@ -22,7 +24,7 @@ func NewGoLambdaConfig(c Config) GoLambdaConfig {
GoVersion: "1.22",
Lint: NewGolangCiLintConfig(),
Quirk: NewQuirkConfig(c),
Deploy: NewDeployConfig(),
Deploy: []DeployConfig{},
Lambdas: []string{c.Name},
OpenAPI: NewOpenAPIConfig(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,27 @@ jobs:
if: success() || failure()
{{- end }}

deploy-dev:
{{ range .Deploy }}
deploy-{{ .Environment }}:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
{{- if .If }}
if: {{ .If }}
{{- end }}
permissions:
id-token: write
contents: read
needs: build
strategy:
matrix:
profile:
{{- range .Lambdas }}
{{- range $.Lambdas }}
- {{ . }}
{{- end }}
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: {{ .Deploy.Region }}
role-to-assume: {{ .Deploy.RoleARN }}
aws-region: {{ .Region }}
role-to-assume: {{ .RoleARN }}
- uses: actions/download-artifact@v4
with:
name: "zip-${{"{{"}} matrix.profile {{"}}"}}"
Expand All @@ -100,4 +103,5 @@ jobs:
with:
authToken: ${{"{{"}} secrets.ALTF4LLC_CACHIX_AUTH_TOKEN {{"}}"}}
name: ${{"{{"}} env.CACHIX_BINARY_CACHE {{"}}"}}
- run: nix develop -c just deploy dev "${{"{{"}} matrix.profile {{"}}"}}"
- run: nix develop -c just deploy {{ .Environment }} "${{"{{"}} matrix.profile {{"}}"}}"
{{- end }}

0 comments on commit 2ec29ea

Please sign in to comment.