Created
October 10, 2023 14:54
-
-
Save levlaz/de1a59886d3c8ca835fe58248b3f5fdc to your computer and use it in GitHub Desktop.
Dagger + Hugo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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