gitgreen 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/init.js +22 -71
  2. package/package.json +1 -1
package/dist/init.js CHANGED
@@ -80,80 +80,27 @@ const setVariable = async (auth, project, key, value, masked = false) => {
80
80
  return setVariableApi(auth.baseUrl, auth.pat, project, key, value, masked);
81
81
  }
82
82
  };
83
- const generateCiJob = (runnerTag) => {
83
+ const generateCiJob = (opts = {}) => {
84
+ const { runnerTag, carbonBudget, failOnBudget } = opts;
85
+ let inputs = ` gcp_project_id: $GCP_PROJECT_ID
86
+ gcp_instance_id: $GCP_INSTANCE_ID
87
+ gcp_zone: $GCP_ZONE
88
+ machine_type: $MACHINE_TYPE`;
89
+ if (carbonBudget) {
90
+ inputs += `\n carbon_budget_grams: "${carbonBudget}"`;
91
+ }
92
+ if (failOnBudget) {
93
+ inputs += `\n fail_on_budget: true`;
94
+ }
84
95
  const tagsSection = runnerTag ? `\n tags:\n - ${runnerTag}` : '';
85
96
  return `# GitGreen carbon analysis (generated by gitgreen init)
86
- # Fetches real CPU and RAM metrics from GCP Monitoring API
97
+ include:
98
+ - component: gitlab.com/youneslaaroussi/gitgreen/gitgreen-cli-component@0.1.0
99
+ inputs:
100
+ ${inputs}
87
101
 
88
102
  carbon_analysis:
89
- stage: test${tagsSection}
90
- before_script:
91
- - echo "$GCP_SA_KEY_BASE64" | base64 -d > /tmp/gcp-key.json
92
- - gcloud auth activate-service-account --key-file=/tmp/gcp-key.json
93
- - npm install ./gitgreen-cli-0.1.0.tgz
94
- script:
95
- - |
96
- TOKEN=$(gcloud auth print-access-token)
97
- START_TIME=$(date -u -d "$CI_PIPELINE_CREATED_AT" +%Y-%m-%dT%H:%M:%SZ)
98
- END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
99
-
100
- CPU_FILTER="resource.type=\\"gce_instance\\" AND resource.labels.instance_id=\\"$GCP_INSTANCE_ID\\" AND resource.labels.zone=\\"$GCP_ZONE\\" AND metric.type=\\"compute.googleapis.com/instance/cpu/utilization\\""
101
- CPU_ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$CPU_FILTER'''))")
102
-
103
- RAM_USED_FILTER="resource.type=\\"gce_instance\\" AND resource.labels.instance_id=\\"$GCP_INSTANCE_ID\\" AND metric.type=\\"compute.googleapis.com/instance/memory/balloon/ram_used\\""
104
- RAM_USED_ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$RAM_USED_FILTER'''))")
105
-
106
- RAM_SIZE_FILTER="resource.type=\\"gce_instance\\" AND resource.labels.instance_id=\\"$GCP_INSTANCE_ID\\" AND metric.type=\\"compute.googleapis.com/instance/memory/balloon/ram_size\\""
107
- RAM_SIZE_ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$RAM_SIZE_FILTER'''))")
108
-
109
- # Calculate expected data points (GCP reports every 60s)
110
- START_SEC=$(date -d "$START_TIME" +%s)
111
- NOW_SEC=$(date +%s)
112
- DURATION_SEC=$((NOW_SEC - START_SEC))
113
- EXPECTED_POINTS=$((DURATION_SEC / 60))
114
- [ "$EXPECTED_POINTS" -lt 1 ] && EXPECTED_POINTS=1
115
-
116
- echo "Pipeline duration: \${DURATION_SEC}s, expecting ~\$EXPECTED_POINTS data points"
117
- echo "Waiting for metrics data (GCP has ~3 min lag)..."
118
-
119
- for i in $(seq 1 30); do
120
- END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
121
- CPU_DATA=$(curl -s -H "Authorization: Bearer $TOKEN" \\
122
- "https://monitoring.googleapis.com/v3/projects/$GCP_PROJECT_ID/timeSeries?filter=$CPU_ENCODED&interval.startTime=$START_TIME&interval.endTime=$END_TIME")
123
-
124
- POINTS=$(echo "$CPU_DATA" | python3 -c "import sys,json; d=json.load(sys.stdin); print(sum(len(ts.get('points',[])) for ts in d.get('timeSeries',[])))" 2>/dev/null || echo "0")
125
-
126
- if [ "$POINTS" -ge "$EXPECTED_POINTS" ]; then
127
- echo "Got $POINTS CPU data points (expected $EXPECTED_POINTS)"
128
- break
129
- fi
130
- echo "Waiting for data... got $POINTS/$EXPECTED_POINTS (attempt $i/30)"
131
- sleep 10
132
- done
133
-
134
- # Fetch RAM data
135
- RAM_USED_DATA=$(curl -s -H "Authorization: Bearer $TOKEN" \\
136
- "https://monitoring.googleapis.com/v3/projects/$GCP_PROJECT_ID/timeSeries?filter=$RAM_USED_ENCODED&interval.startTime=$START_TIME&interval.endTime=$END_TIME")
137
-
138
- RAM_SIZE_DATA=$(curl -s -H "Authorization: Bearer $TOKEN" \\
139
- "https://monitoring.googleapis.com/v3/projects/$GCP_PROJECT_ID/timeSeries?filter=$RAM_SIZE_ENCODED&interval.startTime=$START_TIME&interval.endTime=$END_TIME")
140
-
141
- # Save timeseries to files
142
- echo "$CPU_DATA" > /tmp/cpu_timeseries.json
143
- echo "$RAM_USED_DATA" > /tmp/ram_used_timeseries.json
144
- echo "$RAM_SIZE_DATA" > /tmp/ram_size_timeseries.json
145
-
146
- CMD="./node_modules/.bin/gitgreen --provider gcp --machine $MACHINE_TYPE --region $GCP_ZONE --cpu-timeseries /tmp/cpu_timeseries.json --ram-used-timeseries /tmp/ram_used_timeseries.json --ram-size-timeseries /tmp/ram_size_timeseries.json --out-md carbon-report.md --out-json carbon-report.json --no-gitlab"
147
- [ -n "\${CARBON_BUDGET_GRAMS:-}" ] && CMD="$CMD --budget $CARBON_BUDGET_GRAMS"
148
- [ "\${FAIL_ON_BUDGET:-}" = "true" ] && CMD="$CMD --fail-on-budget"
149
-
150
- eval "$CMD"
151
- cat carbon-report.md
152
- artifacts:
153
- paths:
154
- - carbon-report.md
155
- - carbon-report.json
156
- expire_in: 30 days
103
+ extends: .gitgreen-carbon-analysis${tagsSection}
157
104
  `;
158
105
  };
159
106
  const runInit = async (opts = {}) => {
@@ -747,7 +694,11 @@ systemctl start gitlab-runner
747
694
  message: 'Add job to .gitlab-ci.yml?',
748
695
  initial: true
749
696
  });
750
- const ciJob = generateCiJob(runnerTag || undefined);
697
+ const ciJob = generateCiJob({
698
+ runnerTag: runnerTag || undefined,
699
+ carbonBudget: carbonBudget || undefined,
700
+ failOnBudget
701
+ });
751
702
  if (addCiJob) {
752
703
  const ciPath = path_1.default.join(process.cwd(), '.gitlab-ci.yml');
753
704
  if (fs_1.default.existsSync(ciPath)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitgreen",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "GitGreen CLI for carbon reporting in GitLab pipelines (GCP/AWS)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",