| 1 | // Package runner voert pipelinejobs uit, elk in een eigen Docker-container. |
| 2 | package runner |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "log/slog" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "time" |
| 15 | |
| 16 | "gitlab.com/Timfa/bluegit/internal/access" |
| 17 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 18 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 19 | "gitlab.com/Timfa/bluegit/internal/store" |
| 20 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 21 | ) |
| 22 | |
| 23 | // Runner plant jobs en stuurt het resultaat door naar webhooks. |
| 24 | type Runner struct { |
| 25 | Store *store.Store |
| 26 | GitBin string |
| 27 | Docker string |
| 28 | Storage string |
| 29 | BaseURL string |
| 30 | Network string |
| 31 | Timeout time.Duration |
| 32 | Log *slog.Logger |
| 33 | OnEvent func(repoID int64, ev webhook.Event) |
| 34 | slots chan struct{} |
| 35 | } |
| 36 | |
| 37 | // Loop blijft jobs oppakken tot ctx afloopt. |
| 38 | func (r *Runner) Loop(ctx context.Context, concurrency int) { |
| 39 | if concurrency < 1 { |
| 40 | concurrency = 1 |
| 41 | } |
| 42 | r.slots = make(chan struct{}, concurrency) |
| 43 | tick := time.NewTicker(2 * time.Second) |
| 44 | defer tick.Stop() |
| 45 | for { |
| 46 | select { |
| 47 | case <-ctx.Done(): |
| 48 | return |
| 49 | case <-tick.C: |
| 50 | r.Tick(ctx) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Tick bekijkt alle open pipelines één keer. |
| 56 | func (r *Runner) Tick(ctx context.Context) { |
| 57 | pipes, err := r.Store.ActivePipelines(ctx) |
| 58 | if err != nil { |
| 59 | r.Log.Error("pipelines", "err", err) |
| 60 | return |
| 61 | } |
| 62 | for _, p := range pipes { |
| 63 | r.schedule(ctx, p) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (r *Runner) schedule(ctx context.Context, p store.Pipeline) { |
| 68 | jobs, err := r.Store.JobsByPipeline(ctx, p.ID) |
| 69 | if err != nil { |
| 70 | r.Log.Error("jobs", "err", err) |
| 71 | return |
| 72 | } |
| 73 | specs := make([]ci.Job, len(jobs)) |
| 74 | status := map[string]string{} |
| 75 | for i, j := range jobs { |
| 76 | specs[i] = ci.Job{Name: j.Name, Stage: j.Stage, Needs: j.Needs, NeedsSet: j.NeedsSet, AllowFailure: j.AllowFailure} |
| 77 | status[j.Name] = j.Status |
| 78 | } |
| 79 | for _, j := range jobs { |
| 80 | if j.Status != "queued" { |
| 81 | continue |
| 82 | } |
| 83 | switch ci.Gate(specs, status, specs[indexOf(jobs, j.Name)]) { |
| 84 | case "skip": |
| 85 | _ = r.Store.SkipJob(ctx, j.ID, "overgeslagen omdat een eerdere job mislukte") |
| 86 | status[j.Name] = "skipped" |
| 87 | case "ready": |
| 88 | ok, err := r.Store.MarkJobRunning(ctx, j.ID) |
| 89 | if err != nil || !ok { |
| 90 | continue |
| 91 | } |
| 92 | status[j.Name] = "running" |
| 93 | job := j |
| 94 | pipe := p |
| 95 | go r.execute(ctx, pipe, job) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func indexOf(jobs []store.Job, name string) int { |
| 101 | for i, j := range jobs { |
| 102 | if j.Name == name { |
| 103 | return i |
| 104 | } |
| 105 | } |
| 106 | return 0 |
| 107 | } |
| 108 | |
| 109 | func (r *Runner) execute(ctx context.Context, p store.Pipeline, job store.Job) { |
| 110 | if r.slots != nil { |
| 111 | r.slots <- struct{}{} |
| 112 | defer func() { <-r.slots }() |
| 113 | } |
| 114 | before, _ := r.Store.PipelineByID(context.Background(), p.RepoID, p.ID) |
| 115 | logText, err := r.runOne(ctx, p, job) |
| 116 | status := "success" |
| 117 | if err != nil { |
| 118 | status = "failed" |
| 119 | if logText != "" { |
| 120 | logText += "\n" |
| 121 | } |
| 122 | logText += err.Error() + "\n" |
| 123 | } |
| 124 | if ferr := r.Store.FinishJob(context.Background(), job.ID, status, logText); ferr != nil { |
| 125 | r.Log.Error("job afronden", "err", ferr) |
| 126 | } |
| 127 | final, rerr := r.Store.RefreshPipelineStatus(context.Background(), p.ID) |
| 128 | if rerr != nil { |
| 129 | r.Log.Error("pipelinestatus", "err", rerr) |
| 130 | return |
| 131 | } |
| 132 | if before.Status != final && (final == "success" || final == "failed") && r.OnEvent != nil { |
| 133 | kind := webhook.EventPipelineSuccess |
| 134 | word := "geslaagd" |
| 135 | if final == "failed" { |
| 136 | kind = webhook.EventPipelineFailure |
| 137 | word = "mislukt" |
| 138 | } |
| 139 | repo, err := r.Store.RepoByID(context.Background(), p.RepoID) |
| 140 | if err != nil { |
| 141 | return |
| 142 | } |
| 143 | branch, _ := gitrepo.BranchOfRef(p.Ref) |
| 144 | if branch == "" { |
| 145 | branch = strings.TrimPrefix(p.Ref, "refs/heads/") |
| 146 | } |
| 147 | r.OnEvent(p.RepoID, webhook.Event{ |
| 148 | Type: kind, |
| 149 | Project: repo.FullPath(), |
| 150 | Branch: branch, |
| 151 | Commit: p.CommitSHA, |
| 152 | Summary: fmt.Sprintf("pipeline #%d voor %s is %s", p.ID, repo.FullPath(), word), |
| 153 | URL: strings.TrimRight(r.BaseURL, "/") + "/" + repo.FullPath() + "/-/pipelines/" + fmt.Sprint(p.ID), |
| 154 | PipelineID: fmt.Sprint(p.ID), |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func (r *Runner) runOne(ctx context.Context, p store.Pipeline, job store.Job) (string, error) { |
| 160 | if r.Timeout > 0 { |
| 161 | var cancel context.CancelFunc |
| 162 | ctx, cancel = context.WithTimeout(ctx, r.Timeout) |
| 163 | defer cancel() |
| 164 | } |
| 165 | repo, err := r.Store.RepoByID(ctx, p.RepoID) |
| 166 | if err != nil { |
| 167 | return "", err |
| 168 | } |
| 169 | work := filepath.Join(r.Storage, "work", fmt.Sprint(p.ID), job.Name) |
| 170 | _ = os.RemoveAll(work) |
| 171 | if err := os.MkdirAll(work, 0o755); err != nil { |
| 172 | return "", err |
| 173 | } |
| 174 | bare := gitrepo.Dir(r.Storage, repo.OwnerPath, repo.Name) |
| 175 | if _, err := execOut(ctx, r.GitBin, "clone", "--local", bare, work); err != nil { |
| 176 | return "", fmt.Errorf("clone: %w", err) |
| 177 | } |
| 178 | if _, err := execOut(ctx, r.GitBin, "-C", work, "checkout", "--detach", p.CommitSHA); err != nil { |
| 179 | return "", fmt.Errorf("checkout %s: %w", p.CommitSHA, err) |
| 180 | } |
| 181 | if err := r.materialize(ctx, p, job, work); err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | scriptPath := filepath.Join(work, ".bluegit-job.sh") |
| 185 | if err := os.WriteFile(scriptPath, []byte(scriptOf(job.Script)), 0o755); err != nil { |
| 186 | return "", err |
| 187 | } |
| 188 | token, err := r.Store.CreateJobToken(ctx, job.ID) |
| 189 | if err != nil { |
| 190 | return "", err |
| 191 | } |
| 192 | env, err := r.environment(ctx, repo, p, job, token) |
| 193 | if err != nil { |
| 194 | return "", err |
| 195 | } |
| 196 | if _, err := exec.LookPath(r.Docker); err != nil { |
| 197 | return "", fmt.Errorf("docker (%s) is niet beschikbaar; de job draait alleen in een container", r.Docker) |
| 198 | } |
| 199 | // -e NAAM zonder waarde laat Docker de variabele uit de omgeving van |
| 200 | // deze client overnemen. Zo blijft een meerregelige sleutel heel en |
| 201 | // staat de waarde niet in de proceslijst. |
| 202 | args := []string{"run", "--rm", "--network", r.Network, "-v", work + ":/workspace", "-w", "/workspace"} |
| 203 | cmdEnv := append([]string{}, os.Environ()...) |
| 204 | for k, v := range env { |
| 205 | args = append(args, "-e", k) |
| 206 | cmdEnv = append(cmdEnv, k+"="+v) |
| 207 | } |
| 208 | args = append(args, "--entrypoint", "sh", job.Image, "/workspace/.bluegit-job.sh") |
| 209 | cmd := exec.CommandContext(ctx, r.Docker, args...) |
| 210 | cmd.Env = cmdEnv |
| 211 | var buf bytes.Buffer |
| 212 | cmd.Stdout = &buf |
| 213 | cmd.Stderr = &buf |
| 214 | err = cmd.Run() |
| 215 | logText := buf.String() |
| 216 | if err != nil { |
| 217 | return logText, fmt.Errorf("container eindigde met een fout: %w", err) |
| 218 | } |
| 219 | if err := r.collect(ctx, job, work); err != nil { |
| 220 | return logText, err |
| 221 | } |
| 222 | return logText, nil |
| 223 | } |
| 224 | |
| 225 | func scriptOf(lines string) string { |
| 226 | var b strings.Builder |
| 227 | b.WriteString("#!/bin/sh\nset -e\ncd /workspace\n") |
| 228 | b.WriteString(lines) |
| 229 | if !strings.HasSuffix(lines, "\n") { |
| 230 | b.WriteByte('\n') |
| 231 | } |
| 232 | return b.String() |
| 233 | } |
| 234 | |
| 235 | func (r *Runner) materialize(ctx context.Context, p store.Pipeline, job store.Job, work string) error { |
| 236 | jobs, err := r.Store.JobsByPipeline(ctx, p.ID) |
| 237 | if err != nil { |
| 238 | return err |
| 239 | } |
| 240 | wanted := map[string]bool{} |
| 241 | if job.NeedsSet { |
| 242 | for _, n := range job.Needs { |
| 243 | wanted[n] = true |
| 244 | } |
| 245 | } else { |
| 246 | stageOrder := map[string]int{} |
| 247 | n := 0 |
| 248 | for _, j := range jobs { |
| 249 | if _, ok := stageOrder[j.Stage]; !ok { |
| 250 | stageOrder[j.Stage] = n |
| 251 | n++ |
| 252 | } |
| 253 | } |
| 254 | my := stageOrder[job.Stage] |
| 255 | for _, j := range jobs { |
| 256 | if stageOrder[j.Stage] < my { |
| 257 | wanted[j.Name] = true |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | for _, j := range jobs { |
| 262 | if !wanted[j.Name] || j.Status != "success" { |
| 263 | continue |
| 264 | } |
| 265 | for _, rel := range j.ArtifactPaths { |
| 266 | src, err := r.Store.ArtifactPath(ctx, p.ID, j.Name, rel) |
| 267 | if err != nil { |
| 268 | continue |
| 269 | } |
| 270 | dst := filepath.Join(work, filepath.FromSlash(rel)) |
| 271 | if err := copyFile(src, dst); err != nil { |
| 272 | return err |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return nil |
| 277 | } |
| 278 | |
| 279 | func (r *Runner) collect(ctx context.Context, job store.Job, work string) error { |
| 280 | var expires *time.Time |
| 281 | if job.ArtifactExpire > 0 { |
| 282 | t := time.Now().Add(job.ArtifactExpire) |
| 283 | expires = &t |
| 284 | } |
| 285 | for _, rel := range job.ArtifactPaths { |
| 286 | src := filepath.Join(work, filepath.FromSlash(rel)) |
| 287 | info, err := os.Stat(src) |
| 288 | if err != nil || info.IsDir() { |
| 289 | return fmt.Errorf("artifact %s ontbreekt", rel) |
| 290 | } |
| 291 | dst := filepath.Join(r.Storage, "artifacts", fmt.Sprint(job.ID), filepath.FromSlash(rel)) |
| 292 | if err := copyFile(src, dst); err != nil { |
| 293 | return err |
| 294 | } |
| 295 | if err := r.Store.AddArtifact(ctx, job.ID, rel, dst, info.Size(), expires); err != nil { |
| 296 | return err |
| 297 | } |
| 298 | } |
| 299 | return nil |
| 300 | } |
| 301 | |
| 302 | func (r *Runner) environment(ctx context.Context, repo store.Repo, p store.Pipeline, job store.Job, token string) (map[string]string, error) { |
| 303 | branch := strings.TrimPrefix(p.Ref, "refs/heads/") |
| 304 | patterns, err := r.Store.ProtectedPatterns(ctx, repo.ID) |
| 305 | if err != nil { |
| 306 | return nil, err |
| 307 | } |
| 308 | refProtected := access.BranchProtected(patterns, branch) |
| 309 | env := map[string]string{} |
| 310 | if repo.OwnerKind == "group" { |
| 311 | groupSecrets, err := r.Store.SecretValues(ctx, "group", repo.OwnerID, refProtected) |
| 312 | if err != nil { |
| 313 | return nil, err |
| 314 | } |
| 315 | for k, v := range groupSecrets { |
| 316 | env[k] = v |
| 317 | } |
| 318 | } |
| 319 | repoSecrets, err := r.Store.SecretValues(ctx, "repo", repo.ID, refProtected) |
| 320 | if err != nil { |
| 321 | return nil, err |
| 322 | } |
| 323 | for k, v := range repoSecrets { |
| 324 | env[k] = v |
| 325 | } |
| 326 | for k, v := range job.Variables { |
| 327 | env[k] = v |
| 328 | } |
| 329 | env["CI"] = "true" |
| 330 | env["CI_COMMIT_SHA"] = p.CommitSHA |
| 331 | env["CI_COMMIT_BRANCH"] = branch |
| 332 | env["CI_COMMIT_REF"] = p.Ref |
| 333 | env["CI_PROJECT_PATH"] = repo.FullPath() |
| 334 | env["CI_PIPELINE_ID"] = fmt.Sprint(p.ID) |
| 335 | env["CI_JOB_ID"] = fmt.Sprint(job.ID) |
| 336 | env["CI_JOB_NAME"] = job.Name |
| 337 | env["CI_JOB_TOKEN"] = token |
| 338 | env["CI_API_URL"] = strings.TrimRight(r.BaseURL, "/") + "/api/v1" |
| 339 | return env, nil |
| 340 | } |
| 341 | |
| 342 | func copyFile(src, dst string) error { |
| 343 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 344 | return err |
| 345 | } |
| 346 | in, err := os.Open(src) |
| 347 | if err != nil { |
| 348 | return err |
| 349 | } |
| 350 | defer in.Close() |
| 351 | out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 352 | if err != nil { |
| 353 | return err |
| 354 | } |
| 355 | defer out.Close() |
| 356 | _, err = io.Copy(out, in) |
| 357 | return err |
| 358 | } |
| 359 | |
| 360 | func execOut(ctx context.Context, bin string, args ...string) (string, error) { |
| 361 | if bin == "" { |
| 362 | bin = "git" |
| 363 | } |
| 364 | cmd := exec.CommandContext(ctx, bin, args...) |
| 365 | var stdout, stderr bytes.Buffer |
| 366 | cmd.Stdout = &stdout |
| 367 | cmd.Stderr = &stderr |
| 368 | if err := cmd.Run(); err != nil { |
| 369 | msg := strings.TrimSpace(stderr.String()) |
| 370 | if msg == "" { |
| 371 | msg = err.Error() |
| 372 | } |
| 373 | return "", fmt.Errorf("%s", msg) |
| 374 | } |
| 375 | return stdout.String(), nil |
| 376 | } |