Skip to content

Instantly share code, notes, and snippets.

@jpadams
Created January 24, 2023 19:59
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 jpadams/5b3c2a8ca48958ab881052eabf95b223 to your computer and use it in GitHub Desktop.
Save jpadams/5b3c2a8ca48958ab881052eabf95b223 to your computer and use it in GitHub Desktop.
// on-push-or-pull-request executes the Dagger CI/CD pipeline
// for every push or pull request to any branch within this repo.
//
// It performs the following steps:
// - TBD
package main
import (
"context"
"fmt"
"log"
"os"
"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()
// I believe it is more secure to state what _should_ be included in the image
// as opposed to what should be exluded.
src := client.Host().Directory(".", dagger.HostDirectoryOpts{
// Exclude: []string{
// "cmd", "coverage", "deploy", "dist", ".git", ".github", "node_modules", "playwright-utils"},
Include: []string{
"babel.config.js",
"feature-toggles.json",
"jest.config.js",
"package.json",
"playwright.config.js",
"public",
"src",
"vue.config.js",
"webpack.config.js",
"yarn.lock",
},
})
node := client.Container().From("node:16").WithEntrypoint([]string{})
nodeVersion, err := node.WithExec([]string{"node", "-v"}).Stdout(ctx)
if err != nil {
return err
}
log.Printf("Running node version: %v", nodeVersion)
yarnVersion, err := node.WithExec([]string{"yarn", "--version"}).Stdout(ctx)
if err != nil {
return err
}
log.Printf("Running yarn version: %v", yarnVersion)
// mount cloned repository into `node` image and run the pipeline
mounted := node.WithMountedDirectory(".", src).
WithExec([]string{"yarn", "install"}).
WithExec([]string{"yarn", "test:unit"}).
WithExec([]string{"yarn", "lint"})
results, err := mounted.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