with buildpath

This commit is contained in:
Kasper Juul Hermansen 2022-10-29 23:25:31 +02:00
parent e9dd36f1e4
commit f89936200e
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
3 changed files with 30 additions and 11 deletions

View File

@ -16,7 +16,7 @@ steps:
- "sleep 5" - "sleep 5"
- apk add git - apk add git
- mkdir -p tmp/dagger-go - mkdir -p tmp/dagger-go
- git clone "https://git.front.kjuulh.io/kjuulh/dagger-go.git" /mnt/dagger-go - git clone "https://git.front.kjuulh.io/kjuulh/dagger-go.git" tmp/dagger-go
- docker build -t dagger-go-builder:${DRONE_COMMIT} -f tmp/dagger-go/Dockerfile . - docker build -t dagger-go-builder:${DRONE_COMMIT} -f tmp/dagger-go/Dockerfile .
- 'docker run -v "$PWD/:/src/" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build --image-tag "{{ or .input.imageTag $imageTag }}"' - 'docker run -v "$PWD/:/src/" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build --image-tag "{{ or .input.imageTag $imageTag }}"'

27
example/main.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks"
)
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
builder, err := internal.New(ctx)
if err != nil {
return err
}
defer builder.CleanUp()
return tasks.Build(builder, "some-image")
}

View File

@ -10,7 +10,7 @@ import (
"git.front.kjuulh.io/kjuulh/dagger-go/internal" "git.front.kjuulh.io/kjuulh/dagger-go/internal"
) )
func Build(builder *internal.Builder, imageTag string) error { func Build(builder *internal.Builder, imageTag string, buildPath string) error {
log.Printf("building image: %s", imageTag) log.Printf("building image: %s", imageTag)
client := builder.Dagger client := builder.Dagger
@ -31,18 +31,10 @@ func Build(builder *internal.Builder, imageTag string) error {
return err return err
} }
log.Println("listing files in /src/")
dir, err := os.ReadDir("/src/")
if err == nil {
for _, d := range dir {
log.Printf("content: %s\n", d.Name())
}
}
golang := client.Container().From("golang:latest") golang := client.Container().From("golang:latest")
golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src") golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src")
_, err = golang.Exec(dagger.ContainerExecOpts{ _, err = golang.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "build", "-o", "build/"}, Args: []string{"go", "build", "-o", "build/", buildPath},
}).ExitCode(ctx) }).ExitCode(ctx)
return err return err