Skip to content

Download DBIP data and generate dbip_country.rs #24

Download DBIP data and generate dbip_country.rs

Download DBIP data and generate dbip_country.rs #24

Workflow file for this run

name: Download DBIP data and generate dbip_country.rs
on:
schedule:
- cron: "0 0 3 * *" # Runs at midnight on the 3rd of every month
env:
TEMP_DIR: '/tmp'
YEAR_MONTH: '01-9999'
permissions:
contents: write
jobs:
generate:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Download DBIP data in MMDB format
run: |
export YEAR_MONTH=$(date +%Y-%m)
echo "TEMP_DIR=$(mktemp -d)" >> $GITHUB_ENV
echo "YEAR_MONTH=$YEAR_MONTH" >> $GITHUB_ENV
cd ip_country
mkdir -p dbip-data
curl -L -o dbip-data/dbip-country-lite.mmdb.gz "https://download.db-ip.com/free/dbip-country-lite-$YEAR_MONTH.mmdb.gz"
gunzip dbip-data/dbip-country-lite.mmdb.gz
- name: Generate Rust source file
run: |
cd ip_country
cargo run < "dbip-data/dbip-country-lite.mmdb" > "$TEMP_DIR"/dbip_country.rs
ls "$TEMP_DIR"
- name: Commit and push generated file
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch
BRANCH="$(git ls-remote --exit-code --heads origin generated-source 2>/dev/null || true)"
if [[ "$BRANCH" == "" ]]
then
git checkout -B generated-source
git rm -rf .
mkdir -p ip_country/src
else
git checkout generated-source
fi
DIFF="$(diff "${TEMP_DIR}"/dbip_country.rs ip_country/src/dbip_country.rs || true)"
if [[ "$DIFF" != "" || "$BRANCH" == "" ]]
then
mv "${TEMP_DIR}"/dbip_country.rs ip_country/src/dbip_country.rs
git add ip_country/src/dbip_country.rs
git commit -m "Update generated dbip_country ${YEAR_MONTH} Rust source file" || true
git push -u origin HEAD
fi