feature/docker-action (#11)
Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: https://git.front.kjuulh.io/kjuulh/kraken/pulls/11
This commit is contained in:
@@ -30,6 +30,18 @@ func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
|
||||
|
||||
zap.L().Debug("Execution done")
|
||||
|
||||
case "docker-build":
|
||||
zap.L().Debug("Building docker-build")
|
||||
runCmd, err := builders.NewDockerBuild(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = runCmd(ctx, area.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
default:
|
||||
return errors.New("could not determine action type")
|
||||
}
|
||||
|
95
internal/actions/builders/docker.go
Normal file
95
internal/actions/builders/docker.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package builders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapio"
|
||||
)
|
||||
|
||||
type DockerBuild struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewDockerBuild(logger *zap.Logger) *DockerBuild {
|
||||
return &DockerBuild{logger: logger}
|
||||
}
|
||||
|
||||
type DockerRunCommand func(ctx context.Context, victimPath string) error
|
||||
|
||||
func (g *DockerBuild) Build(ctx context.Context, modulePath, entryPath string) (DockerRunCommand, error) {
|
||||
g.logger.Debug("Building docker image", zap.String("actiondir", modulePath), zap.String("entry", entryPath))
|
||||
|
||||
if _, err := os.Stat(fmt.Sprintf("%s/%s", modulePath, entryPath)); os.IsNotExist(err) {
|
||||
return nil, errors.New("could not find entry")
|
||||
}
|
||||
|
||||
b := make([]byte, 20)
|
||||
_, err := rand.Reader.Read(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tag := hex.EncodeToString(b)
|
||||
buildDockerCmd := fmt.Sprintf("(cd %s; docker build -f %s --tag kraken/%s .)", modulePath, entryPath, tag)
|
||||
g.logger.Debug("Running command", zap.String("command", buildDockerCmd))
|
||||
|
||||
cmd := exec.CommandContext(
|
||||
ctx,
|
||||
"/bin/bash",
|
||||
"-c",
|
||||
buildDockerCmd,
|
||||
)
|
||||
|
||||
debugwriter := &zapio.Writer{
|
||||
Log: g.logger,
|
||||
Level: zap.DebugLevel,
|
||||
}
|
||||
defer debugwriter.Close()
|
||||
|
||||
cmd.Stdout = debugwriter
|
||||
cmd.Stderr = debugwriter
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g.logger.Debug("Docker image built!")
|
||||
|
||||
return func(ctx context.Context, victimPath string) error {
|
||||
g.logger.Debug("Executing script", zap.String("victim", victimPath))
|
||||
|
||||
cmd := exec.CommandContext(
|
||||
ctx,
|
||||
"/bin/bash",
|
||||
"-c",
|
||||
fmt.Sprintf("docker run --rm -v %s/:/src/work/ kraken/%s", victimPath, tag),
|
||||
)
|
||||
|
||||
runDockerWriter := &zapio.Writer{
|
||||
Log: g.logger,
|
||||
Level: zap.DebugLevel,
|
||||
}
|
||||
defer runDockerWriter.Close()
|
||||
|
||||
cmd.Stdout = runDockerWriter
|
||||
cmd.Stderr = runDockerWriter
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cmd.Wait()
|
||||
}, nil
|
||||
}
|
@@ -168,6 +168,16 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr
|
||||
return fmt.Errorf("could not add file: %w", err)
|
||||
}
|
||||
|
||||
status, err := wt.Status()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if status.IsClean() {
|
||||
pr.logger.Info("Returning early, as no modifications are detected")
|
||||
return nil
|
||||
}
|
||||
|
||||
err = pr.git.Commit(ctx, repo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get diff: %w", err)
|
||||
@@ -175,15 +185,6 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr
|
||||
|
||||
dryrun := false
|
||||
if !dryrun {
|
||||
status, err := wt.Status()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if status.IsClean() {
|
||||
pr.logger.Info("Returning early, as no modifications are detected")
|
||||
return nil
|
||||
}
|
||||
|
||||
err = pr.git.Push(ctx, repo)
|
||||
if err != nil {
|
||||
|
@@ -2,7 +2,9 @@ package gitproviders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
@@ -121,7 +123,15 @@ func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Cl
|
||||
client, ok := g.giteaClients[server]
|
||||
if !ok || client == nil {
|
||||
c, err := gitea.NewClient(server)
|
||||
c.SetBasicAuth("kjuulh", "c0bd801cc9a7f2ed559ea45d603afc92f5443f19")
|
||||
username, ok := os.LookupEnv("GITEA_USERNAME")
|
||||
if !ok {
|
||||
return nil, errors.New("missing environment variable GITEA_USERNAME")
|
||||
}
|
||||
apitoken, ok := os.LookupEnv("GITEA_API_TOKEN")
|
||||
if !ok {
|
||||
return nil, errors.New("missing environment variable GITEA_API_TOKEN")
|
||||
}
|
||||
c.SetBasicAuth(username, apitoken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -63,12 +64,12 @@ func NewGit(logger *zap.Logger, gitConfig *GitConfig, openPGP *signer.OpenPGP) *
|
||||
}
|
||||
|
||||
func (g *Git) GetOriginHEADForRepo(ctx context.Context, gitRepo *GitRepo) (string, error) {
|
||||
remote, err := gitRepo.repo.Remote("origin")
|
||||
auth, err := g.GetAuth()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
auth, err := g.GetAuth()
|
||||
remote, err := gitRepo.repo.Remote("origin")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -82,11 +83,16 @@ func (g *Git) GetOriginHEADForRepo(ctx context.Context, gitRepo *GitRepo) (strin
|
||||
|
||||
headRef := ""
|
||||
for _, ref := range refs {
|
||||
//g.logger.Debug(ref.String())
|
||||
if !ref.Name().IsBranch() {
|
||||
headRef = ref.Target().Short()
|
||||
}
|
||||
}
|
||||
|
||||
if headRef == "" {
|
||||
return "", errors.New("no upstream HEAD branch could be found")
|
||||
}
|
||||
|
||||
return headRef, nil
|
||||
}
|
||||
|
||||
@@ -107,7 +113,7 @@ func (g *Git) CloneBranch(ctx context.Context, storageArea *storage.Area, repoUr
|
||||
Auth: auth,
|
||||
RemoteName: "origin",
|
||||
ReferenceName: plumbing.NewBranchReferenceName(branch),
|
||||
SingleBranch: true,
|
||||
SingleBranch: false,
|
||||
NoCheckout: false,
|
||||
Depth: 1,
|
||||
RecurseSubmodules: 1,
|
||||
@@ -118,7 +124,7 @@ func (g *Git) CloneBranch(ctx context.Context, storageArea *storage.Area, repoUr
|
||||
}
|
||||
|
||||
repo, err := git.PlainCloneContext(ctx, storageArea.Path, false, &cloneOptions)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -144,7 +150,7 @@ func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl stri
|
||||
Auth: auth,
|
||||
RemoteName: "origin",
|
||||
ReferenceName: "refs/heads/main",
|
||||
SingleBranch: true,
|
||||
SingleBranch: false,
|
||||
NoCheckout: false,
|
||||
Depth: 1,
|
||||
RecurseSubmodules: 1,
|
||||
@@ -245,7 +251,7 @@ func (g *Git) CreateBranch(ctx context.Context, gitRepo *GitRepo) error {
|
||||
InsecureSkipTLS: false,
|
||||
CABundle: []byte{},
|
||||
})
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||
return fmt.Errorf("could not pull from origin: %w", err)
|
||||
}
|
||||
|
||||
@@ -287,12 +293,11 @@ func (g *Git) Push(ctx context.Context, gitRepo *GitRepo) error {
|
||||
Auth: auth,
|
||||
Progress: g.getProgressWriter(),
|
||||
Prune: false,
|
||||
Force: false,
|
||||
Force: true,
|
||||
InsecureSkipTLS: false,
|
||||
CABundle: []byte{},
|
||||
RequireRemoteRefs: []config.RefSpec{},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user