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

Add IP2Location.io API #97

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
- Send metrics data to metrics.green-coding.io to create and display badge, and see an overview of the energy of your CI runs. Set to false to send no data. The data we send are: the energy value and duration of measurement; cpu model; repository name/branch/workflow_id/run_id; commit_hash; source (GitHub or GitLab). We use this data to display in our green-metrics-tool front-end here: https://metrics.green-coding.io/ci-index.html
- `calculate-co2`: (optional) (default: true)
- You might typically always want this value to be shown unless you are in a restricted network and cannot make outbound requests
- Gets the location using https://ipapi.co/
- Gets the location using https://ipapi.co/ or https://www.ip2location.io/
- Get the CO2 grid intensity for the location from https://www.electricitymaps.com/
- Estimates the amount of carbon the measurement has produced
- `gh-api-base`: (optional) (default: 'api.github.com')
Expand Down Expand Up @@ -149,6 +149,25 @@ You will need to set this token as a secret and pass it in the initalization. To

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

#### IP2LOCATIONIO API Key

You can choose to use IP2Location.io API instead of the default ipapi.co API. You will need a IP2Location.io API Key if you wish to use the API,and you can easily obtain a free one at here: https://www.ip2location.io/sign-up.

To use IP2Location.io API as the default API, you will first need to add a new variable called `USE_IP2LOCATION_API`. To learn more on how to set up, kindly refer to GitHub documentation to get started: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#creating-configuration-variables-for-a-repository. Set the value of the variable to `true` to use the API.

You will need to set the API key as a secret `IP2LOCATIONIO_API_KEY`. 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, for example:
```
- name: Start Measurement
uses: ./
env:
USE_IP2LOCATION_API: ${{ vars.USE_IP2LOCATION_API }}
IP2LOCATIONIO_API_KEY: ${{ secrets.IP2LOCATIONIO_API_KEY }}
with:
task: start-measurement
```

#### Continuing on Errors

Once you have initially set up Eco-CI and have given it a test spin we recommend running our action
Expand Down
40 changes: 38 additions & 2 deletions scripts/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@ set -euo pipefail
source "$(dirname "$0")/vars.sh"

get_geoip() {
response=$(curl -s https://ipapi.co/json || true)
USE_IP2LOCATION_API=${USE_IP2LOCATION_API:-false}

# Check if the IP2Location.io API key is provided and non-empty (after trimming)
trimmed_api_key=$(echo "$IP2LOCATIONIO_API_KEY" | xargs)

if [ "$USE_IP2LOCATION_API" = "true" ] && [ -n "$trimmed_api_key" ]; then
echo "Detected IP2Location.io API Key, will use IP2Location.io API key now."
http_code=$(curl -s -w "%{http_code}" -o /tmp/response_body.txt https://api.ip2location.io/?key=$IP2LOCATIONIO_API_KEY)
response=$(< /tmp/response_body.txt)
rm /tmp/response_body.txt
if echo "$response" | jq '.latitude, .longitude, .city_name, .ip' | grep -q null; then
echo -e "Required data is missing\nResponse is ${response}\nExiting" >&2
return 1
fi
response=$(echo "$response" | jq '. | .city = .city_name | del(.city_name)')
echo "$response"
else
if [ "$USE_IP2LOCATION_API" = "true" ] && [ -z "$trimmed_api_key" ]; then
echo "USE_IP2LOCATION_API is enabled but IP2LOCATIONIO_API_KEY is not provided or contains only spaces. Exiting." >&2
return 1
fi
http_code=$(curl -s -w "%{http_code}" -o /tmp/response_body.txt https://ipapi.co/json)
response=$(< /tmp/response_body.txt)
rm /tmp/response_body.txt
echo "$response"

if [[ "$http_code" == "429" ]]; then
http_code=$(curl -s -w "%{http_code}" -o /tmp/response_body.txt https://api.ip2location.io/)
response=$(< /tmp/response_body.txt)
rm /tmp/response_body.txt
if echo "$response" | jq '.latitude, .longitude, .city_name, .ip' | grep -q null; then
echo -e "Required data is missing\nResponse is ${response}\nExiting" >&2
return 1
fi
response=$(echo "$response" | jq '. | .city = .city_name | del(.city_name)')
fi
fi

if [[ -z "$response" ]] || ! echo "$response" | jq empty; then
echo "Failed to retrieve data or received invalid JSON. Exiting" >&2
Expand Down Expand Up @@ -83,4 +119,4 @@ get_energy_co2 (){
echo "Failed to get carbon intensity data." >&2
fi

}
}