-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (79 loc) · 3.9 KB
/
app.py
File metadata and controls
94 lines (79 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
Provider selector example demonstrating custom provider selection logic.
This example demonstrates:
- Custom provider selection based on repository name
- Filtering out certain jobs (e.g., draft PRs)
- Dynamically adding labels based on job metadata
- Route production repos to dedicated high-performance providers
"""
import aws_cdk as cdk
from aws_cdk import Stack, aws_lambda as lambda_, aws_codebuild as codebuild
from cloudsnorkel.cdk_github_runners import (
GitHubRunners,
CodeBuildRunnerProvider,
)
class ProviderSelectorStack(Stack):
def __init__(self, scope, construct_id, **kwargs):
super().__init__(scope, construct_id, **kwargs)
# Create a default provider for regular builds
default_provider = CodeBuildRunnerProvider(
self, "DefaultProvider",
labels=["custom-runner", "default"],
compute_type=codebuild.ComputeType.SMALL,
)
# Create a production provider with more resources for production repos
production_provider = CodeBuildRunnerProvider(
self, "ProductionProvider",
labels=["custom-runner", "production"],
compute_type=codebuild.ComputeType.LARGE, # More CPU and memory for production builds
)
# Create a provider selector Lambda function
# This function receives the webhook payload and can customize provider selection
provider_selector = lambda_.Function(
self, "ProviderSelector",
runtime=lambda_.Runtime.NODEJS_LATEST,
handler="index.handler",
code=lambda_.Code.from_inline("""
exports.handler = async (event) => {
const { payload, providers, defaultProvider, defaultLabels } = event;
console.log('Processing job:', {
repository: payload.repository?.name,
branch: payload.workflow_job?.head_branch,
labels: payload.workflow_job?.labels,
});
// Route production repos to dedicated provider
if (payload.repository?.name?.includes('prod') ||
payload.repository?.name?.includes('production')) {
console.log('Routing to production provider');
return {
provider: '""" + production_provider.node.path + """',
labels: ['custom-runner', 'production', 'modified-via-selector'],
};
}
// Filter out draft PRs (skip runner provisioning)
if (payload.workflow_job?.head_branch?.startsWith('draft/') ||
payload.workflow_job?.head_branch?.startsWith('wip/')) {
console.log('Skipping draft PR');
return { provider: undefined }; // Skip runner provisioning
}
// Add branch name as a dynamic label for all other jobs
const branch = payload.workflow_job?.head_branch || 'unknown';
const labels = [...(defaultLabels || []), 'branch:' + branch.replace(/[^a-zA-Z0-9-]/g, '-')];
console.log('Using default provider with dynamic labels');
return {
provider: defaultProvider,
labels: labels,
};
};
"""),
)
# Create the GitHub runners infrastructure with provider selector
GitHubRunners(
self, "GitHubRunners",
providers=[default_provider, production_provider],
provider_selector=provider_selector,
)
app = cdk.App()
ProviderSelectorStack(app, "provider-selector-example")
app.synth()