Skip to content

Instantly share code, notes, and snippets.

@gmlewis
Last active January 25, 2023 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmlewis/bd446031759a4465f488ea205ed5b22e to your computer and use it in GitHub Desktop.
Save gmlewis/bd446031759a4465f488ea205ed5b22e to your computer and use it in GitHub Desktop.
Dagger newbie troubleshooting first Go SDK pipeline
// on-push-or-pull-request executes the Dagger CI/CD pipeline
// for every push or pull request to any branch within this repo.
//
// Usage:
// go run cicd/go-on-push-or-pull-request/main.go
//
// It performs the following steps:
// - yarn install
// - yarn test:unit
// - yarn lint
// - yarn build
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
log.Fatal(err)
}
log.Printf("Done.")
}
func build(ctx context.Context) error {
fmt.Println("Running on-push-or-pull-request CI/CD pipeline using Dagger")
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return err
}
defer client.Close()
_, filename, _, _ := runtime.Caller(0)
scriptDir := filepath.Dir(filename)
// Since this script lives in cicd/go-push-or-pull-request/main.go,
// 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)
// I believe it is more secure to state what _should_ be included in the image
// as opposed to what should be excluded.
src := client.Host().Directory(repoBaseDir, dagger.HostDirectoryOpts{
Include: []string{
".env.test",
".eslintrc.js",
"babel.config.js",
"feature-toggles.json",
"jest.config.js",
"package.json",
"playwright.config.js",
"public",
"src",
"tests",
"vue.config.js",
"webpack.config.js",
"yarn.lock",
},
})
node := client.Container().From("node:16").WithExec([]string{"node", "-v"})
nodeVersion, err := node.Stdout(ctx)
if err != nil {
return err
}
log.Printf("Running node version: %v", nodeVersion)
node = node.WithExec([]string{"yarn", "--version"})
yarnVersion, err := node.Stdout(ctx)
if err != nil {
return err
}
log.Printf("Running yarn version: %v", yarnVersion)
cacheKey := "node-modules-cache"
cache := client.CacheVolume(cacheKey)
// mount cloned repository into `node` image and run the pipeline
workdir := "/temp-workdir"
node = node.WithMountedDirectory(workdir, 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 err
}
log.Printf("%v", results)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment