dagger-drone-test/main.go

54 lines
1.1 KiB
Go
Raw Normal View History

2022-10-28 23:36:54 +02:00
package main
2022-10-28 23:40:47 +02:00
import (
"context"
"fmt"
"os"
2022-10-28 23:41:27 +02:00
"dagger.io/dagger"
2022-10-28 23:40:47 +02:00
)
2022-10-28 23:36:54 +02:00
func main() {
2022-10-28 23:38:35 +02:00
fmt.Printf("hello, world!\n")
2022-10-28 23:40:47 +02:00
if len(os.Args) < 2 {
fmt.Println("must pass in a git repo to build")
os.Exit(1)
}
repo := os.Args[1]
if err := build(repo); err != nil {
fmt.Println(err)
}
}
func build(repoUrl string) error {
fmt.Printf("Building %s\n", repoUrl)
// 1. Get a context
ctx := context.Background()
// 2. Initialize dagger client
2022-10-29 00:11:22 +02:00
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
2022-10-28 23:40:47 +02:00
if err != nil {
return err
}
defer client.Close()
2022-10-29 00:11:22 +02:00
// 3. Clone the repo using Dagger
repo := client.Git(repoUrl)
src, err := repo.Branch("main").Tree().ID(ctx)
if err != nil {
return err
}
// 4. Load the golang image
golang := client.Container().From("golang:latest")
// 5. Mount the cloned repo to the golang image
golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src")
// 6. Do the go build
_, err = golang.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "build", "-o", "build/"},
}).ExitCode(ctx)
if err != nil {
return err
}
2022-10-28 23:40:47 +02:00
return nil
2022-10-28 23:36:54 +02:00
}