Skip to content

Instantly share code, notes, and snippets.

@levlaz
Created October 10, 2023 14:54
Show Gist options
  • Save levlaz/de1a59886d3c8ca835fe58248b3f5fdc to your computer and use it in GitHub Desktop.
Save levlaz/de1a59886d3c8ca835fe58248b3f5fdc to your computer and use it in GitHub Desktop.
Dagger + Hugo
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
fmt.Println(err)
}
}
func getParentDirectory() string {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
parent := filepath.Dir(pwd)
return parent
}
func build(ctx context.Context) error {
fmt.Println("Building with Dagger")
// init dagger client
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
return err
}
defer client.Close()
// get ref to local project
src := client.Host().Directory(".")
// get hugo image
golang := client.Container().From("golang:latest")
// install hugo
golang = golang.WithExec([]string{"go", "install", "github.com/gohugoio/hugo@latest"})
// mount cloned repo into image
golang = golang.WithDirectory("/src", src).WithWorkdir("/src")
// define application build command
path := "public/"
golang = golang.WithExec([]string{"hugo", "version"})
golang = golang.WithExec([]string{"hugo"})
// golang = golang.WithEntrypoint([]string{"hugo-official"})
// get ref to build output directory
output := golang.Directory(path)
// write contents of container public/ to the host
// outputPath := getParentDirectory() + "/" + path
_, err = output.Export(ctx, path)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment