Skip to content

Instantly share code, notes, and snippets.

@gmlewis
Created February 3, 2023 00:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmlewis/1bee4f31757405ed8be501473351903f to your computer and use it in GitHub Desktop.
Save gmlewis/1bee4f31757405ed8be501473351903f to your computer and use it in GitHub Desktop.
Work in progress - build step using Dagger.io
package common
import (
"context"
"fmt"
"log"
"path/filepath"
"runtime"
"dagger.io/dagger"
)
// HostRepo returns the host repo from Dagger.
func (c *Client) HostRepo(opts ...dagger.HostDirectoryOpts) *dagger.Directory {
_, filename, _, _ := runtime.Caller(0)
scriptDir := filepath.Dir(filename)
// Since this script lives in the cicd/common subdir,
// we need to get the base dir of the repo and start there.
repoBaseDir := filepath.Dir(filepath.Dir(scriptDir))
log.Printf("script dir: %v, repo base dir: %v", scriptDir, repoBaseDir)
return c.Client.Host().Directory(repoBaseDir, opts...)
}
// BuildOption represents an option for modifying the Build behavior.
type BuildOption func(b *buildOptionsT)
// WithDirectory provides a pre-built dagger.Directory for the Build step.
func WithDirectory(src *dagger.Directory) BuildOption {
return func(b *buildOptionsT) {
b.src = src
}
}
type buildOptionsT struct {
src *dagger.Directory
}
// Build performs the following steps:
// - yarn install
// - yarn test:unit
// - yarn lint
// - yarn build
//
// and returns the container in which the steps were performed.
func (c *Client) Build(ctx context.Context, opts ...BuildOption) (*dagger.Directory, error) {
log.Printf("Building node container")
bopts := &buildOptionsT{}
for _, opt := range opts {
opt(bopts)
}
fmt.Println("Running Build pipeline using Dagger")
if bopts.src == nil {
// I believe it is more secure to state what _should_ be included in the image
// as opposed to what should be excluded.
bopts.src = c.HostRepo(dagger.HostDirectoryOpts{
Include: []string{
".env.test",
".eslintrc.js",
"babel.config.js",
"feature-toggles.json",
"jest.config.js",
"node_modules",
"package.json",
"playwright.config.js",
"public",
"src",
"tests",
"vue.config.js",
"webpack.config.js",
"yarn.lock",
},
})
}
node := c.Client.Container().From("node:16").WithExec([]string{"node", "-v"})
nodeVersion, err := node.Stdout(ctx)
if err != nil {
return nil, err
}
log.Printf("Running node version: %v", nodeVersion)
node = node.WithExec([]string{"yarn", "--version"})
yarnVersion, err := node.Stdout(ctx)
if err != nil {
return nil, err
}
log.Printf("Running yarn version: %v", yarnVersion)
cacheKey := "node-modules-cache"
cache := c.Client.CacheVolume(cacheKey)
// mount cloned repository into `node` image and run the pipeline
workdir := "/temp-workdir"
node = node.WithMountedDirectory(workdir, bopts.src).WithWorkdir(workdir).
WithMountedCache(filepath.Join(workdir, "node_modules"), cache).
WithExec([]string{"pwd"}).
WithExec([]string{"ls", "-la"}).
WithExec([]string{"yarn", "install"}).
WithExec([]string{"yarn", "test:unit"}).
WithExec([]string{"yarn", "lint"}).
WithExec([]string{"yarn", "build"}).
WithExec([]string{"ls", "-laR", "dist"})
results, err := node.Stdout(ctx)
if err != nil {
return nil, err
}
log.Printf("%v", results)
log.Printf("Successfully built node container")
return bopts.src, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment