Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Run CI jobs in a container chosen by the runner

7da9111ecabb236d48fc48592cd3d1d45f9afe78 · Keenan Nemetz · 15d ago · parent 474a812d

Run CI jobs in a container chosen by the runner

Each lease gets a disposable checkout and, when --image is set, its own container from that image. The runner keeps the SSH key and trusted Git cache outside the job, so workflow code cannot reach either; older caches are rebuilt because a previous job may have written to them.

The record names the image and runtime, and the queue mirrors them for the run page. Without --image the runner still executes on the host and says so.

ci/agent/worktree.go +158

@@ -0,0 +1,158 @@
1 package agent
2
3 // A job gets a disposable checkout, and the runner keeps a trusted clone beside it.
4 //
5 // The workflow may edit anything in the job tree, including its .git directory, so
6 // the host must never run Git from that tree after the steps finish. The record and
7 // site pushes use the trusted cache, which is never mounted into the container. The
8 // job tree is a clone of that cache with its own .git, so builds that use Git history
9 // still work, and a poisoned hook dies with the job.
10
11 import (
12 "fmt"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "strconv"
17 "strings"
18 "time"
19
20 "github.com/rodent-software/vole/client"
21 )
22
23 // cacheMarker says the cache was created by a runner that never gave the job
24 // access to it. A cache without the marker predates disposable job trees and may
25 // have hooks or config written by a workflow, so it is rebuilt instead of trusted.
26 const cacheMarker = ".vole-trusted-cache"
27
28 type checkout struct {
29 // Work is the disposable tree the steps run in and the container mounts. It is
30 // removed when the job ends.
31 Work string
32 // GitDir is the trusted cache's git directory. It is used only by the runner
33 // process, never mounted into the job, and survives between jobs.
34 GitDir string
35 }
36
37 func (c checkout) cleanup() {
38 _ = os.RemoveAll(c.Work)
39 }
40
41 // cleanupJobTrees removes job trees left by a crashed runner. It is age-based so a
42 // second runner sharing the same --dir cannot delete work another process is using.
43 func cleanupJobTrees(cache string) {
44 root := filepath.Join(cache, "jobs")
45 entries, err := os.ReadDir(root)
46 if err != nil {
47 return
48 }
49 cutoff := time.Now().Add(-7 * 24 * time.Hour)
50 for _, e := range entries {
51 if !e.IsDir() {
52 continue
53 }
54 info, err := e.Info()
55 if err == nil && info.ModTime().Before(cutoff) {
56 _ = os.RemoveAll(filepath.Join(root, e.Name()))
57 }
58 }
59 }
60
61 // prepareWorktree fetches the exact commit into the trusted cache, then makes a
62 // disposable clone for the job. The cache is updated but never checked out for the
63 // job; its .git is what the record push uses.
64 func prepareWorktree(remote, cache, repo, sha, runID string, attempt int) (checkout, error) {
65 cloneURL, err := client.RepoURL(remote, repo)
66 if err != nil {
67 return checkout{}, err
68 }
69 cacheDir := filepath.Join(cache, repo)
70
71 _, gitErr := os.Stat(filepath.Join(cacheDir, ".git"))
72 _, markerErr := os.Stat(filepath.Join(cacheDir, cacheMarker))
73 if gitErr != nil || markerErr != nil {
74 if err := os.RemoveAll(cacheDir); err != nil {
75 return checkout{}, err
76 }
77 if err := os.MkdirAll(filepath.Dir(cacheDir), 0o755); err != nil {
78 return checkout{}, err
79 }
80 cmd := exec.Command("git", "clone", "--quiet", cloneURL, cacheDir)
81 if out, err := cmd.CombinedOutput(); err != nil {
82 return checkout{}, fmt.Errorf("git clone: %v: %s", err, out)
83 }
84 } else {
85 cmd := exec.Command("git", "-C", cacheDir, "fetch", "--quiet", "--prune", "--tags",
86 "origin", "+refs/heads/*:refs/remotes/origin/*")
87 if out, err := cmd.CombinedOutput(); err != nil {
88 return checkout{}, fmt.Errorf("git fetch: %v: %s", err, out)
89 }
90 }
91 if err := os.WriteFile(filepath.Join(cacheDir, cacheMarker), []byte("1\n"), 0o644); err != nil {
92 return checkout{}, fmt.Errorf("mark the trusted cache: %w", err)
93 }
94
95 if !hasCommit(cacheDir, sha) {
96 cmd := exec.Command("git", "-C", cacheDir, "fetch", "--quiet", "origin", sha)
97 if out, err := cmd.CombinedOutput(); err != nil {
98 return checkout{}, fmt.Errorf("git fetch %s: %v: %s", sha, err, out)
99 }
100 }
101 if !hasCommit(cacheDir, sha) {
102 return checkout{}, fmt.Errorf("the cache does not have commit %s after fetching it", sha)
103 }
104 // A commit fetched by id may not be on a branch, and a local clone only copies
105 // reachable objects. A temporary ref makes it reachable for the job clone; the
106 // ref lives in the trusted cache and is removed before the job runs.
107 jobRef := "refs/vole/jobs/" + runID + "-" + strconv.Itoa(attempt)
108 if out, err := exec.Command("git", "-C", cacheDir, "update-ref", jobRef, sha).CombinedOutput(); err != nil {
109 return checkout{}, fmt.Errorf("git update-ref %s: %v: %s", jobRef, err, out)
110 }
111 defer func() {
112 _ = exec.Command("git", "-C", cacheDir, "update-ref", "-d", jobRef).Run()
113 }()
114
115 gitDir, err := client.GitDirOf(cacheDir)
116 if err != nil {
117 return checkout{}, err
118 }
119
120 jobDir := filepath.Join(cache, "jobs", repo, runID+"-"+strconv.Itoa(attempt))
121 if err := os.MkdirAll(filepath.Dir(jobDir), 0o755); err != nil {
122 return checkout{}, err
123 }
124 if err := os.RemoveAll(jobDir); err != nil {
125 return checkout{}, err
126 }
127 cmd := exec.Command("git", "clone", "--quiet", "--no-hardlinks", cacheDir, jobDir)
128 if out, err := cmd.CombinedOutput(); err != nil {
129 return checkout{}, fmt.Errorf("git clone the job tree: %v: %s", err, out)
130 }
131 // The local clone may not carry a commit that is not on a branch. Fetch the
132 // temporary ref from the cache before pointing origin at the server, which the
133 // job cannot reach without the runner's key.
134 if !hasCommit(jobDir, sha) {
135 cmd := exec.Command("git", "-C", jobDir, "fetch", "--quiet", "origin", jobRef)
136 if out, err := cmd.CombinedOutput(); err != nil {
137 _ = os.RemoveAll(jobDir)
138 return checkout{}, fmt.Errorf("git fetch the job tree: %v: %s", err, out)
139 }
140 }
141 _ = exec.Command("git", "-C", jobDir, "remote", "set-url", "origin", cloneURL).Run()
142
143 for _, args := range [][]string{
144 {"checkout", "--quiet", "--force", "--detach", sha},
145 {"clean", "-qffdx"},
146 } {
147 cmd := exec.Command("git", append([]string{"-C", jobDir}, args...)...)
148 if out, err := cmd.CombinedOutput(); err != nil {
149 _ = os.RemoveAll(jobDir)
150 return checkout{}, fmt.Errorf("git %s: %v: %s", strings.Join(args, " "), err, out)
151 }
152 }
153 return checkout{Work: jobDir, GitDir: gitDir}, nil
154 }
155
156 func hasCommit(dir, sha string) bool {
157 return exec.Command("git", "-C", dir, "cat-file", "-e", sha+"^{commit}").Run() == nil
158 }