From 2e6b4076f3d8ce30a430ffa0b440567751b67485 Mon Sep 17 00:00:00 2001 From: pweimann Date: Wed, 30 Oct 2024 11:33:33 +0100 Subject: [PATCH] ci: implement energy consumption check (WIP) --- .github/workflows/ci.yaml | 54 +++++++++++++++++-- .../energy-consumption-baseline.yaml | 15 ++++++ 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/energy-consumption-baseline.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1150cb1..da06172 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,6 +1,13 @@ name: lpt-application-ui CI -on: [push] +on: + push: + workflow_call: + inputs: + upload: + default: false + required: false + type: boolean permissions: actions: read @@ -85,8 +92,45 @@ jobs: uses: green-coding-solutions/eco-ci-energy-estimation@v4 with: task: display-results - - name: Store Baseline - uses: actions/upload-artifact@v3 + - 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: test + run: | + echo "runId: ${{steps.get-baseline.outputs.baseline_run_id}}" + - name: Download Baseline Energy Consumption + uses: actions/download-artifact@v4 + with: + name: energy-data + path: energy-data.json + github-token: ${{ github.token }} + repository: ${{ github.repository }} + run-id: ${{ steps.get-baseline.outputs.baseline_run_id }} + - name: Compare energy consumption + run: | + echo "Comparing energy consumption with baseline" + CURRENT_ENERGY=$(jq '.energy_joules' /tmp/eco-ci/total-data.json) + BASELINE_ENERGY=$(jq '.energy_joules' baseline-energy-data/total-data.json) + + echo "Current energy consumption: ${CURRENT_ENERGY} joules" + echo "Baseline energy consumption: ${BASELINE_ENERGY} joules" + + DIFF=$(echo "scale=2; (($CURRENT_ENERGY - $BASELINE_ENERGY) / $BASELINE_ENERGY) * 100" | bc) + + echo "Energy consumption difference: ${DIFF}%" + - name: Upload energy data + if: inputs.upload == true + uses: actions/upload-artifact@v4 with: - name: energy-baseline - path: steps.data-total.outputs.data-total-json + name: energy-data + path: /tmp/eco-ci/total-data.json + retention-days: 10 diff --git a/.github/workflows/energy-consumption-baseline.yaml b/.github/workflows/energy-consumption-baseline.yaml new file mode 100644 index 0000000..c8b05e1 --- /dev/null +++ b/.github/workflows/energy-consumption-baseline.yaml @@ -0,0 +1,15 @@ +name: store energy baseline + +on: +# push: + workflow_dispatch: + +permissions: + actions: read + contents: read + +jobs: + call-ci: + uses: ./.github/workflows/ci.yaml + with: + upload: true