ci: implement energy consumption check (WIP) #74
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |