shuttle-drone-templates/shuttletask/ci.go
kjuulh ed5d4674d7
All checks were successful
continuous-integration/drone/push Build is passing
trying with stdout instead
2023-04-08 12:21:22 +02:00

95 lines
1.9 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"path"
"dagger.io/dagger"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
)
func Ci(ctx context.Context) error {
_ = godotenv.Load()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return err
}
defer client.Close()
drone := client.
Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
From("debian").
WithExec([]string{
"apt", "update",
}).
WithExec([]string{
"apt", "install", "-y", "wget", "tar",
}).
WithExec([]string{
"wget", "https://github.com/harness/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz",
}).
WithExec([]string{
"tar", "-xvf", "drone_linux_amd64.tar.gz",
}).
WithExec([]string{
"mv", "drone", "/usr/local/bin",
}).
WithExec([]string{
"drone", "--version",
})
_, err = drone.ExitCode(ctx)
if err != nil {
return err
}
templates := client.Host().Directory("templates")
droneTemplates := drone.
WithEnvVariable("DRONE_SERVER", "https://ci.i.kjuulh.io").
WithEnvVariable("DRONE_TOKEN", os.Getenv("DRONE_TOKEN")).
WithExec([]string{"drone", "info"}).
WithMountedDirectory("/mnt/templates", templates).
WithWorkdir("/mnt/templates")
entries, err := templates.Entries(ctx)
if err != nil {
return err
}
egrp, _ := errgroup.WithContext(ctx)
for _, entry := range entries {
entry := entry
egrp.Go(func() error {
name := path.Base(entry)
namespace := "kjuulh"
log.Printf("running for: %s", entry)
_, _ = droneTemplates.
WithExec([]string{
"drone", "template", "add", "--namespace", namespace, "--name", name, "--data", fmt.Sprintf("@%s", name),
}).
Stdout(ctx)
_, err := droneTemplates.
WithExec([]string{
"drone", "template", "update", "--namespace", namespace, "--name", name, "--data", fmt.Sprintf("@%s", name),
}).
ExitCode(ctx)
return err
})
}
if err := egrp.Wait(); err != nil {
return err
}
return nil
}