-
Notifications
You must be signed in to change notification settings - Fork 0
160 lines (150 loc) · 5.79 KB
/
ci.yaml
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: lpt-application-ui CI
on:
push:
workflow_call:
inputs:
upload:
default: false
required: false
type: boolean
permissions:
actions: read
contents: read
jobs:
build:
timeout-minutes: 2
runs-on: ubuntu-latest
steps:
- name: Eco-CI Init
uses: green-coding-berlin/eco-ci-energy-estimation@main
with:
task: start-measurement
json-output: true
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout Repo Measurement
uses: green-coding-solutions/eco-ci-energy-estimation@v4
with:
task: get-measurement
label: 'repo checkout'
- name: Get npm cache directory
id: npm-cache-dir
shell: bash
run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT}
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Npm Cache Measurement
uses: green-coding-solutions/eco-ci-energy-estimation@v4
with:
task: get-measurement
label: 'cache npm dependencies'
- name: Install dependencies
if: steps.cache-npm.outputs.cache-hit != 'true'
run: npm ci --prefer-offline --no-audit --no-progress --no-fund
- name: Install Dependencies Measurement
uses: green-coding-solutions/eco-ci-energy-estimation@v4
with:
task: get-measurement
label: 'install dependencies'
# - name: Check linting across the board
# shell: bash
# run: npm run lint:ci
# - name: Lint Measurement
# uses: green-coding-solutions/eco-ci-energy-estimation@v4
# with:
# task: get-measurement
# label: 'lint app'
# - name: Build application
# shell: bash
# run: npm run build:gcp
# - name: Build Measurement
# uses: green-coding-solutions/eco-ci-energy-estimation@v4
# with:
# task: get-measurement
# label: 'build:gcp app'
# - name: Run unit tests
# shell: bash
# run: |
# # no tests to run:
# npm run test:cover || echo 0
# - name: Unit Tests Measurement
# uses: green-coding-solutions/eco-ci-energy-estimation@v4
# with:
# task: get-measurement
# label: 'unit tests'
- name: Show Energy Result
id: data-total
uses: green-coding-solutions/eco-ci-energy-estimation@v4
with:
task: display-results
- name: Get last successful baseline run ID
id: get-baseline
run: |
BASELINE_RUN_ID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"repos/${{ github.repository }}/actions/workflows/energy-consumption-baseline.yaml/runs?per_page=1&status=success" \
--jq '.workflow_runs[0].id')
echo "baseline_run_id=${BASELINE_RUN_ID}" >> $GITHUB_OUTPUT
echo "used runId of the energy baseline run: ${BASELINE_RUN_ID}"
env:
GH_TOKEN: ${{ github.token }}
- name: Download Baseline Energy Consumption
uses: actions/download-artifact@v4
with:
name: energy-data
path: ${{ github.workspace }}/baseline-data
github-token: ${{ github.token }}
repository: ${{ github.repository }}
run-id: ${{ steps.get-baseline.outputs.baseline_run_id }}
- name: Compare energy consumption
uses: actions/github-script@v7
with:
script: |
import fs from "fs";
try {
// Read the energy values
const currentData = JSON.parse(fs.readFileSync('/tmp/eco-ci/total-data.json', 'utf8'));
const baselineData = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/baseline-data/total-data.json`, 'utf8'));
const currentEnergy = currentData.energy_joules;
const baselineEnergy = baselineData.energy_joules;
// Calculate percentage difference
let percentDiff = ((currentEnergy - baselineEnergy) / baselineEnergy) * 100;
const threshold = 10;
percentDiff = Number.parseFloat(percentDiff).toFixed(2)
// Log results
console.log('Energy Consumption Analysis:');
console.log(`Current: ${Number.parseFloat(currentEnergy).toFixed(2)} joules`);
console.log(`Baseline: ${Number.parseFloat(baselineEnergy).toFixed(2)} joules`);
console.log(`Difference: ${percentDiff}%`);
let message
// Evaluate results
if (percentDiff > threshold) {
message = `Energy consumption increased by ${percentDiff}% (above ${threshold}% threshold)`;
} else if (percentDiff < 0) {
message = `Energy consumption decreased by ${percentDiff}%`;
} else {
message = `Energy consumption increased by ${percentDiff}% (within ${threshold}% threshold)`;
}
console.log(message)
} catch (error) {
console.log(`Error occurred: ${error.message}`);
}
- name: Upload energy data
if: inputs.upload == true
uses: actions/upload-artifact@v4
with:
name: energy-data
path: /tmp/eco-ci/total-data.json
retention-days: 10