F1 database updater #113
This file contains 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: F1 database updater | |
on: | |
workflow_dispatch: | |
inputs: | |
distinct_id: | |
description: "A unique identifier for the workflow run" | |
required: true | |
jobs: | |
setup: | |
name: Setup env variables and check if update is needed | |
runs-on: ubuntu-latest | |
outputs: | |
race_id: ${{ steps.setup.outputs.race_id }} | |
round: ${{ steps.setup.outputs.round }} | |
race_name: ${{ steps.setup.outputs.race_name }} | |
should_update: ${{ steps.setup.outputs.should_update }} | |
is_sprint: ${{ steps.setup.outputs.is_sprint }} | |
services: | |
database: | |
image: ghcr.io/race-tech/f1-db:latest | |
env: | |
MYSQL_DATABASE: f1db | |
MYSQL_ROOT_PASSWORD: password | |
ports: | |
- 13306:3306 | |
options: --health-cmd="mysqladmin ping -u root" --health-interval=10s --health-timeout=5s --health-retries=3 | |
steps: | |
- name: echo distinct ID ${{ github.event.inputs.distinct_id }} | |
run: echo ${{ github.event.inputs.distinct_id }} | |
- name: Wait for MySQL to initialize | |
run: sleep 15 # Adding a delay | |
- name: Verify f1db DB exists | |
run: | | |
export MYSQL_PWD=password | |
mysql --host 127.0.0.1 --port 13306 -uroot -e "SHOW DATABASES LIKE 'f1db'" | |
- name: Setup env variables | |
id: setup | |
shell: bash | |
run: | | |
export MYSQL_PWD=password | |
race_id=$(echo "SELECT r.raceId FROM races AS r WHERE r.date < '$(date +'%Y-%m-%d')' ORDER BY r.date DESC LIMIT 1" | mysql -h 127.0.0.1 -P 13306 --protocol=tcp -u root -D f1db | tail -1 | xargs) | |
round=$(echo "SELECT r.round FROM races AS r WHERE r.date < '$(date +'%Y-%m-%d')' ORDER BY r.date DESC LIMIT 1" | mysql -h 127.0.0.1 -P 13306 --protocol=tcp -u root -D f1db | tail -1 | xargs) | |
race_name=$(echo "SELECT r.name FROM races AS r WHERE r.date < '$(date +'%Y-%m-%d')' ORDER BY r.date DESC LIMIT 1" | mysql -h 127.0.0.1 -P 13306 --protocol=tcp -u root -D f1db | tail -1 | xargs) | |
sprint_race_date=$(echo "SELECT r.sprint_date FROM races AS r WHERE r.date < '$(date +'%Y-%m-%d')' ORDER BY r.date DESC LIMIT 1" | mysql -h 127.0.0.1 -P 13306 --protocol=tcp -u root -D f1db | tail -1 | xargs) | |
should_update=$([ $(echo "SELECT resultId FROM results WHERE raceId = $race_id" | mysql -h 127.0.0.1 -P 13306 --protocol=TCP -u root -D f1db | wc -l) -ge 1 ] && echo 'false' || echo 'true') | |
is_sprint=$($([[ $sprint_race_date -eq "NULL" ]]) && echo 'false' || echo 'true') | |
echo "round=$round" >> $GITHUB_OUTPUT | |
echo "race_id=$race_id" >> $GITHUB_OUTPUT | |
echo "race_name=$race_name" >> $GITHUB_OUTPUT | |
echo "should_update=$should_update" >> $GITHUB_OUTPUT | |
echo "is_sprint=$is_sprint" >> $GITHUB_OUTPUT | |
main: | |
name: Update F1 database | |
runs-on: ubuntu-latest | |
needs: setup | |
if: needs.setup.outputs.should_update == 'true' | |
services: | |
database: | |
image: ghcr.io/race-tech/f1-db:latest | |
env: | |
MYSQL_DATABASE: f1db | |
MYSQL_ROOT_PASSWORD: password | |
ports: | |
- 13306:3306 | |
options: --health-cmd="mysqladmin ping -u root" --health-interval=10s --health-timeout=5s --health-retries=3 | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v4 | |
- name: Wait for MySQL to initialize | |
run: sleep 15 # Adding a delay | |
- name: Verify f1db DB exists | |
run: | | |
export MYSQL_PWD=password | |
mysql --host 127.0.0.1 --port 13306 -uroot -e "SHOW DATABASES LIKE 'f1db'" | |
- name: Check params value | |
run: | | |
echo ${{ needs.setup.outputs.round }} | |
echo ${{ needs.setup.outputs.race_name }} | |
echo ${{ needs.setup.outputs.race_id }} | |
echo ${{ needs.setup.outputs.is_sprint }} | |
- name: Build updater image | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
push: false | |
tags: f1db-updater | |
build-args: | | |
ROUND_NUMBER=${{ needs.setup.outputs.round }} | |
ROUND_NAME=${{ needs.setup.outputs.race_name }} | |
IS_SPRINT=${{ needs.setup.outputs.is_sprint }} | |
- name: Run updater | |
uses: addnab/docker-run-action@v3 | |
with: | |
image: f1db-updater | |
run: cargo run --release -- ${{ needs.setup.outputs.round }} ${{ needs.setup.outputs.is_sprint }} | |
docker_network: host | |
- name: Create sql dump | |
run: | | |
export MYSQL_PWD=password | |
mysqldump --host 127.0.0.1 --port 13306 -uroot f1db > f1db.sql | |
gzip f1db.sql | |
- name: Upload sql dump | |
uses: actions/upload-artifact@v4 | |
with: | |
name: f1db.sql.gz | |
path: f1db.sql.gz |