diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75458a1..b5b381f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - name: Insert to demo database (using env) uses: ./ env: diff --git a/Dockerfile b/Dockerfile index ff1ab42..fe87842 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,7 @@ -FROM burnett0/alpine-php7-cli-pdo-mysql +FROM burnett0/alpine-php85-cli-pdo-mysql COPY db-processor / -COPY docker/entrypoint.sh / -RUN chmod +x /entrypoint.sh +RUN chmod +x /db-processor -ENTRYPOINT ["/entrypoint.sh"] +ENTRYPOINT ["/db-processor"] diff --git a/LICENSE b/LICENSE index 8518549..b670301 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019-2021 Limelight Gaming Ltd. +Copyright (c) 2019-2026 Limelight Gaming Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1b80be4..1c32d35 100644 --- a/README.md +++ b/README.md @@ -35,20 +35,24 @@ # [[input-pass]INPUT_PASS] Set the mysql password # The default value is env INPUT_PASS # Format: String -# [[input-tabl]INPUT_TABL] Set the mysql table, eg: db.tbl +# [[input-tabl]INPUT_TABL] Set the mysql db+table, eg: db.tbl # The default value is env INPUT_TABL # Format: String # [[input-debug]INPUT_DEBUG] Toggle the debug mode # The default value is env INPUT_DEBUG # or false # Format: Boolean/Integer +# [[input-timeo]INPUT_TIMEO] Set the max timeout for PDO +# The default value is env INPUT_TIMEO +# or 5 +# Format: Integer # # EXAMPLES # ./db-processor "column1=value1;column2=value2" ... # #================================================================ #- IMPLEMENTATION -#- version 1.0.0 +#- version 3.0.0 #- author Steven Agyekum #- copyright Copyright (c) Limelight Gaming Ltd. #- license MIT License @@ -64,48 +68,31 @@ We use this product in our CD workflows for tracking some commit messages. ## Usage -### Standalone - -Requirements: - -- MySQL client -- PHP 7 with PDO extension enabled - -Permissions: +Use it as standalone, docker container, or GH action: -``chmod +x ./db-processor`` - -Execution: - -``` -./db-processor -``` +### Docker -or +Build the container using the Dockerfile +```bash +docker build -t db-processor . ``` -./db-processor -``` -when using environment variables. +and run: -### Docker - -Build the container using the Dockerfile and run: - -``` -docker run -e INPUT_MAPS="" -e INPUT_HOST="" -e INPUT_PORT="" -e INPUT_USER="" -e INPUT_PASS="" -e INPUT_TABL="" -e INPUT_DEBUG=true +```bash +docker run --rm -e INPUT_MAPS="" -e INPUT_HOST="" -e INPUT_PORT="3306" -e INPUT_USER="" -e INPUT_PASS="" -e INPUT_TABL="" -e INPUT_DEBUG=true db-processor ``` ### Github Actions You can run this action by using the ``with:`` block -``` +```bash steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - name: Insert to database - uses: limelight-development/db-processor@2.0 + uses: limelight-development/db-processor@3.0 with: maps: column1=value1;column2=value2; host: ${{ secrets.MYSQL_HOST }} @@ -118,11 +105,11 @@ You can run this action by using the ``with:`` block or by using the ``env:`` block for environment variables -``` +```bash steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - name: Insert to database - uses: limelight-development/db-processor@2.0 + uses: limelight-development/db-processor@3.0 env: INPUT_MAPS: column1=value1;column2=value2; INPUT_HOST: ${{ secrets.MYSQL_HOST }} @@ -133,9 +120,9 @@ or by using the ``env:`` block for environment variables INPUT_DEBUG: true ``` -**Full example:** +**Full action example:** -``` +```bash name: CI on: [push] @@ -146,9 +133,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - name: Insert to database - uses: limelight-development/db-processor@2.0 + uses: limelight-development/db-processor@3.0 with: maps: column1=value1;column2=value2; host: ${{ secrets.MYSQL_HOST }} @@ -157,11 +144,36 @@ jobs: pass: ${{ secrets.MYSQL_PASS }} tabl: ${{ secrets.MYSQL_TABL }} debug: true + timeo: 5 ``` ---- -Check synopsis above to get an example. +### Standalone + +Requirements: + +- MySQL client +- PHP 8.5+ with PDO extension enabled + +Permissions: + +``chmod +x ./db-processor`` + +Execution: + +``` +./db-processor +``` + +or + +``` +./db-processor +``` + +when using environment variables. + +--- ## Demo diff --git a/db-processor b/db-processor index cea6e71..a3bfc09 100644 --- a/db-processor +++ b/db-processor @@ -33,20 +33,24 @@ # [[input-pass]INPUT_PASS] Set the mysql password # The default value is env INPUT_PASS # Format: String -# [[input-tabl]INPUT_TABL] Set the mysql table, eg: db.tbl +# [[input-tabl]INPUT_TABL] Set the mysql db+table, eg: db.tbl # The default value is env INPUT_TABL # Format: String # [[input-debug]INPUT_DEBUG] Toggle the debug mode # The default value is env INPUT_DEBUG # or false # Format: Boolean/Integer +# [[input-timeo]INPUT_TIMEO] Set the max timeout for PDO +# The default value is env INPUT_TIMEO +# or 5 +# Format: Integer # # EXAMPLES # ./db-processor "column1=value1;column2=value2" ... # #================================================================ #- IMPLEMENTATION -#- version 1.0.0 +#- version 3.0.0 #- author Steven Agyekum #- copyright Copyright (c) Limelight Gaming Ltd. #- license MIT License @@ -64,6 +68,7 @@ $INPUT_USER = $ARGV[4] ?? env("INPUT_USER") ?: err('ERR_NO_INPUT_USER', 4); $INPUT_PASS = $ARGV[5] ?? env("INPUT_PASS") ?: err('ERR_NO_INPUT_PASS', 5); $INPUT_TABL = $ARGV[6] ?? env("INPUT_TABL") ?: err('ERR_NO_INPUT_TABL', 6); $INPUT_DEBUG = $ARGV[7] ?? env("INPUT_DEBUG") ?: 0; +$INPUT_TIMEO = $ARGV[8] ?? env("INPUT_TIMEO") ?: 5; # Extract key mappings $EXPRESSION = "/(?:\b(\w+)\=\b([\w\s=+:.?!+\/\\´'\"§%&$,)(#-]+)\;?)/"; @@ -81,7 +86,7 @@ foreach( $MAPS as $IDX => $KV ) $VALS[$IDX] = $KV[2]; } -$KEYS = join($KEYS, ','); +$KEYS = implode(',', $KEYS); # Invoke debug mode debug($INPUT_DEBUG); @@ -89,9 +94,11 @@ debug($INPUT_DEBUG); # Insert into database try { - $DB = new PDO("mysql:host=${INPUT_HOST};port=${INPUT_PORT}", $INPUT_USER, $INPUT_PASS); - $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $DB->prepare("INSERT INTO ${INPUT_TABL} SET ${KEYS};")->execute($VALS); + $DB = (new PDO("mysql:host=${INPUT_HOST};port=${INPUT_PORT}", $INPUT_USER, $INPUT_PASS, [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_TIMEOUT => $INPUT_TIMEO, + ])) + ->prepare("INSERT INTO ${INPUT_TABL} SET ${KEYS};")->execute($VALS); } catch(PDOException $e) { @@ -122,13 +129,13 @@ function debug($mode) if(!$mode) return; - echo "[KEYMAPPING]-arg extracted:"; + echo "[KEYMAPPING]-arg extracted: "; print_r($MAPS); - echo "\nProcessed SQL Query:\n"; + echo "\nSQL Query:\n"; echo "INSERT INTO ${INPUT_TABL} SET ${KEYS};"; - echo "\nPrepared-Statement values:"; + echo "\nPrepared-Statement values: "; print_r($VALS); } ?> diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh deleted file mode 100644 index dca74af..0000000 --- a/docker/entrypoint.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -chmod +x /db-processor - -/db-processor