rework pipeline
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
"git.front.kjuulh.io/kjuulh/byg"
|
||||
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
|
||||
)
|
||||
|
||||
func Build(builder *internal.Builder, imageTag string, buildPath string) error {
|
||||
log.Printf("building image: %s", imageTag)
|
||||
|
||||
client := builder.Dagger
|
||||
ctx := context.Background()
|
||||
|
||||
return byg.
|
||||
New().
|
||||
Step(
|
||||
"build golang",
|
||||
byg.Step{
|
||||
Execute: func(_ byg.Context) error {
|
||||
src, err := client.
|
||||
Host().
|
||||
Workdir().
|
||||
Read().
|
||||
ID(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
golang := client.Container().From("harbor.front.kjuulh.io/docker-proxy/library/golang:alpine")
|
||||
golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src")
|
||||
_, err = golang.Exec(dagger.ContainerExecOpts{
|
||||
Args: []string{"go", "build", "-o", "build/", buildPath},
|
||||
}).ExitCode(ctx)
|
||||
|
||||
return err
|
||||
},
|
||||
}).
|
||||
Execute(context.Background())
|
||||
|
||||
}
|
13
pkg/tasks/container/create_scratch.go
Normal file
13
pkg/tasks/container/create_scratch.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func CreateScratch(client *dagger.Client) *dagger.Container {
|
||||
log.Println("creating scratch image")
|
||||
|
||||
return client.Container(dagger.ContainerOpts{ID: ""})
|
||||
}
|
13
pkg/tasks/container/load.go
Normal file
13
pkg/tasks/container/load.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func LoadImage(client *dagger.Client, image string) *dagger.Container {
|
||||
log.Printf("loading image: %s", image)
|
||||
|
||||
return client.Container().From(image)
|
||||
}
|
27
pkg/tasks/container/mount.go
Normal file
27
pkg/tasks/container/mount.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func MountCurrent(ctx context.Context, client *dagger.Client, container *dagger.Container, into string) (*dagger.Container, error) {
|
||||
log.Printf("mounting current working directory into path: %s", into)
|
||||
src, err := client.
|
||||
Host().
|
||||
Workdir().
|
||||
Read().
|
||||
ID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return container.WithMountedDirectory(into, src), nil
|
||||
}
|
||||
|
||||
func MountFileFromLoaded(container *dagger.Container, bin dagger.FileID, path string) *dagger.Container {
|
||||
log.Printf("mounting binary into container: into (path=%s)", path)
|
||||
return container.WithMountedFile(path, bin)
|
||||
}
|
9
pkg/tasks/container/workdir.go
Normal file
9
pkg/tasks/container/workdir.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func Workdir(container *dagger.Container, into string) *dagger.Container {
|
||||
return container.WithWorkdir(into)
|
||||
}
|
29
pkg/tasks/golang-bin/build.go
Normal file
29
pkg/tasks/golang-bin/build.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package golangbin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func Build(ctx context.Context, container *dagger.Container, binname string, buildpath string) (dagger.FileID, error) {
|
||||
log.Printf("building binary: (binName=%s) into (buildPath=%s)", binname, buildpath)
|
||||
binpath := fmt.Sprintf("dist/%s", binname)
|
||||
c := container.Exec(dagger.ContainerExecOpts{
|
||||
Args: []string{"go", "build", "-o", binpath, buildpath},
|
||||
})
|
||||
|
||||
_, err := c.ExitCode(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bin, err := c.File(binpath).ID(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bin, nil
|
||||
}
|
18
pkg/tasks/golang/cache.go
Normal file
18
pkg/tasks/golang/cache.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package golang
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func Cache(ctx context.Context, client *dagger.Client, container *dagger.Container) (*dagger.Container, error) {
|
||||
cacheKey := "gomods"
|
||||
cacheID, err := client.CacheVolume(cacheKey).ID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return container.
|
||||
WithMountedCache(cacheID, "/cache").
|
||||
WithEnvVariable("GOMODCACHE", "/cache"), nil
|
||||
}
|
22
pkg/tasks/golang/test.go
Normal file
22
pkg/tasks/golang/test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package golang
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func Test(ctx context.Context, container *dagger.Container) error {
|
||||
log.Printf("testing: image")
|
||||
c := container.Exec(dagger.ContainerExecOpts{
|
||||
Args: []string{"go", "test", "./..."},
|
||||
})
|
||||
|
||||
_, err := c.ExitCode(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user