Test data output as json #602
This file contains hidden or 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: Test data output as json | |
on: | |
pull_request: | |
paths-ignore: | |
- 'README.md' | |
- 'LICENSE' | |
- '.gitlab-ci.yml.example' | |
- '.gitignore' | |
schedule: | |
# only run once a week to show the action is working and preserve as much energy as possible | |
# Reason being that we pull our ML model and this could have changed in the meantime | |
- cron: '22 4 * * 6' | |
workflow_dispatch: | |
jobs: | |
test-data-output-action: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v5 | |
with: | |
fetch-depth: 0 | |
- name: Initialize Energy Estimation | |
uses: ./ | |
with: | |
task: start-measurement | |
json-output: true | |
project: "Eco CI" | |
machine: "ubuntu-latest" | |
tags: "CI/CD,JSON-Workflow" | |
gmt-api-token: ${{ secrets.GMT_API_TOKEN }} | |
co2-calculation-method: "location-based" | |
co2-grid-intensity-api-token: ${{ secrets.ELECTRICITYMAPS_TOKEN }} | |
- uses: actions/setup-node@v5 | |
with: | |
node-version: '18' | |
- name: Sleep 2 | |
run: | | |
sleep 2 | |
- name: Node Setup Energy Measurment | |
id: data-node-setup | |
uses: ./ | |
with: | |
task: get-measurement | |
label: 'node setup' | |
- name: Print node setup data | |
run: | | |
echo "total json: ${{ steps.data-node-setup.outputs.data-lap-json }}" | |
- name: Assert node setup output is appropriate | |
shell: bash | |
run: | | |
# setup | |
output='${{ steps.data-node-setup.outputs.data-lap-json }}' | |
required_label="node setup" | |
required_fields=("repository" "branch" "workflow" "run_id" "steps") | |
required_count=${#required_fields[@]} | |
# assertions | |
if [ -z "$output" ]; then | |
echo "Output must not be empty" | |
exit 1 | |
fi | |
fields_count=$(echo "$output" | jq 'keys | length') | |
if ! [ "${fields_count}" -eq "${required_count}" ]; then | |
echo "output has ${fields_count} instead of ${required_count}" | |
exit 1 | |
fi | |
for field in "${required_fields[@]}"; do | |
if ! echo "$output" | jq -e --arg field "$field" 'has($field)' >/dev/null; then | |
echo "output is missing the field: $field" | |
exit 1 | |
fi | |
done | |
- name: Wait before the next step | |
run: sleep 10 | |
- name: Sleep measurement | |
id: data-sleep | |
uses: ./ | |
with: | |
task: get-measurement | |
label: "sleep" | |
- name: Print sleep data | |
run: | | |
echo "total json: ${{ steps.data-sleep.outputs.data-lap-json }}" | |
- name: Assert sleep output is appropriate | |
shell: bash | |
run: | | |
# setup | |
output='${{ steps.data-sleep.outputs.data-lap-json }}' | |
required_label="sleep" | |
required_fields=("repository" "branch" "workflow" "run_id" "steps") | |
required_count=${#required_fields[@]} | |
# assertions | |
if [ -z "$output" ]; then | |
echo "Output must not be empty" | |
exit 1 | |
fi | |
fields_count=$(echo "$output" | jq 'keys | length') | |
if ! [ "${fields_count}" -eq "${required_count}" ]; then | |
echo "output has ${fields_count} instead of ${required_count}" | |
exit 1 | |
fi | |
for field in "${required_fields[@]}"; do | |
if ! echo "$output" | jq -e --arg field "$field" 'has($field)' >/dev/null; then | |
echo "output is missing the field: $field" | |
exit 1 | |
fi | |
done | |
- name: Final | |
uses: ./ | |
with: | |
task: get-measurement | |
label: "final" | |
# must be called here, otherwise we cannot access JSON | |
- name: Eco CI Energy Estimation | |
id: data-total | |
uses: ./ | |
with: | |
task: display-results |