-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircleci-config.yml
More file actions
89 lines (83 loc) · 2.99 KB
/
Copy pathcircleci-config.yml
File metadata and controls
89 lines (83 loc) · 2.99 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
# Carbon-Aware CI for CircleCI
#
# Add this to your .circleci/config.yml or use as an orb.
# Set environment variables in CircleCI Project Settings:
# GRID_ZONE: Grid zone to check (default: auto:detect)
# MAX_CARBON: Max gCO2eq/kWh threshold (default: 250)
# ELECTRICITY_MAPS_TOKEN: Optional, for global coverage
# ENTSOE_TOKEN: Optional, for EU coverage
#
# Usage: Add the carbon-check job before your heavy build jobs.
# The build job only runs if the grid is clean.
version: 2.1
jobs:
carbon-check:
docker:
- image: cimg/python:3.11
steps:
- checkout
- run:
name: Install dependencies
command: pip install -q uv && uv pip install --system -q requests==2.32.*
- run:
name: Check grid carbon intensity
command: |
python -c "
import os, sys
sys.path.insert(0, '.')
from check_grid import parse_zones_input, check_carbon_intensity
from providers import detect_provider
zone_str = os.environ.get('GRID_ZONE', 'auto:detect')
max_carbon = float(os.environ.get('MAX_CARBON', '250'))
entsoe_token = os.environ.get('ENTSOE_TOKEN', '')
zones = parse_zones_input(zone_str)
zone = zones[0]['zone'] if zones else 'CISO'
provider = detect_provider(zone, entsoe_token)
is_green, intensity = check_carbon_intensity(
zone, max_carbon, provider,
eia_api_key=os.environ.get('EIA_API_KEY', ''),
emaps_api_key=os.environ.get('ELECTRICITY_MAPS_TOKEN', ''),
entsoe_token=entsoe_token,
)
result = 'true' if is_green else 'false'
# Write to file for downstream jobs
with open('/tmp/carbon_result.txt', 'w') as f:
f.write(result)
with open('/tmp/carbon_intensity.txt', 'w') as f:
f.write(str(intensity or 'unknown'))
print(f'Grid clean: {result} ({intensity} gCO2eq/kWh)')
"
- persist_to_workspace:
root: /tmp
paths:
- carbon_result.txt
- carbon_intensity.txt
build:
docker:
- image: cimg/base:current
steps:
- attach_workspace:
at: /tmp
- run:
name: Check carbon result
command: |
GRID_CLEAN=$(cat /tmp/carbon_result.txt)
if [ "$GRID_CLEAN" != "true" ]; then
echo "Grid is dirty ($(cat /tmp/carbon_intensity.txt) gCO2eq/kWh). Skipping build."
circleci-agent step halt
fi
echo "Grid is clean! Running build on green energy..."
- checkout
- run:
name: Build
command: |
echo "Your build commands here"
workflows:
carbon-aware-build:
# Schedule: run hourly (CircleCI scheduled triggers)
# Configure in CircleCI UI: Project Settings → Triggers
jobs:
- carbon-check
- build:
requires:
- carbon-check