feat(dispatch): pull_request events + one Spot runner per job
- pull_request (opened / reopened / synchronized): on.pull_request branches (the base) and paths / paths-ignore against the PR's changed files (pulls/N/files); workflows read from the PR head. - Every non-host job gets its own ephemeral runner (each runner takes one job; a two-job workflow used to leave a job queued forever). Capped at 6 per workflow. - Push path unchanged otherwise (on.push filters + the commit-marker gate). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PWvSpst5uUEnzviMEBZX9D
This commit is contained in:
1 parent
80dad77b49
commit
125b265e80
1 file changed
+147
-43
+147
-43
@@ -18,8 +18,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -46,6 +46,23 @@ type GiteaPushEvent struct {
|
|||||||
Commits []GiteaCommit `json:"commits"`
|
Commits []GiteaCommit `json:"commits"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GiteaPullRequestEvent is the pull_request webhook (opened / reopened / synchronized).
|
||||||
|
type GiteaPullRequestEvent struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
Number int `json:"number"`
|
||||||
|
PullRequest struct {
|
||||||
|
Head struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
Sha string `json:"sha"`
|
||||||
|
} `json:"head"`
|
||||||
|
Base struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
} `json:"base"`
|
||||||
|
} `json:"pull_request"`
|
||||||
|
Repository GiteaRepo `json:"repository"`
|
||||||
|
Sender GiteaUser `json:"sender"`
|
||||||
|
}
|
||||||
|
|
||||||
type GiteaRepo struct {
|
type GiteaRepo struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -217,6 +234,9 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
|
|||||||
if eventType == "workflow_job" {
|
if eventType == "workflow_job" {
|
||||||
return handleCancel(ctx, c, request.Body)
|
return handleCancel(ctx, c, request.Body)
|
||||||
}
|
}
|
||||||
|
if eventType == "pull_request" || eventType == "pull_request_sync" {
|
||||||
|
return handlePullRequest(ctx, c, request.Body)
|
||||||
|
}
|
||||||
|
|
||||||
// Handle push event
|
// Handle push event
|
||||||
var push GiteaPushEvent
|
var push GiteaPushEvent
|
||||||
@@ -257,36 +277,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
label := "host"
|
dispatched += dispatchJobs(ctx, c, wf, push.Repository.FullName, push.After, name)
|
||||||
for _, job := range wf.Jobs {
|
|
||||||
if job.RunsOn != "" {
|
|
||||||
label = job.RunsOn
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
runID := fmt.Sprintf("%s-%s-%d", push.After[:7], name, time.Now().UnixMilli())
|
|
||||||
|
|
||||||
switch label {
|
|
||||||
case "host":
|
|
||||||
log.Printf("Skipping %s (runs-on: host — registered runner)", name)
|
|
||||||
continue
|
|
||||||
default:
|
|
||||||
// All non-host jobs (go/node/docker/deploy/godot) go through Spot.
|
|
||||||
// The old `deploy` → Lambda-executor path was retired when
|
|
||||||
// tinqs-ci-exec was deleted (26 May 2026); deploy now launches
|
|
||||||
// a t3.micro Spot runner labelled `deploy` like any other job.
|
|
||||||
instanceID, err := startSpotRunner(ctx, c, label, runID)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to start spot for %s: %v", name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := trackRun(ctx, c, push.Repository.FullName, runID, instanceID, name, label); err != nil {
|
|
||||||
log.Printf("Warning: failed to track run: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatched++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return respond(200, fmt.Sprintf(
|
return respond(200, fmt.Sprintf(
|
||||||
@@ -294,6 +285,107 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
|
|||||||
len(workflows), dispatched, push.Repository.FullName, branch))
|
len(workflows), dispatched, push.Repository.FullName, branch))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dispatchJobs starts one Spot runner per non-host job of the workflow (each ephemeral runner takes ONE job, so a
|
||||||
|
// workflow of N jobs needs N runners) and returns how many it started. Capped so a malformed workflow cannot fan out.
|
||||||
|
func dispatchJobs(ctx context.Context, c cfg, wf Workflow, repo, sha, name string) int {
|
||||||
|
const maxJobsPerWorkflow = 6
|
||||||
|
started := 0
|
||||||
|
for jobName, job := range wf.Jobs {
|
||||||
|
label := job.RunsOn
|
||||||
|
if label == "" || label == "host" {
|
||||||
|
log.Printf("Skipping %s/%s (runs-on: host — registered runner)", name, jobName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if started >= maxJobsPerWorkflow {
|
||||||
|
log.Printf("Cap: %s has more than %d spot jobs — not starting %s", name, maxJobsPerWorkflow, jobName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
runID := fmt.Sprintf("%s-%s-%s-%d", sha[:7], name, jobName, time.Now().UnixMilli())
|
||||||
|
instanceID, err := startSpotRunner(ctx, c, label, runID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to start spot for %s/%s: %v", name, jobName, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := trackRun(ctx, c, repo, runID, instanceID, name, label); err != nil {
|
||||||
|
log.Printf("Warning: failed to track run: %v", err)
|
||||||
|
}
|
||||||
|
started++
|
||||||
|
}
|
||||||
|
return started
|
||||||
|
}
|
||||||
|
|
||||||
|
// handlePullRequest dispatches the workflows whose `on.pull_request` matches: base branch filter and paths /
|
||||||
|
// paths-ignore against the PR's changed files. Workflows are read from the PR head (what Gitea runs).
|
||||||
|
func handlePullRequest(ctx context.Context, c cfg, body string) (events.APIGatewayProxyResponse, error) {
|
||||||
|
var pr GiteaPullRequestEvent
|
||||||
|
if err := json.Unmarshal([]byte(body), &pr); err != nil {
|
||||||
|
log.Printf("Failed to parse pull_request webhook: %v", err)
|
||||||
|
return respond(400, `{"error":"invalid payload"}`)
|
||||||
|
}
|
||||||
|
switch pr.Action {
|
||||||
|
case "opened", "reopened", "synchronized":
|
||||||
|
default:
|
||||||
|
return respond(200, fmt.Sprintf(`{"status":"skipped","reason":"pull_request action %s"}`, pr.Action))
|
||||||
|
}
|
||||||
|
repo, head, base, sha := pr.Repository.FullName, pr.PullRequest.Head.Ref, pr.PullRequest.Base.Ref, pr.PullRequest.Head.Sha
|
||||||
|
if len(sha) < 7 {
|
||||||
|
return respond(400, `{"error":"pull_request without head sha"}`)
|
||||||
|
}
|
||||||
|
log.Printf("PR #%d %s %s→%s action=%s by=%s", pr.Number, repo, head, base, pr.Action, pr.Sender.Login)
|
||||||
|
|
||||||
|
changedFiles, err := fetchPullFiles(c.GiteaURL, c.GiteaToken, repo, pr.Number)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to list PR #%d files: %v", pr.Number, err)
|
||||||
|
}
|
||||||
|
workflows, err := fetchWorkflows(c.GiteaURL, c.GiteaToken, repo, head)
|
||||||
|
if err != nil || len(workflows) == 0 {
|
||||||
|
return respond(200, `{"status":"no_workflows"}`)
|
||||||
|
}
|
||||||
|
dispatched := 0
|
||||||
|
for name, content := range workflows {
|
||||||
|
var wf Workflow
|
||||||
|
if err := yaml.Unmarshal([]byte(content), &wf); err != nil {
|
||||||
|
log.Printf("Failed to parse %s: %v", name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !shouldTriggerOn(wf, "pull_request", base, changedFiles) {
|
||||||
|
log.Printf("Skipping %s (no matching pull_request trigger)", name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dispatched += dispatchJobs(ctx, c, wf, repo, sha, name)
|
||||||
|
}
|
||||||
|
return respond(200, fmt.Sprintf(`{"status":"dispatched","pr":%d,"workflows":%d,"jobs":%d}`, pr.Number, len(workflows), dispatched))
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchPullFiles lists the PR's changed file paths (paged).
|
||||||
|
func fetchPullFiles(baseURL, token, repo string, number int) ([]string, error) {
|
||||||
|
var files []string
|
||||||
|
for page := 1; page <= 20; page++ {
|
||||||
|
url := fmt.Sprintf("%s/api/v1/repos/%s/pulls/%d/files?limit=100&page=%d", baseURL, repo, number, page)
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Set("Authorization", "token "+token)
|
||||||
|
resp, err := (&http.Client{Timeout: 10 * time.Second}).Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return files, err
|
||||||
|
}
|
||||||
|
var batch []struct {
|
||||||
|
Filename string `json:"filename"`
|
||||||
|
}
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&batch)
|
||||||
|
resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return files, err
|
||||||
|
}
|
||||||
|
for _, f := range batch {
|
||||||
|
files = append(files, f.Filename)
|
||||||
|
}
|
||||||
|
if len(batch) < 100 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
|
|
||||||
// --- EC2 Spot runner ---
|
// --- EC2 Spot runner ---
|
||||||
|
|
||||||
func startSpotRunner(ctx context.Context, c cfg, label, runID string) (string, error) {
|
func startSpotRunner(ctx context.Context, c cfg, label, runID string) (string, error) {
|
||||||
@@ -345,8 +437,8 @@ func startSpotRunner(ctx context.Context, c cfg, label, runID string) (string, e
|
|||||||
InstanceMarketOptions: &ec2types.InstanceMarketOptionsRequest{
|
InstanceMarketOptions: &ec2types.InstanceMarketOptionsRequest{
|
||||||
MarketType: ec2types.MarketTypeSpot,
|
MarketType: ec2types.MarketTypeSpot,
|
||||||
SpotOptions: &ec2types.SpotMarketOptions{
|
SpotOptions: &ec2types.SpotMarketOptions{
|
||||||
MaxPrice: aws.String(spot.MaxPrice),
|
MaxPrice: aws.String(spot.MaxPrice),
|
||||||
SpotInstanceType: ec2types.SpotInstanceTypeOneTime,
|
SpotInstanceType: ec2types.SpotInstanceTypeOneTime,
|
||||||
InstanceInterruptionBehavior: ec2types.InstanceInterruptionBehaviorTerminate,
|
InstanceInterruptionBehavior: ec2types.InstanceInterruptionBehaviorTerminate,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -413,9 +505,9 @@ func handleCancel(ctx context.Context, c cfg, body string) (events.APIGatewayPro
|
|||||||
ddb := dynamodb.NewFromConfig(awsCfg)
|
ddb := dynamodb.NewFromConfig(awsCfg)
|
||||||
|
|
||||||
out, err := ddb.Query(ctx, &dynamodb.QueryInput{
|
out, err := ddb.Query(ctx, &dynamodb.QueryInput{
|
||||||
TableName: aws.String(c.DDBTable),
|
TableName: aws.String(c.DDBTable),
|
||||||
KeyConditionExpression: aws.String("repo = :repo"),
|
KeyConditionExpression: aws.String("repo = :repo"),
|
||||||
FilterExpression: aws.String("#s = :running"),
|
FilterExpression: aws.String("#s = :running"),
|
||||||
ExpressionAttributeNames: map[string]string{"#s": "status"},
|
ExpressionAttributeNames: map[string]string{"#s": "status"},
|
||||||
ExpressionAttributeValues: map[string]types.AttributeValue{
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
||||||
":repo": &types.AttributeValueMemberS{Value: event.Repository.FullName},
|
":repo": &types.AttributeValueMemberS{Value: event.Repository.FullName},
|
||||||
@@ -471,8 +563,8 @@ func handleCleanup(ctx context.Context, c cfg) (events.APIGatewayProxyResponse,
|
|||||||
cutoff := time.Now().Add(-30 * time.Minute).Unix()
|
cutoff := time.Now().Add(-30 * time.Minute).Unix()
|
||||||
|
|
||||||
out, err := ddb.Scan(ctx, &dynamodb.ScanInput{
|
out, err := ddb.Scan(ctx, &dynamodb.ScanInput{
|
||||||
TableName: aws.String(c.DDBTable),
|
TableName: aws.String(c.DDBTable),
|
||||||
FilterExpression: aws.String("#s = :running AND started_at < :cutoff"),
|
FilterExpression: aws.String("#s = :running AND started_at < :cutoff"),
|
||||||
ExpressionAttributeNames: map[string]string{"#s": "status"},
|
ExpressionAttributeNames: map[string]string{"#s": "status"},
|
||||||
ExpressionAttributeValues: map[string]types.AttributeValue{
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
||||||
":running": &types.AttributeValueMemberS{Value: "running"},
|
":running": &types.AttributeValueMemberS{Value: "running"},
|
||||||
@@ -639,12 +731,21 @@ func fetchRawFile(baseURL, token, repoFullName, path, branch string) (string, er
|
|||||||
// --- Trigger evaluation ---
|
// --- Trigger evaluation ---
|
||||||
|
|
||||||
func shouldTrigger(wf Workflow, wfContent string, branch string, changedFiles []string, commitMessages []string) bool {
|
func shouldTrigger(wf Workflow, wfContent string, branch string, changedFiles []string, commitMessages []string) bool {
|
||||||
|
if !shouldTriggerOn(wf, "push", branch, changedFiles) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return markerAllows(wfContent, commitMessages)
|
||||||
|
}
|
||||||
|
|
||||||
|
// shouldTriggerOn evaluates `on.<event>`: its branches filter (the pushed branch, or a PR's base) and paths /
|
||||||
|
// paths-ignore against the changed files. A workflow whose `on` is not a map triggers on everything.
|
||||||
|
func shouldTriggerOn(wf Workflow, event, branch string, changedFiles []string) bool {
|
||||||
onMap, ok := wf.On.(map[string]interface{})
|
onMap, ok := wf.On.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
pushCfg, ok := onMap["push"]
|
pushCfg, ok := onMap[event]
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -706,10 +807,13 @@ func shouldTrigger(wf Workflow, wfContent string, branch string, changedFiles []
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opt-in commit-marker gate (Ozan 2026-09-22): a `# tinqs-marker: <regex>` comment in the
|
return true
|
||||||
// workflow makes push triggers require a pushed commit whose MESSAGE matches the regex
|
}
|
||||||
// (case-insensitive). No match → no runner, nothing spawned. workflow_dispatch events
|
|
||||||
// never reach this function, so button/CLI publishes are unaffected.
|
// markerAllows is the opt-in commit-marker gate: a `# tinqs-marker: <regex>` comment in the workflow makes push
|
||||||
|
// triggers require a pushed commit whose MESSAGE matches the regex (case-insensitive). No match → nothing spawned.
|
||||||
|
// workflow_dispatch and pull_request events never reach it.
|
||||||
|
func markerAllows(wfContent string, commitMessages []string) bool {
|
||||||
if mr := markerRegex(wfContent); mr != nil {
|
if mr := markerRegex(wfContent); mr != nil {
|
||||||
matched := false
|
matched := false
|
||||||
for _, msg := range commitMessages {
|
for _, msg := range commitMessages {
|
||||||
|
|||||||
Reference in new issue
Block a user