Commit 2b36034ba0

2b36034ba0d1a30802724e65b4aad3a2e776dfb9

parent: 8ddd998102

Verified · cmc ci/build: success ci/quality: success ci/test: success ci/vuln: success

cmc <hello@cleberg.net> · 2026-10-31 08:16 UTC

Runner attachments can name the jobs they claim

repo runner add <repo> --jobs <job,...> narrows an attachment; --jobs
all restores every job and a re-add without --jobs keeps the list.
runner next claims through ClaimBuildFor, which limits each narrowed
repository to its jobs. repo runner list gains a JOBS column and the
settings page takes the list when attaching. Migration 0082 adds
runner_repos.jobs.

This lets krz/omaha-metro-blotter keep daily-pull on the laptop runner,
which can reach its geo-blocked source, while the bay1 runner takes its
code scan.

Changelog for v1.47.0.

Layout: unified · split

.gitbay/wiki/Parity.org +1
@@ -220,6 +220,7 @@ there shows the app's rendering rather than the one the web page shows.
220| mirrors | yes | no | yes | 220| mirrors | yes | no | yes |
221| custom pages domains | yes | no | yes | 221| custom pages domains | yes | no | yes |
222| runners attach, list, detach | yes | yes | n/a | 222| runners attach, list, detach | yes | yes | n/a |
223| runner attached for some jobs | yes | yes | n/a |
223| import from a remote | yes | yes | yes | 224| import from a remote | yes | yes | yes |
224| topics, website | yes | yes | yes | 225| topics, website | yes | yes | yes |
225| visibility | yes | yes | yes | 226| visibility | yes | yes | yes |
.gitbay/wiki/Users.org +7
@@ -890,6 +890,13 @@ detaches one (the key stays on your account; =keys remove= drops it).
890A runner key reaches only the runner protocol and read-only git, so a 890A runner key reaches only the runner protocol and read-only git, so a
891build step that reads it off disk cannot administer your account. 891build step that reads it off disk cannot administer your account.
892 892
893=repo runner add owner/name --jobs a,b < key.pub= narrows an
894attachment to the named jobs, so two runners can share a repository by
895job: one that reaches a geo-blocked data source for the repository's
896own job (=--jobs daily-pull=), another with the code scanning image
897(=--jobs gitbay-security=). A re-add without =--jobs= keeps the list;
898=--jobs all= restores every job. =repo runner list= shows it.
899
893* Large files (LFS) 900* Large files (LFS)
894 901
895Standard Git LFS works over both transports with no setup beyond the 902Standard Git LFS works over both transports with no setup beyond the
CHANGELOG.org +12
@@ -4,6 +4,18 @@ Versioning follows semver from v0.1.0. Database migrations run
4automatically on daemon start; upgrade notes appear per release when 4automatically on daemon start; upgrade notes appear per release when
5anything beyond "replace the binary and restart" is needed. 5anything beyond "replace the binary and restart" is needed.
6 6
7* v1.47.0 — 2026-10-31
8
9Migration 0082.
10
11- =repo runner add <repo> --jobs <job,...> < key.pub= narrows an
12 attachment to the named jobs; =--jobs all= restores every job, and a
13 re-add without =--jobs= keeps the list. =repo runner list= has a
14 JOBS column, and the settings page takes the list when attaching.
15 Two runners can split a repository by job, for instance the
16 repository's own job on a runner that can reach its data and the
17 code scan on one with the scanning image.
18
7* v1.46.1 — 2026-10-31 19* v1.46.1 — 2026-10-31
8 20
9No migrations. 21No migrations.
e2e/codescan_test.go +52
@@ -63,3 +63,55 @@ steps = ['mkdir -p "$GITBAY_RESULTS"', 'cp scan.sarif "$GITBAY_RESULTS/scan.sari
63 t.Fatalf("scan set a status:\n%s", out) 63 t.Fatalf("scan set a status:\n%s", out)
64 } 64 }
65} 65}
66
67// A runner attached for the code scan only leaves the repository's own
68// jobs to its other runner: the shape of a repository whose job must run
69// somewhere the scan image is not.
70func TestRunnerAttachedForJobs(t *testing.T) {
71 t.Parallel()
72 inst := startInstanceWith(t, `[security]
73image = "localhost/scan:1"
74steps = ['mkdir -p "$GITBAY_RESULTS"', 'cp empty.sarif "$GITBAY_RESULTS/x.sarif"']
75`)
76 inst.runner = buildRunner(t)
77 aliceKey := inst.newKey(t, "alice")
78 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
79 scanKey := inst.newKey(t, "scanner")
80 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
81 t.Fatalf("repo create: %s", errOut)
82 }
83 pub, err := os.ReadFile(scanKey + ".pub")
84 if err != nil {
85 t.Fatal(err)
86 }
87 if _, errOut, code := inst.ssh(t, aliceKey, string(pub), "repo", "runner", "add", "alice/app", "--jobs", "gitbay-security"); code != 0 {
88 t.Fatalf("runner add --jobs: %s", errOut)
89 }
90 work := t.TempDir()
91 env := inst.gitEnv(aliceKey)
92 mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/app"), "w")
93 dir := filepath.Join(work, "w")
94 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
95 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n pull:\n steps:\n - echo pulled\n"), 0o644)
96 os.WriteFile(filepath.Join(dir, "empty.sarif"), []byte(`{"version":"2.1.0","runs":[]}`), 0o644)
97 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
98 mustGit(t, dir, env, "add", ".")
99 mustGit(t, dir, env, "commit", "-q", "-m", "ci")
100 mustGit(t, dir, env, "push", "-q", "origin", "main")
101 inst.ssh(t, aliceKey, "", "repo", "security", "enable", "alice/app")
102 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "run", "alice/app"); code != 0 {
103 t.Fatalf("security run: %s", errOut)
104 }
105
106 // The pull build is older, but this runner may only take the scan.
107 if out := inst.runnerOnce(t, scanKey); !strings.Contains(out, "gitbay-security") || strings.Contains(out, " pull ") {
108 t.Fatalf("runner:\n%s", out)
109 }
110 if out := inst.runnerOnce(t, scanKey); strings.Contains(out, "pull") {
111 t.Fatalf("the scan runner claimed the pull job:\n%s", out)
112 }
113 out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
114 if !strings.Contains(out, "pull\tpending") {
115 t.Fatalf("pull was not left pending:\n%s", out)
116 }
117}
internal/ci/ci.go +4
@@ -35,6 +35,10 @@ const (
35 35
36var jobName = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,39}$`) 36var jobName = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,39}$`)
37 37
38// ValidJobName reports whether name could name a job: one of ci.yml's, or
39// SecurityJob.
40func ValidJobName(name string) bool { return jobName.MatchString(name) }
41
38// imageRef matches an OCI image reference conservatively: registry path 42// imageRef matches an OCI image reference conservatively: registry path
39// segments, an optional :tag and an optional @sha256: digest. This string 43// segments, an optional :tag and an optional @sha256: digest. This string
40// becomes an argument to `podman run`, and a repository's config file must 44// becomes an argument to `podman run`, and a repository's config file must
internal/control/build.go +6 −1
@@ -701,11 +701,16 @@ func runRunnerNext(c *Ctx, args []string) int {
701 } 701 }
702 } 702 }
703 untrusted := f.Has("--untrusted") 703 untrusted := f.Has("--untrusted")
704 // An attachment may name the jobs this key claims in its repository.
705 jobs, err := c.Store.RunnerJobFilters(key.ID)
706 if err != nil {
707 return c.fail(protocol.ExitFailure, "%v", err)
708 }
704 var b store.Build 709 var b store.Build
705 var repo store.Repo 710 var repo store.Repo
706 var ok bool 711 var ok bool
707 for attempt := 0; attempt < maxOrphanSkip; attempt++ { 712 for attempt := 0; attempt < maxOrphanSkip; attempt++ {
708 b, ok, err = c.Store.ClaimBuild(repoIDs, untrusted) 713 b, ok, err = c.Store.ClaimBuildFor(repoIDs, untrusted, jobs)
709 if err != nil { 714 if err != nil {
710 return c.fail(protocol.ExitFailure, "%v", err) 715 return c.fail(protocol.ExitFailure, "%v", err)
711 } 716 }
internal/control/runnerrepo.go +51 −9
@@ -4,9 +4,12 @@ import (
4 "errors" 4 "errors"
5 "fmt" 5 "fmt"
6 "io" 6 "io"
7 "slices"
8 "strings"
7 9
8 "golang.org/x/crypto/ssh" 10 "golang.org/x/crypto/ssh"
9 11
12 "gitbay.org/gitbay/internal/ci"
10 "gitbay.org/gitbay/internal/policy" 13 "gitbay.org/gitbay/internal/policy"
11 "gitbay.org/gitbay/internal/protocol" 14 "gitbay.org/gitbay/internal/protocol"
12 "gitbay.org/gitbay/internal/store" 15 "gitbay.org/gitbay/internal/store"
@@ -19,9 +22,12 @@ import (
19// read-only git. 22// read-only git.
20func init() { 23func init() {
21 register(Command{Path: []string{"repo", "runner", "add"}, 24 register(Command{Path: []string{"repo", "runner", "add"},
22 Summary: "attach a runner's public key to a repository", 25 Summary: "attach a runner's public key to a repository",
23 Usage: "repo runner add <owner/name> < key.pub", 26 Usage: "repo runner add <owner/name> [--jobs <job,...>|all] < key.pub",
24 Examples: []string{"repo runner add krz/gitbay < key.pub"}, 27 Flags: []Flag{
28 {"--jobs", "<job,...>", "claim only these jobs here; all restores every job", "every job, or as before on a re-add"},
29 },
30 Examples: []string{"repo runner add krz/gitbay < key.pub", "repo runner add krz/app --jobs gitbay-security < key.pub"},
25 ReadsStdin: true, 31 ReadsStdin: true,
26 MintsCredential: true, NeedsRecentSignIn: true, Run: runRepoRunnerAdd}) 32 MintsCredential: true, NeedsRecentSignIn: true, Run: runRepoRunnerAdd})
27 register(Command{Path: []string{"repo", "runner", "list"}, 33 register(Command{Path: []string{"repo", "runner", "list"},
@@ -37,10 +43,21 @@ func init() {
37} 43}
38 44
39func runRepoRunnerAdd(c *Ctx, args []string) int { 45func runRepoRunnerAdd(c *Ctx, args []string) int {
40 f, err := c.parseArgs(args, flagSpec{MaxPos: 1, Usage: "repo runner add <owner/name> < key.pub"}) 46 f, err := c.parseArgs(args, flagSpec{Values: []string{"--jobs"}, MaxPos: 1, Usage: c.Cmd.Usage})
41 if err != nil || len(f.Pos) != 1 { 47 if err != nil || len(f.Pos) != 1 {
42 return c.usage() 48 return c.usage()
43 } 49 }
50 var jobs []string
51 if v := f.Value("--jobs"); v != "" && v != "all" {
52 for _, j := range strings.Split(v, ",") {
53 if !ci.ValidJobName(j) {
54 return c.fail(protocol.ExitUsage, "bad job name %q", j)
55 }
56 if !slices.Contains(jobs, j) {
57 jobs = append(jobs, j)
58 }
59 }
60 }
44 repo, code := resolveRepo(c, f.Pos[0], policy.CanAdmin) 61 repo, code := resolveRepo(c, f.Pos[0], policy.CanAdmin)
45 if code >= 0 { 62 if code >= 0 {
46 return code 63 return code
@@ -92,9 +109,26 @@ func runRepoRunnerAdd(c *Ctx, args []string) int {
92 if err := c.Store.AttachRunner(key.ID, repo.ID); err != nil { 109 if err := c.Store.AttachRunner(key.ID, repo.ID); err != nil {
93 return c.fail(protocol.ExitFailure, "%v", err) 110 return c.fail(protocol.ExitFailure, "%v", err)
94 } 111 }
95 c.Store.Audit(c.User.ID, "repo.runner.add", map[string]any{"repo": repo.Path(), "fingerprint": fp}) 112 // Without --jobs a re-add leaves the attachment's jobs as they were.
96 d := map[string]string{"fingerprint": fp, "repo": repo.Path()} 113 if f.Has("--jobs") {
114 if err := c.Store.SetRunnerJobs(key.ID, repo.ID, jobs); err != nil {
115 return c.fail(protocol.ExitFailure, "%v", err)
116 }
117 }
118 audit := map[string]any{"repo": repo.Path(), "fingerprint": fp}
119 if f.Has("--jobs") {
120 audit["jobs"] = strings.Join(jobs, ",")
121 }
122 c.Store.Audit(c.User.ID, "repo.runner.add", audit)
123 d := map[string]any{"fingerprint": fp, "repo": repo.Path()}
124 if len(jobs) > 0 {
125 d["jobs"] = jobs
126 }
97 return c.emit(d, func(w io.Writer) { 127 return c.emit(d, func(w io.Writer) {
128 if len(jobs) > 0 {
129 fmt.Fprintf(w, "runner %s attached to %s for %s\n", fp, repo.Path(), strings.Join(jobs, ", "))
130 return
131 }
98 fmt.Fprintf(w, "runner %s attached to %s\n", fp, repo.Path()) 132 fmt.Fprintf(w, "runner %s attached to %s\n", fp, repo.Path())
99 }) 133 })
100} 134}
@@ -115,7 +149,7 @@ func runRepoRunnerList(c *Ctx, args []string) int {
115 runners = []store.RepoRunner{} 149 runners = []store.RepoRunner{}
116 } 150 }
117 return c.emitView(runners, func(w io.Writer) { 151 return c.emitView(runners, func(w io.Writer) {
118 tb := c.table(w, "FINGERPRINT", "ALGO", "USER", "SEEN", "HELD") 152 tb := c.table(w, "FINGERPRINT", "ALGO", "USER", "SEEN", "HELD", "JOBS")
119 for _, r := range runners { 153 for _, r := range runners {
120 seen := r.LastSeen 154 seen := r.LastSeen
121 if seen == "" { 155 if seen == "" {
@@ -125,7 +159,11 @@ func runRepoRunnerList(c *Ctx, args []string) int {
125 if r.BuildNumber != 0 { 159 if r.BuildNumber != 0 {
126 held = fmt.Sprintf("%s #%d %s since %s", r.BuildRepo, r.BuildNumber, r.BuildJob, r.StartedAt) 160 held = fmt.Sprintf("%s #%d %s since %s", r.BuildRepo, r.BuildNumber, r.BuildJob, r.StartedAt)
127 } 161 }
128 tb.row(cRef(r.Fingerprint), cText(r.Algo), cText(r.Username), cAge(seen), cText(held)) 162 jobs := "all"
163 if len(r.Jobs) > 0 {
164 jobs = strings.Join(r.Jobs, ",")
165 }
166 tb.row(cRef(r.Fingerprint), cText(r.Algo), cText(r.Username), cAge(seen), cText(held), cText(jobs))
129 } 167 }
130 tb.flush() 168 tb.flush()
131 }, func() screen { 169 }, func() screen {
@@ -139,7 +177,11 @@ func runRepoRunnerList(c *Ctx, args []string) int {
139 if r.LastSeen != "" { 177 if r.LastSeen != "" {
140 seen = "seen " + relAge(r.LastSeen, termNow()) 178 seen = "seen " + relAge(r.LastSeen, termNow())
141 } 179 }
142 rows[i] = rowOf(cFlexRef(r.Fingerprint), lead, cText(r.Username), cMeta(r.Algo, seen, held)) 180 only := ""
181 if len(r.Jobs) > 0 {
182 only = "only " + strings.Join(r.Jobs, ",")
183 }
184 rows[i] = rowOf(cFlexRef(r.Fingerprint), lead, cText(r.Username), cMeta(r.Algo, seen, held, only))
143 } 185 }
144 return listScreen("Runners", rows, 186 return listScreen("Runners", rows,
145 action{"Runners", []string{"repo", "runner", "remove", repo.Path(), "<fingerprint>"}}, 187 action{"Runners", []string{"repo", "runner", "remove", repo.Path(), "<fingerprint>"}},
internal/control/runnerrepo_test.go +35
@@ -122,3 +122,38 @@ func TestRepoRunnerAddRefusesWrongKeys(t *testing.T) {
122 t.Fatalf("attached anyway: %+v", runners) 122 t.Fatalf("attached anyway: %+v", runners)
123 } 123 }
124} 124}
125
126// --jobs narrows an attachment, a re-add without it keeps the list, and
127// all restores every job; runner next claims accordingly.
128func TestRepoRunnerAddJobs(t *testing.T) {
129 st, repo, uid := newQueueTestRepo(t)
130 add := func(args ...string) string {
131 t.Helper()
132 c, out := repoRunnerCtx(t, st, uid, false, testRunnerPub)
133 if code := runRepoRunnerAdd(c, append([]string{repo.Path()}, args...)); code != protocol.ExitOK {
134 t.Fatalf("add %v: exit %d %s", args, code, out.String())
135 }
136 c, out = repoRunnerCtx(t, st, uid, false, "")
137 runRepoRunnerList(c, []string{repo.Path()})
138 return out.String()
139 }
140 if out := add("--jobs", "gitbay-security,gitbay-security"); !strings.Contains(out, `"jobs":["gitbay-security"]`) {
141 t.Fatalf("list after --jobs: %s", out)
142 }
143 if out := add(); !strings.Contains(out, `"jobs":["gitbay-security"]`) {
144 t.Fatalf("a re-add without --jobs dropped the list: %s", out)
145 }
146 c, out := repoRunnerCtx(t, st, uid, false, testRunnerPub)
147 if code := runRepoRunnerAdd(c, []string{repo.Path(), "--jobs", "Bad Name"}); code != protocol.ExitUsage {
148 t.Fatalf("bad job name: exit %d %s", code, out.String())
149 }
150
151 keys, _ := st.ListSSHKeys(uid)
152 filters, err := st.RunnerJobFilters(keys[0].ID)
153 if err != nil || len(filters[repo.ID]) != 1 {
154 t.Fatalf("filters = %v %v", filters, err)
155 }
156 if out := add("--jobs", "all"); strings.Contains(out, `"jobs"`) {
157 t.Fatalf("all did not restore every job: %s", out)
158 }
159}
internal/control/testdata/plain/repo-runner-list.txt +1 −1
@@ -1 +1 @@
1SHA256:z/fSv0Z0RZS+Lccbc6ZoOWwt/fbj1VFJGSBKQbo4icE ssh-ed25519 alice never idle 1SHA256:z/fSv0Z0RZS+Lccbc6ZoOWwt/fbj1VFJGSBKQbo4icE ssh-ed25519 alice never idle all
internal/httpd/settings.go +6 −1
@@ -106,6 +106,7 @@ func (s *Server) settingsFormWith(w http.ResponseWriter, r *http.Request, u stor
106 "events": submitted.Get("events"), 106 "events": submitted.Get("events"),
107 "name": submitted.Get("name"), 107 "name": submitted.Get("name"),
108 "new-owner": submitted.Get("new-owner"), 108 "new-owner": submitted.Get("new-owner"),
109 "jobs": submitted.Get("jobs"),
109 } 110 }
110 } 111 }
111 s.render(w, "settings.html", settingsPage{ 112 s.render(w, "settings.html", settingsPage{
@@ -247,7 +248,11 @@ func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store.
247 s.settingsRedirect(w, r, "paste the runner's public key") 248 s.settingsRedirect(w, r, "paste the runner's public key")
248 return 249 return
249 } 250 }
250 msg, ok := s.runControlStdin(u, []string{"repo", "runner", "add", repo}, body+"\n") 251 argv := []string{"repo", "runner", "add", repo}
252 if jobs := strings.Join(strings.Fields(strings.ReplaceAll(v("jobs"), ",", " ")), ","); jobs != "" {
253 argv = append(argv, "--jobs", jobs)
254 }
255 msg, ok := s.runControlStdin(u, argv, body+"\n")
251 if !ok { 256 if !ok {
252 s.settingsFormWith(w, r, u, msg, r.Form) 257 s.settingsFormWith(w, r, u, msg, r.Form)
253 return 258 return
internal/store/builds.go +32 −2
@@ -106,6 +106,12 @@ func scanBuild(row interface{ Scan(...any) error }) (Build, error) {
106// repository — are skipped unless untrusted is set: they run a stranger's 106// repository — are skipped unless untrusted is set: they run a stranger's
107// code, which only a runner that isolates should take. 107// code, which only a runner that isolates should take.
108func (s *Store) ClaimBuild(repoIDs []int64, untrusted bool) (Build, bool, error) { 108func (s *Store) ClaimBuild(repoIDs []int64, untrusted bool) (Build, bool, error) {
109 return s.ClaimBuildFor(repoIDs, untrusted, nil)
110}
111
112// ClaimBuildFor is ClaimBuild where jobs narrows some repositories to the
113// jobs named for them (RunnerJobFilters).
114func (s *Store) ClaimBuildFor(repoIDs []int64, untrusted bool, jobs map[int64][]string) (Build, bool, error) {
109 tx, err := s.DB.Begin() 115 tx, err := s.DB.Begin()
110 if err != nil { 116 if err != nil {
111 return Build{}, false, err 117 return Build{}, false, err
@@ -117,11 +123,33 @@ func (s *Store) ClaimBuild(repoIDs []int64, untrusted bool) (Build, bool, error)
117 query += " AND trusted = 1" 123 query += " AND trusted = 1"
118 } 124 }
119 if len(repoIDs) > 0 { 125 if len(repoIDs) > 0 {
120 marks := strings.TrimSuffix(strings.Repeat("?,", len(repoIDs)), ",") 126 var whole []int64
121 query += " AND repo_id IN (" + marks + ")" 127 var narrowed []string
122 for _, id := range repoIDs { 128 for _, id := range repoIDs {
129 names, ok := jobs[id]
130 if !ok {
131 whole = append(whole, id)
132 continue
133 }
134 narrowed = append(narrowed, "(repo_id = ? AND job IN ("+marks(len(names))+"))")
123 args = append(args, id) 135 args = append(args, id)
136 for _, n := range names {
137 args = append(args, n)
138 }
124 } 139 }
140 var alts []string
141 if len(whole) > 0 {
142 alts = append(alts, "repo_id IN ("+marks(len(whole))+")")
143 // The narrowed repos' args were appended first; put these in
144 // front to match the clause order.
145 front := make([]any, len(whole))
146 for i, id := range whole {
147 front[i] = id
148 }
149 args = append(front, args...)
150 }
151 alts = append(alts, narrowed...)
152 query += " AND (" + strings.Join(alts, " OR ") + ")"
125 } 153 }
126 query += " ORDER BY id LIMIT 1" 154 query += " ORDER BY id LIMIT 1"
127 var id int64 155 var id int64
@@ -518,3 +546,5 @@ func (s *Store) SuccessBuildFor(repoID int64, sha, job string) (Build, bool, err
518 } 546 }
519 return b, err == nil, err 547 return b, err == nil, err
520} 548}
549
550func marks(n int) string { return strings.TrimSuffix(strings.Repeat("?,", n), ",") }
internal/store/builds_test.go +37
@@ -653,3 +653,40 @@ func TestBuildIDsNotReusedAfterRepoDelete(t *testing.T) {
653 t.Fatalf("next build took id %d; a runner may still hold %d", got, claimed) 653 t.Fatalf("next build took id %d; a runner may still hold %d", got, claimed)
654 } 654 }
655} 655}
656
657// A repository narrowed to some jobs yields only those; the others in the
658// claim are unaffected.
659func TestClaimBuildForNarrowsByJob(t *testing.T) {
660 s := open(t)
661 if err := s.MigrateUp(); err != nil {
662 t.Fatal(err)
663 }
664 uid, _ := s.CreateUser("cmc", true)
665 blotter, _ := s.CreateRepo("user", uid, "blotter", "public")
666 site, _ := s.CreateRepo("user", uid, "site", "public")
667 s.CreateBuild(blotter, "daily-pull", "a1", "main", `["true"]`, "", "", true)
668 scan, _ := s.CreateBuild(blotter, "gitbay-security", "a1", "main", `["true"]`, "", "", true)
669 site1, _ := s.CreateBuild(site, "deploy", "b1", "main", `["true"]`, "", "", true)
670
671 jobs := map[int64][]string{blotter: {"gitbay-security"}}
672 var got []string
673 for {
674 b, ok, err := s.ClaimBuildFor([]int64{blotter, site}, false, jobs)
675 if err != nil {
676 t.Fatal(err)
677 }
678 if !ok {
679 break
680 }
681 got = append(got, b.Job)
682 if (b.RepoID == blotter && b.Number != scan) || (b.RepoID == site && b.Number != site1) {
683 t.Errorf("claimed %+v", b)
684 }
685 }
686 if strings.Join(got, ",") != "gitbay-security,deploy" {
687 t.Fatalf("claimed %v; daily-pull must stay for another runner", got)
688 }
689 if b, ok, _ := s.ClaimBuild([]int64{blotter}, false); !ok || b.Job != "daily-pull" {
690 t.Fatalf("unnarrowed claim = %+v %v", b, ok)
691 }
692}
internal/store/migrations/0082_runner_jobs.down.sql added +1
@@ -0,0 +1 @@
1ALTER TABLE runner_repos DROP COLUMN jobs;
internal/store/migrations/0082_runner_jobs.up.sql added +5
@@ -0,0 +1,5 @@
1-- An attachment may name the jobs its runner claims in that repository,
2-- comma-separated; '' is every job. It lets two runners share a
3-- repository by job: one that can reach a geo-blocked source for the
4-- repository's own job, another with the code scanning image.
5ALTER TABLE runner_repos ADD COLUMN jobs TEXT NOT NULL DEFAULT '';
internal/store/runners.go +45 −8
@@ -1,6 +1,9 @@
1package store 1package store
2 2
3import "sort" 3import (
4 "sort"
5 "strings"
6)
4 7
5// Runner is one runner key as the instance admin sees it. 8// Runner is one runner key as the instance admin sees it.
6type Runner struct { 9type Runner struct {
@@ -26,11 +29,13 @@ type RepoRunner struct {
26 Algo string `json:"algo"` 29 Algo string `json:"algo"`
27 Username string `json:"username"` 30 Username string `json:"username"`
28 AddedAt string `json:"added_at"` 31 AddedAt string `json:"added_at"`
29 LastSeen string `json:"last_seen,omitempty"` 32 // Jobs are the only jobs this runner claims here; none is every job.
30 BuildRepo string `json:"build_repo,omitempty"` 33 Jobs []string `json:"jobs,omitempty"`
31 BuildNumber int64 `json:"build_number,omitempty"` 34 LastSeen string `json:"last_seen,omitempty"`
32 BuildJob string `json:"build_job,omitempty"` 35 BuildRepo string `json:"build_repo,omitempty"`
33 StartedAt string `json:"started_at,omitempty"` 36 BuildNumber int64 `json:"build_number,omitempty"`
37 BuildJob string `json:"build_job,omitempty"`
38 StartedAt string `json:"started_at,omitempty"`
34} 39}
35 40
36// AttachRunner lets a key claim a repository's builds. Attaching twice is 41// AttachRunner lets a key claim a repository's builds. Attaching twice is
@@ -40,6 +45,34 @@ func (s *Store) AttachRunner(keyID, repoID int64) error {
40 return err 45 return err
41} 46}
42 47
48// SetRunnerJobs narrows an attachment to the named jobs; none restores
49// every job.
50func (s *Store) SetRunnerJobs(keyID, repoID int64, jobs []string) error {
51 _, err := s.DB.Exec("UPDATE runner_repos SET jobs = ? WHERE key_id = ? AND repo_id = ?",
52 strings.Join(jobs, ","), keyID, repoID)
53 return err
54}
55
56// RunnerJobFilters maps each repository a key is attached to with a job
57// list to that list. Repositories it may claim every job in are absent.
58func (s *Store) RunnerJobFilters(keyID int64) (map[int64][]string, error) {
59 rows, err := s.DB.Query("SELECT repo_id, jobs FROM runner_repos WHERE key_id = ? AND jobs != ''", keyID)
60 if err != nil {
61 return nil, err
62 }
63 defer rows.Close()
64 out := map[int64][]string{}
65 for rows.Next() {
66 var id int64
67 var jobs string
68 if err := rows.Scan(&id, &jobs); err != nil {
69 return nil, err
70 }
71 out[id] = strings.Split(jobs, ",")
72 }
73 return out, rows.Err()
74}
75
43// DetachRunner removes one attachment by fingerprint. The key itself stays. 76// DetachRunner removes one attachment by fingerprint. The key itself stays.
44func (s *Store) DetachRunner(repoID int64, fingerprint string) error { 77func (s *Store) DetachRunner(repoID int64, fingerprint string) error {
45 res, err := s.DB.Exec(`DELETE FROM runner_repos WHERE repo_id = ? 78 res, err := s.DB.Exec(`DELETE FROM runner_repos WHERE repo_id = ?
@@ -104,7 +137,7 @@ func (s *Store) RunnerAttached(keyID, repoID int64) (bool, error) {
104// ListRepoRunners is every key attached to a repository with its last 137// ListRepoRunners is every key attached to a repository with its last
105// poll and the build it holds, oldest attachment first. 138// poll and the build it holds, oldest attachment first.
106func (s *Store) ListRepoRunners(repoID int64) ([]RepoRunner, error) { 139func (s *Store) ListRepoRunners(repoID int64) ([]RepoRunner, error) {
107 rows, err := s.DB.Query(`SELECT k.fingerprint, k.algo, u.username, rr.added_at, 140 rows, err := s.DB.Query(`SELECT k.fingerprint, k.algo, u.username, rr.added_at, rr.jobs,
108 COALESCE(rs.last_seen, ''), 141 COALESCE(rs.last_seen, ''),
109 COALESCE(COALESCE(bu.username, bo.name) || '/' || br.name, ''), 142 COALESCE(COALESCE(bu.username, bo.name) || '/' || br.name, ''),
110 COALESCE(b.number, 0), COALESCE(b.job, ''), COALESCE(b.started_at, '') 143 COALESCE(b.number, 0), COALESCE(b.job, ''), COALESCE(b.started_at, '')
@@ -124,10 +157,14 @@ func (s *Store) ListRepoRunners(repoID int64) ([]RepoRunner, error) {
124 var out []RepoRunner 157 var out []RepoRunner
125 for rows.Next() { 158 for rows.Next() {
126 var r RepoRunner 159 var r RepoRunner
127 if err := rows.Scan(&r.Fingerprint, &r.Algo, &r.Username, &r.AddedAt, &r.LastSeen, 160 var jobs string
161 if err := rows.Scan(&r.Fingerprint, &r.Algo, &r.Username, &r.AddedAt, &jobs, &r.LastSeen,
128 &r.BuildRepo, &r.BuildNumber, &r.BuildJob, &r.StartedAt); err != nil { 162 &r.BuildRepo, &r.BuildNumber, &r.BuildJob, &r.StartedAt); err != nil {
129 return nil, err 163 return nil, err
130 } 164 }
165 if jobs != "" {
166 r.Jobs = strings.Split(jobs, ",")
167 }
131 out = append(out, r) 168 out = append(out, r)
132 } 169 }
133 return out, rows.Err() 170 return out, rows.Err()
internal/web/templates/settings.html +4 −1
@@ -291,7 +291,7 @@
291<section id="runners"><h2>Runners</h2> 291<section id="runners"><h2>Runners</h2>
292{{if .Runners}} 292{{if .Runners}}
293<ul class="protlist"> 293<ul class="protlist">
294{{range .Runners}}<li><code>{{.Fingerprint}}</code> <span class="meta">{{.Username}}{{if .LastSeen}}, last poll {{when .LastSeen}}{{else}}, never polled{{end}}{{if .BuildNumber}}, running {{.BuildRepo}} #{{.BuildNumber}} {{.BuildJob}}{{end}}</span> 294{{range .Runners}}<li><code>{{.Fingerprint}}</code> <span class="meta">{{.Username}}{{if .LastSeen}}, last poll {{when .LastSeen}}{{else}}, never polled{{end}}{{if .BuildNumber}}, running {{.BuildRepo}} #{{.BuildNumber}} {{.BuildJob}}{{end}}{{if .Jobs}}, only {{join .Jobs ", "}}{{end}}</span>
295 <form method="post" action="{{$base}}" class="inline"> 295 <form method="post" action="{{$base}}" class="inline">
296 <input type="hidden" name="field" value="runner-remove"> 296 <input type="hidden" name="field" value="runner-remove">
297 <input type="hidden" name="fingerprint" value="{{.Fingerprint}}"> 297 <input type="hidden" name="fingerprint" value="{{.Fingerprint}}">
@@ -305,6 +305,9 @@
305 <label for="runner-key">Attach a runner</label> 305 <label for="runner-key">Attach a runner</label>
306 <p class="hint">Install <code>gitbay-runner</code>, run <code>gitbay-runner init</code>, and paste the key it prints. The runner builds your commits with the repository's secrets; merge requests from forks wait unless it runs with <code>-untrusted</code>.</p> 306 <p class="hint">Install <code>gitbay-runner</code>, run <code>gitbay-runner init</code>, and paste the key it prints. The runner builds your commits with the repository's secrets; merge requests from forks wait unless it runs with <code>-untrusted</code>.</p>
307 <textarea id="runner-key" name="key" rows="3" spellcheck="false" placeholder="ssh-ed25519 AAAA… (from gitbay-runner init)">{{index .Submitted "key"}}</textarea> 307 <textarea id="runner-key" name="key" rows="3" spellcheck="false" placeholder="ssh-ed25519 AAAA… (from gitbay-runner init)">{{index .Submitted "key"}}</textarea>
308 <label for="runner-jobs">Jobs</label>
309 <p class="hint">Leave empty for every job. Name jobs, comma-separated, to have this runner claim only those here, for instance <code>gitbay-security</code> on a runner with the code scanning image.</p>
310 <input type="text" id="runner-jobs" name="jobs" value="{{index .Submitted "jobs"}}">
308 <button type="submit" class="btn">Attach</button> 311 <button type="submit" class="btn">Attach</button>
309</form> 312</form>
310</section> 313</section>