Skip to content

Commit

Permalink
ci: automate action generation (#182)
Browse files Browse the repository at this point in the history
* dagger: sync ci module

* ci: automate action generation
  • Loading branch information
aweris authored Nov 8, 2023
1 parent bc4664c commit b22fab8
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 4 deletions.
5 changes: 4 additions & 1 deletion ci/dagger.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "gale-ci",
"root": "..",
"sdk": "go",
"root": ".."
"dependencies": [
"../daggerverse/actions/generator"
]
}
82 changes: 82 additions & 0 deletions ci/gha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"context"
)

const (
// ghaPath is the relative path to the home directory of the generated Github Actions modules
ghaPath = "daggerverse/gha"

// catalogFile is the name of the catalog file for the generated Github Actions modules
catalogFile = "catalog.yaml"
)

type Gha struct{}

// Catalog represents the catalog of generated Github Actions modules.
type Catalog struct {
Global Global `yaml:"global,omitempty"`
Actions []Action `yaml:"actions,omitempty"`
}

// Global represents the global configuration for the generated Github Actions modules.
type Global struct {
// Global Runtime Version to use. If not specified, latest version will be used.
RuntimeVersion string `yaml:"actionsRuntimeVersion,omitempty"`

// Dagger Version to use. If not specified, latest version will be used.
DaggerVersion string `yaml:"daggerVersion,omitempty"`
}

// Action represents a Github Action configuration to generate.
type Action struct {
// Name of the action repo. Format:<owner>/<repo>
Repo string `yaml:"repo"`

// Version of the action to run. Format:<version>
Version string `yaml:"version"`

// Action Runtime Version to use. If not specified, global version will be used.
RuntimeVersion string `yaml:"actionsRuntimeVersion,omitempty"`

// Dagger Version to use. If not specified, global version will be used.
DaggerVersion string `yaml:"daggerVersion,omitempty"`
}

// Generate generates the Github Actions modules from gha catalog and returns the directory containing the generated modules.
func (g *Gha) Generate(ctx context.Context) (*Directory, error) {
var (
gha = dag.Host().Directory(root()).Directory(ghaPath)
cf = gha.File(catalogFile)
)

var catalog Catalog

err := unmarshalContentsToYAML(ctx, cf, &catalog)
if err != nil {
return nil, err
}

for _, action := range catalog.Actions {
var (
name = action.Repo + "@" + action.Version
runtime = action.RuntimeVersion
dagger = action.DaggerVersion
)

if runtime == "" {
runtime = catalog.Global.RuntimeVersion
}

if dagger == "" {
dagger = catalog.Global.DaggerVersion
}

dir := dag.ActionsGenerator().Generate(name, ActionsGeneratorGenerateOpts{RuntimeVersion: runtime, DaggerVersion: dagger})

gha = gha.WithDirectory(".", dir)
}

return gha, nil
}
6 changes: 5 additions & 1 deletion ci/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ require (
github.com/99designs/gqlgen v0.17.31
github.com/Khan/genqlient v0.6.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sync v0.3.0
golang.org/x/sync v0.4.0
)

require (
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1
)
16 changes: 14 additions & 2 deletions ci/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVb
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -25,10 +35,12 @@ github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq
github.com/vektah/gqlparser/v2 v2.5.6/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
19 changes: 19 additions & 0 deletions ci/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"context"
"fmt"
"path/filepath"
"runtime"

"gopkg.in/yaml.v3"
)

// root returns the root directory of the project.
Expand All @@ -12,3 +16,18 @@ func root() string {

return filepath.Join(filepath.Dir(current), "..")
}

// unmarshalContentsToYAML unmarshal the contents of the file as YAML into the given value.
func unmarshalContentsToYAML(ctx context.Context, f *File, v interface{}) error {
stdout, err := f.Contents(ctx)
if err != nil {
return fmt.Errorf("%w: failed to get file contents", err)
}

err = yaml.Unmarshal([]byte(stdout), v)
if err != nil {
return fmt.Errorf("%w: failed to unmarshal file contents", err)
}

return nil
}
4 changes: 4 additions & 0 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ type GaleCi struct{}
func (m *GaleCi) Daggerverse() *Daggerverse {
return &Daggerverse{}
}

func (m *GaleCi) Gha() *Gha {
return &Gha{}
}
13 changes: 13 additions & 0 deletions daggerverse/gha/catalog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# global values to be used by all actions
global:
actionsRuntimeVersion: "e0f087ca2b2b6cb2e2cb2360e497c7e19c2b9365"
daggerVersion: "v0.9.3"

# generated actions. global values can be overridden per action item if needed
actions:
- repo: "actions/hello-world-javascript-action"
version: "main"
- repo: "aquasecurity/trivy-action"
version: "master"
- repo: "trufflesecurity/trufflehog"
version: "main"

0 comments on commit b22fab8

Please sign in to comment.