shuttle-drone-templates/shuttletask/ci.go

92 lines
1.9 KiB
Go
Raw Normal View History

2023-04-08 12:11:12 +02:00
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)
2023-04-08 12:24:15 +02:00
_, err := droneTemplates.
2023-04-08 12:11:12 +02:00
WithExec([]string{
"sh", "-c", fmt.Sprintf("drone template add --namespace %s --name %s --data @%s || true", namespace, name, name),
2023-04-08 12:11:12 +02:00
}).
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
}