Skip to content

Commit

Permalink
refactor: add dagger modules for actions services (#190)
Browse files Browse the repository at this point in the history
* refactor: add dagger modules for actions services

* refactor: rename source module as ghx
  • Loading branch information
aweris authored Nov 20, 2023
1 parent f2d1aca commit e0e54a7
Show file tree
Hide file tree
Showing 47 changed files with 582 additions and 283 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
workdir: [common, ghx, services/artifact, services/artifactcache]
workdir: [common, ghx]

steps:
- name: Check out code
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions daggerverse/actions/artifactcache/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "actions-artifactcache-service",
"root": "../../..",
"sdk": "go"
}
16 changes: 16 additions & 0 deletions daggerverse/actions/artifactcache/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module main

go 1.21.3

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/mod v0.13.0
golang.org/x/sync v0.4.0
)

require (
github.com/stretchr/testify v1.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
)
File renamed without changes.
47 changes: 47 additions & 0 deletions daggerverse/actions/artifactcache/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

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

"golang.org/x/mod/modfile"
)

// root returns the root directory of the project.
func root() string {
// get location of current file
_, current, _, _ := runtime.Caller(0)

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

func GoVersion(ctx context.Context, gomod *File) (string, error) {
mod, err := gomod.Contents(ctx)
if err != nil {
return "", err
}

f, err := modfile.Parse("go.mod", []byte(mod), nil)
if err != nil {
return "", err
}

return f.Go.Version, nil
}

func modCache(container *Container) *Container {
return container.WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-cache"))
}

func buildCache(container *Container) *Container {
return container.WithMountedCache("/root/.cache/go-build", dag.CacheVolume("go-build-cache"))
}

func goBase(version string) *Container {
return dag.Container().
From(fmt.Sprintf("golang:%s", version)).
With(modCache).
With(buildCache)
}
70 changes: 70 additions & 0 deletions daggerverse/actions/artifactcache/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
)

const DefaultCacheVolumeKey = "gale-artifact-cache-service"

type ActionsArtifactcacheService struct {
CacheVol *CacheVolume
}

// Returns the artifact service itself as a service.
func (m *ActionsArtifactcacheService) Service(ctx context.Context) (*Service, error) {
var (
cache = m.CacheVol
opts = ContainerWithMountedCacheOpts{Sharing: Shared}
)

if cache == nil {
cache = dag.CacheVolume(DefaultCacheVolumeKey)
}

ctr, err := new(source).Container(ctx)
if err != nil {
return nil, err
}

// configure cache volume
ctr = ctr.WithMountedCache("/cache", cache, opts).WithEnvVariable("CACHE_DIR", "/cache")

// configure service port
ctr = ctr.WithExposedPort(8081).WithEnvVariable("PORT", "8081")

// configure service run command
ctr = ctr.WithExec([]string{"go", "run", "."})

return ctr.AsService(), nil
}

// Binds the artifact service as a service to the given container and configures ACTIONS_RUNTIME_URL and
// ACTIONS_RUNTIME_TOKEN to allow the artifact service to communicate with the github actions runner.
func (m *ActionsArtifactcacheService) BindAsService(
// context to use for binding the artifact service.
ctx context.Context,
// container to bind the artifact service to.
ctr *Container,
) (*Container, error) {
service, err := m.Service(ctx)
if err != nil {
return nil, err
}

endpoint, err := service.Endpoint(ctx, ServiceEndpointOpts{Port: 8081, Scheme: "http"})
if err != nil {
return nil, err
}

// normalise the endpoint to remove the trailing slash -- dagger returns the endpoint without the trailing slash
// but the github actions expects the endpoint with the trailing slash
endpoint = endpoint + "/"

// bind the service to the container
ctr = ctr.WithServiceBinding("artifact-cache-service", service)

// set the runtime url and token
ctr = ctr.WithEnvVariable("ACTIONS_CACHE_URL", endpoint).WithEnvVariable("ACTIONS_RUNTIME_TOKEN", "token")

return ctr, nil
}
43 changes: 43 additions & 0 deletions daggerverse/actions/artifactcache/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import "context"

// source is a Dagger module for managing source code of the project.
type source struct{}

// Code returns the source code of the artifact service.
func (m *source) Code() *Directory {
return dag.Host().Directory(root(), HostDirectoryOpts{
Include: []string{
"common/**/*.go",
"common/go.*",
"daggerverse/actions/artifactcache/src/*.go",
"daggerverse/actions/artifactcache/src/go.*",
},
})
}

// GoMod returns the go.mod file of the artifact service.
func (m *source) GoMod() *File {
return m.Code().Directory("daggerverse/actions/artifactcache/src").File("go.mod")
}

// GoVersion returns the Go version of the artifact service.
func (m *source) GoVersion(ctx context.Context) (string, error) {
return GoVersion(ctx, m.GoMod())
}

// MountedCode returns the source code of the artifact service mounted in a container at /src and
// sets the working directory to /src/services/artifact.
func (m *source) MountedCode(c *Container) *Container {
return c.WithMountedDirectory("/src", m.Code()).WithWorkdir("/src/daggerverse/actions/artifactcache/src")
}

func (m *source) Container(ctx context.Context) (*Container, error) {
version, err := m.GoVersion(ctx)
if err != nil {
return nil, err
}

return goBase(version).With(m.MountedCode).WithExec([]string{"go", "mod", "download"}), nil
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/aweris/gale/common => ../../common
replace github.com/aweris/gale/common => ../../../../common
12 changes: 12 additions & 0 deletions daggerverse/actions/artifactcache/src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions daggerverse/actions/artifacts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dagger.gen.go
/internal/querybuilder/
/querybuilder/
5 changes: 5 additions & 0 deletions daggerverse/actions/artifacts/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "actions-artifact-service",
"root": "../../..",
"sdk": "go"
}
16 changes: 16 additions & 0 deletions daggerverse/actions/artifacts/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module actions-artifacts

go 1.21.3

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/mod v0.13.0
golang.org/x/sync v0.4.0
)

require (
github.com/stretchr/testify v1.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
)
37 changes: 37 additions & 0 deletions daggerverse/actions/artifacts/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/99designs/gqlgen v0.17.31 h1:VncSQ82VxieHkea8tz11p7h/zSbvHSxSDZfywqWt158=
github.com/99designs/gqlgen v0.17.31/go.mod h1:i4rEatMrzzu6RXaHydq1nmEPZkb3bKQsnxNRHS4DQB4=
github.com/Khan/genqlient v0.6.0 h1:Bwb1170ekuNIVIwTJEqvO8y7RxBxXu639VJOkKSrwAk=
github.com/Khan/genqlient v0.6.0/go.mod h1:rvChwWVTqXhiapdhLDV4bp9tz/Xvtewwkon4DpWWCRM=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
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/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/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq8cRU=
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/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
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/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=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
47 changes: 47 additions & 0 deletions daggerverse/actions/artifacts/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

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

"golang.org/x/mod/modfile"
)

// root returns the root directory of the project.
func root() string {
// get location of current file
_, current, _, _ := runtime.Caller(0)

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

func GoVersion(ctx context.Context, gomod *File) (string, error) {
mod, err := gomod.Contents(ctx)
if err != nil {
return "", err
}

f, err := modfile.Parse("go.mod", []byte(mod), nil)
if err != nil {
return "", err
}

return f.Go.Version, nil
}

func modCache(container *Container) *Container {
return container.WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-cache"))
}

func buildCache(container *Container) *Container {
return container.WithMountedCache("/root/.cache/go-build", dag.CacheVolume("go-build-cache"))
}

func goBase(version string) *Container {
return dag.Container().
From(fmt.Sprintf("golang:%s", version)).
With(modCache).
With(buildCache)
}
Loading

0 comments on commit e0e54a7

Please sign in to comment.