Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electricitymaps unified #102

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ jobs:
- Eco-CI uses the github api to post/edit PR comments and get the workflow id
- set to github's default api, but can be changed if you are using github enterprise
- `type`: (optional)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "machine.ci".[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- Please note that we will add the label as a tag so you can see which steps generated how much CO2
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "machine.ci". Cannot be empty.[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- `project`: (optional)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "CI/CD".[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "CI/CD". Cannot be empty.[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- `machine`: (optional)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "ubuntu-latest".[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "ubuntu-latest". Cannot be empty.[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- `tags`: (optional)
- If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set tags for drill-down later. Please supply comma separated. Tags cannot have commas itself or contain quotes. Defaults to empty.[CarbonDB](https://www.green-coding.io/projects/carbondb/)
- `authentication-token`: (optional)
- `gmt-api-token`: (optional)
- If you are not using the default user for the GMT API supply your auth token. We recommend to have this as a GitHub Secret.
- `api-endpoint-add`: (optional)
- When using the GMT Dashboard and / or CarbonDB specify the endpoint URL to send to. Defaults to "https://api.green-coding.io/v2/ci/measurement/add"
- `api-endpoint-badge-get`: (optional)
- When using the GMT Dashboard and / or CarbonDB specify the endpoint URL to get the badge from to. Defaults to "https://api.green-coding.io//v1/ci/badge/get

- `electricitymaps-api-token`: (optional)
- API token for electricitymaps in case you get rate-limited. See details below.
- `get-measurement`: Measures the energy at this point in time since either the start-measurement or last get-measurement action call.
- `label`: (optional) (default: 'measurement ##')

Expand All @@ -142,19 +142,9 @@ jobs:

We use https://app.electricitymaps.com/ to get the grid intensity for a given location. This service currently works without specifying a token but we recommend to still get one under https://api-portal.electricitymaps.com/

You will need to set this token as a secret `ELECTRICITY_MAPS_TOKEN`. See the documentation how to do this https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions

You will also need to set it in your workflow files where you call `display-results` and `get-measurement`:
```
- name: Eco CI Energy Estimation
uses: ./
env:
ELECTRICITY_MAPS_TOKEN: ${{ secrets.ELECTRICITY_MAPS_TOKEN }}
with:
task: display-results
pr-comment: true
You will need to set this token as a secret and pass it in the initalization. To learn how to create a secret see the GitHub documentation: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions

```
You will then need to pass it in your workflow files in the initialization. See documentation above.

#### Continuing on Errors

Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ inputs:
description: 'If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set tags for drill-down later. Please supply comma separated. Tags cannot have commas itself or contain quotes. Defaults to empty'
default: ''
required: false
authentication-token:
gmt-api-token:
description: 'If you are not using the default user for the GMT API supply your auth token. We recommend to have this as a GitHub Secret'
default: ''
required: false
Expand All @@ -72,6 +72,10 @@ inputs:
description: 'When using the GMT Dashboard and / or CarbonDB specify the endpoint URL to get the badge from to. Defaults to "https://api.green-coding.io//v1/ci/badge/get'
default: 'https://api.green-coding.io/v1/ci/badge/get'
required: false
electricitymaps-api-token:
description: 'API token for electricitymaps in case you get rate-limited. See documentation for details'
default: ''
required: false

outputs:
data-total-json:
Expand Down Expand Up @@ -106,7 +110,7 @@ runs:
workflow_id=${workflow_id:-"not_set"}
workflow_name=${workflow_name:-"not_set"}

${{github.action_path}}/scripts/setup.sh start_measurement "${{inputs.machine-power-data}}" "${{ github.run_id }}" "${{inputs.branch}}" "${{ github.repository }}" "$workflow_id" "$workflow_name" "${{ github.sha }}" "github" "${{ inputs.send-data }}" "${{ inputs.type }}" "${{ inputs.project }}" "${{ inputs.machine }}" "${{ inputs.tags }}" "${{ inputs.calculate-co2 }}" "${{ inputs.authentication-token }}" "${{ inputs.json-output }}" "${{ inputs.api-endpoint-add }}" "${{ inputs.api-endpoint-badge-get }}"
${{github.action_path}}/scripts/setup.sh start_measurement "${{inputs.machine-power-data}}" "${{ github.run_id }}" "${{inputs.branch}}" "${{ github.repository }}" "$workflow_id" "$workflow_name" "${{ github.sha }}" "github" "${{ inputs.send-data }}" "${{ inputs.type }}" "${{ inputs.project }}" "${{ inputs.machine }}" "${{ inputs.tags }}" "${{ inputs.calculate-co2 }}" "${{ inputs.gmt-api-token }}" "${{ inputs.electricitymaps-api-token }}" "${{ inputs.json-output }}" "${{ inputs.api-endpoint-add }}" "${{ inputs.api-endpoint-badge-get }}"

- if: inputs.task == 'get-measurement'
id: run-lap-model
Expand Down
5 changes: 3 additions & 2 deletions eco-ci-gitlab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ variables:
ECO_CI_MACHINE_POWER_DATA: "gitlab_EPYC_7B12_saas-linux-small-amd64.sh"
ECO_CI_API_ENDPOINT_ADD: "https://api.green-coding.io/v2/ci/measurement/add"
ECO_CI_API_BADGE_GET: "https://api.green-coding.io/v1/ci/badge/get"
ECO_CI_API_AUTHENTICATION_TOKEN: ""
ECO_CI_GMT_API_TOKEN: ""
ECO_CI_ELECTRICITYMAPS_API_TOKEN: ""

.start_measurement:
script:
Expand All @@ -24,7 +25,7 @@ variables:
fi
git clone --depth 1 --single-branch --branch "${ECO_CI_CLONE_BRANCH}" https://github.com/green-coding-solutions/eco-ci-energy-estimation /tmp/eco-ci-repo

/tmp/eco-ci-repo/scripts/setup.sh start_measurement "${ECO_CI_MACHINE_POWER_DATA}" "${CI_PIPELINE_ID}" "${CI_COMMIT_REF_NAME}" "${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}" "${CI_PROJECT_ID}" "gitlab-ci.yml" "${CI_COMMIT_SHA}" "gitlab" "${ECO_CI_SEND_DATA}" "${ECO_CI_FILTER_TYPE}" "${ECO_CI_FILTER_PROJECT}" "${ECO_CI_FILTER_MACHINE}" "${ECO_CI_FILTER_TAGS}" "${ECO_CI_CALCULATE_CO2}" "${ECO_CI_API_AUTHENTICATION_TOKEN}" "${ECO_CI_JSON_OUTPUT}" "${ECO_CI_API_ENDPOINT_ADD}" "${ECO_CI_API_BADGE_GET}"
/tmp/eco-ci-repo/scripts/setup.sh start_measurement "${ECO_CI_MACHINE_POWER_DATA}" "${CI_PIPELINE_ID}" "${CI_COMMIT_REF_NAME}" "${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}" "${CI_PROJECT_ID}" "gitlab-ci.yml" "${CI_COMMIT_SHA}" "gitlab" "${ECO_CI_SEND_DATA}" "${ECO_CI_FILTER_TYPE}" "${ECO_CI_FILTER_PROJECT}" "${ECO_CI_FILTER_MACHINE}" "${ECO_CI_FILTER_TAGS}" "${ECO_CI_CALCULATE_CO2}" "${ECO_CI_GMT_API_TOKEN}" "${ECO_CI_ELECTRICITYMAPS_API_TOKEN}" "${ECO_CI_JSON_OUTPUT}" "${ECO_CI_API_ENDPOINT_ADD}" "${ECO_CI_API_BADGE_GET}"

.get_measurement:
script:
Expand Down
5 changes: 3 additions & 2 deletions local_ci.example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ECO_CI_FILTER_TAGS='' # Tags must be comma separated. Tags cannot have commas it
ECO_CI_CALCULATE_CO2='true'
ECO_CI_JSON_OUTPUT='true'

ECO_CI_API_AUTHENTICATION_TOKEN=''
ECO_CI_GMT_API_TOKEN=''
ECO_CI_ELECTRICITYMAPS_API_TOKEN=''

# Change this to a local installation of the GMT if you have
ECO_CI_API_ENDPOINT_ADD='https://api.green-coding.io/v2/ci/measurement/add'
Expand All @@ -31,7 +32,7 @@ ECO_CI_MACHINE_POWER_DATA="default.sh"
# Initialize
echo "Initialize"

$shell "$(dirname "$0")/scripts/setup.sh" start_measurement "$ECO_CI_MACHINE_POWER_DATA" "MY_RUN_ID" "NO_BRANCH" "LOCAL_TEST_REPO" "$ECO_CI_WORKFLOW_ID" "MY WORKFLOW NAME" "NO SHA" "local" "$ECO_CI_SEND_DATA" "$ECO_CI_FILTER_TYPE" "$ECO_CI_FILTER_PROJECT" "$ECO_CI_FILTER_MACHINE" "$ECO_CI_FILTER_TAGS" "$ECO_CI_CALCULATE_CO2" "$ECO_CI_API_AUTHENTICATION_TOKEN" "$ECO_CI_JSON_OUTPUT" "$ECO_CI_API_ENDPOINT_ADD" "$ECO_CI_API_BADGE_GET"
$shell "$(dirname "$0")/scripts/setup.sh" start_measurement "$ECO_CI_MACHINE_POWER_DATA" "MY_RUN_ID" "NO_BRANCH" "LOCAL_TEST_REPO" "$ECO_CI_WORKFLOW_ID" "MY WORKFLOW NAME" "NO SHA" "local" "$ECO_CI_SEND_DATA" "$ECO_CI_FILTER_TYPE" "$ECO_CI_FILTER_PROJECT" "$ECO_CI_FILTER_MACHINE" "$ECO_CI_FILTER_TAGS" "$ECO_CI_CALCULATE_CO2" "$ECO_CI_GMT_API_TOKEN" "$ECO_CI_ELECTRICITYMAPS_API_TOKEN" "$ECO_CI_JSON_OUTPUT" "$ECO_CI_API_ENDPOINT_ADD" "$ECO_CI_API_BADGE_GET"

echo "Duration: "$(($(date "+%s%6N") - $(cat /tmp/eco-ci/timer-total.txt))) "us"

Expand Down
2 changes: 1 addition & 1 deletion scripts/make_measurement.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function make_measurement() {

curl -X POST "${ECO_CI_API_ENDPOINT_ADD}" \
-H 'Content-Type: application/json' \
-H "X-Authentication: ${ECO_CI_API_AUTHENTICATION_TOKEN}" \
-H "X-Authentication: ${ECO_CI_GMT_API_TOKEN}" \
-d "{
\"energy_uj\":\"${energy_uj}\",
\"cpu\":\"${model_name_uri}\",
Expand Down
6 changes: 3 additions & 3 deletions scripts/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ get_geoip() {
}

get_carbon_intensity() {
if [ -z "${ELECTRICITY_MAPS_TOKEN+x}" ]; then
export ELECTRICITY_MAPS_TOKEN='no_token'
if [ -z "${ECO_CI_ELECTRICITYMAPS_API_TOKEN+x}" ]; then
export ECO_CI_ELECTRICITYMAPS_API_TOKEN='no_token'
fi

ECO_CI_GEO_LAT=${ECO_CI_GEO_LAT:-}
ECO_CI_GEO_LON=${ECO_CI_GEO_LON:-}

response=$(curl -s -H "auth-token: ${ELECTRICITY_MAPS_TOKEN}" "https://api.electricitymap.org/v3/carbon-intensity/latest?lat=${ECO_CI_GEO_LAT}&lon=${ECO_CI_GEO_LON}" || true)
response=$(curl -s -H "auth-token: ${ECO_CI_ELECTRICITYMAPS_API_TOKEN}" "https://api.electricitymap.org/v3/carbon-intensity/latest?lat=${ECO_CI_GEO_LAT}&lon=${ECO_CI_GEO_LON}" || true)

if [[ -z "$response" ]] || ! echo "$response" | jq empty; then
echo 'Failed to retrieve data or received invalid JSON. Exiting' >&2
Expand Down
15 changes: 8 additions & 7 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ function start_measurement {
add_var 'ECO_CI_FILTER_MACHINE' "${12}"
add_var 'ECO_CI_FILTER_TAGS' "${13}"
add_var 'ECO_CI_CALCULATE_CO2' "${14}"
add_var 'ECO_CI_API_AUTHENTICATION_TOKEN' "${15}"
add_var 'ECO_CI_JSON_OUTPUT' "${16}"
add_var 'ECO_CI_API_ENDPOINT_ADD' "${17}"
add_var 'ECO_CI_API_ENDPOINT_BADGE_GET' "${18}"
add_var 'ECO_CI_GMT_API_TOKEN' "${15}"
add_var 'ECO_CI_ELECTRICITYMAPS_API_TOKEN' "${16}"
add_var 'ECO_CI_JSON_OUTPUT' "${17}"
add_var 'ECO_CI_API_ENDPOINT_ADD' "${18}"
add_var 'ECO_CI_API_ENDPOINT_BADGE_GET' "${19}"

touch /tmp/eco-ci/cpu-util-step.txt
touch /tmp/eco-ci/cpu-util-total.txt
Expand All @@ -47,9 +48,9 @@ function start_measurement {

if [[ "${14}" == 'true' ]]; then
source "$(dirname "$0")/misc.sh"
get_geoip # will set $GEO_CITY, $GEO_LAT, $GEO_LONG and $GEO_IP
get_geoip # will set $ECO_CI_GEO_CITY, $ECO_CI_GEO_LAT, $ECO_CI_GEO_LONG and $ECO_CI_GEO_IP
read_vars # reload set vars
get_carbon_intensity # will set $CO2_INTENSITY
get_carbon_intensity # will set $ECO_CI_CO2I
fi

# Capture current cpu util file and trim trailing empty lines from the file to not run into read/write race condition later
Expand Down Expand Up @@ -93,7 +94,7 @@ fi

case $option in
start_measurement)
start_measurement "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}"
start_measurement "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}" "${20}"
;;
lap_measurement)
lap_measurement
Expand Down