Files
Synthese-PSM_LARRA/.github/workflows/ScriptPython.yml
T
2026-10-23 17:54:08 +02:00

605 lines
25 KiB
YAML

# Workflow d'exécution automatique des scripts python et de compilation Therion (Full)
name: ScriptPython
on:
push:
branches:
- main
paths:
- 'Outputs/SHP/*.shp'
- 'Outputs/DB_Synthese-PSM_LARRA.sql'
workflow_dispatch:
permissions:
contents: write
concurrency:
group: pyThtoQgis
cancel-in-progress: false
jobs:
ScriptPython:
if: github.event_name == 'workflow_dispatch' || github.actor != 'github-actions[bot]'
runs-on: ubuntu-24.04
steps:
# ============================================================
# 1. Téléchargement dépôt et sous-modules privés
# ============================================================
- name: 1. Téléchargement dépôt et sous-modules privés
uses: actions/checkout@v6
with:
fetch-depth: 0
submodules: recursive
token: ${{ secrets.SUBMODULES_TOKEN }}
# token: ${{ secrets.PUSH_TOKEN }}
persist-credentials: false
# ============================================================
# 2. Installation et exécution de Therion
# ============================================================
- name: 2. Installation et exécution de Therion
shell: bash
run: |
set -e
echo "========================================"
echo "INSTALLATION THERION"
echo "========================================"
sudo apt-get update
sudo apt-get install -y --no-install-recommends therion survex
echo ""
echo "========================================"
echo "VERIFICATION THERION / SURVEX"
echo "========================================"
therion --version
cavern --version
echo "========================================"
echo "COMPILATION FULL"
echo "========================================"
CONFIG="Synthese-PSM_LARRA-Full.thconfig"
if [ ! -f "$CONFIG" ]; then
echo "ERREUR : configuration introuvable : $CONFIG"
exit 1
fi
if therion "$CONFIG"; then
echo "Compilation Therion terminée."
else
THERION_STATUS=$?
echo "::warning::Échec de compilation Therion (code ${THERION_STATUS}). Poursuite du workflow."
fi
# ============================================================
# 3. Vérification des fichiers SHP et SQL modifiés
# ============================================================
- name: 3. Vérification des fichiers SHP et SQL modifiés
id: changed
shell: bash
env:
BEFORE_SHA: ${{ github.event.before }}
run: |
set -euo pipefail
echo "========================================"
echo "VÉRIFICATION DES MODIFICATIONS"
echo "========================================"
COMMIT_DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "commit_date=$COMMIT_DATE" >> "$GITHUB_OUTPUT"
echo "Date du commit : $COMMIT_DATE"
# --------------------------------------------------------
# Lancement manuel
# --------------------------------------------------------
if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
echo "Workflow lancé manuellement."
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "manual=true" >> "$GITHUB_OUTPUT"
echo "shp=true" >> "$GITHUB_OUTPUT"
echo "sql=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Commit précédent : ${BEFORE_SHA:-non disponible}"
echo "Commit courant : $GITHUB_SHA"
# --------------------------------------------------------
# Récupération des fichiers modifiés
# --------------------------------------------------------
if [ -n "${BEFORE_SHA:-}" ] &&
[[ "$BEFORE_SHA" =~ ^0+$ ]]; then
# Premier push : comparer avec l'arbre vide
CHANGED_FILES=$(git diff-tree --root --no-commit-id --name-only --diff-filter=AM -r "$GITHUB_SHA")
elif [ -n "${BEFORE_SHA:-}" ] &&
git cat-file -e "${BEFORE_SHA}^{commit}" 2>/dev/null; then
# Push normal : comparer les deux extrémités
CHANGED_FILES=$(git diff --no-ext-diff --name-only --diff-filter=AM "$BEFORE_SHA" "$GITHUB_SHA")
else
echo "::error::Commit précédent indisponible. Vérifier actions/checkout et l'événement GitHub."
exit 1
fi
echo ""
echo "Fichiers modifiés détectés par Git :"
if [ -n "$CHANGED_FILES" ]; then
printf '%s\n' "$CHANGED_FILES"
else
echo "Aucun"
fi
# --------------------------------------------------------
# Détection des fichiers SHP, y compris sous-répertoires
# --------------------------------------------------------
SHP_FILES=$(printf '%s\n' "$CHANGED_FILES" | grep -Ei '^Outputs/SHP/.*\.shp$' || true)
# --------------------------------------------------------
# Détection du fichier SQL
# --------------------------------------------------------
SQL_FILE="Outputs/DB_Synthese-PSM_LARRA.sql"
SQL_CHANGED=$(printf '%s\n' "$CHANGED_FILES" | grep -Fx "$SQL_FILE" || true)
# --------------------------------------------------------
# Sorties SHP
# --------------------------------------------------------
echo ""
echo "SHP ajoutés/modifiés :"
if [ -n "$SHP_FILES" ]; then
printf '%s\n' "$SHP_FILES"
echo "shp=true" >> "$GITHUB_OUTPUT"
else
echo "Aucun"
echo "shp=false" >> "$GITHUB_OUTPUT"
fi
# --------------------------------------------------------
# Sorties SQL
# --------------------------------------------------------
echo ""
echo "SQL ajouté/modifié :"
if [ -n "$SQL_CHANGED" ]; then
printf '%s\n' "$SQL_CHANGED"
echo "sql=true" >> "$GITHUB_OUTPUT"
else
echo "Aucun"
echo "sql=false" >> "$GITHUB_OUTPUT"
fi
# --------------------------------------------------------
# Décision de poursuivre
# --------------------------------------------------------
if [ -n "$SHP_FILES" ] || [ -n "$SQL_CHANGED" ]; then
echo ""
echo "SHP ou SQL ajouté/modifié : CONTINUER"
echo "changed=true" >> "$GITHUB_OUTPUT"
else
echo ""
echo "Aucun SHP ni SQL ajouté/modifié."
echo "changed=false" >> "$GITHUB_OUTPUT"
fi
echo "manual=false" >> "$GITHUB_OUTPUT"
# ============================================================
# 4. Arrêt si aucun SHP/SQL et pas de lancement manuel
# ============================================================
- name: 4. Arrêt si aucun SHP/SQL et pas de lancement manuel
if: steps.changed.outputs.changed != 'true'
shell: bash
run: |
echo "Aucun SHP ou SQL ajouté/modifié."
echo "Arrêt du workflow."
# ============================================================
# 5. Vérification de la base SQL
# ============================================================
- name: 5. Vérification de la base SQL
if: steps.changed.outputs.changed == 'true'
shell: bash -el {0}
run: |
set -e
echo "========================================"
echo "CHECK DATABASE"
echo "========================================"
SQL_FILE="Outputs/DB_Synthese-PSM_LARRA.sql"
if [ ! -f "$SQL_FILE" ]; then
echo "ERREUR : fichier SQL introuvable :"
echo "$SQL_FILE"
exit 1
fi
echo "Base SQL trouvée :"
ls -lh "$SQL_FILE"
# ============================================================
# 6. Micromamba
# IMPORTANT :PAS de GDAL ici. GDAL sera fourni par notre package précompilé.
# ============================================================
- name: 6. Micromamba
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
uses: mamba-org/setup-micromamba@v3.2.1
with:
environment-name: pyThtoQgis
create-args: >-
python=3.12
numpy
sfcgal=2.2
geos
proj
sqlite
libtiff
libjpeg-turbo
libpng
zlib
curl
openssl
init-shell: bash
cache-environment: true
cache-downloads: true
# ============================================================
# 7. Informations environnement
# ============================================================
- name: 7. Informations environnement
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
echo "========================================"
echo "ENVIRONMENT"
echo "========================================"
echo "CONDA_PREFIX=$CONDA_PREFIX"
python --version
echo ""
conda list | grep -E "python|numpy|sfcgal|geos|proj|sqlite"
# ============================================================
# 8. Téléchargement package GDAL
# ============================================================
- name: 8. Téléchargement package GDAL
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
env:
GH_TOKEN: ${{ github.token }}
run: |
set -e
GDAL_VERSION=3.13.3
PACKAGE_NAME="gdal-${GDAL_VERSION}-sfcgal-2.2-geos-3.14.1-ubuntu-24.04"
TAG="gdal-sfcgal-${GDAL_VERSION}"
echo "========================================"
echo "GDAL PACKAGE"
echo "========================================"
echo "Release : $TAG"
echo "Package : $PACKAGE_NAME"
gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --pattern "${PACKAGE_NAME}.tar.gz" --dir "$RUNNER_TEMP"
echo ""
echo "Package téléchargé :"
ls -lh "$RUNNER_TEMP/${PACKAGE_NAME}.tar.gz"
# ============================================================
# 9. Installation package GDAL
# ============================================================
- name: 9. Installation package GDAL
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
set -e
GDAL_VERSION=3.13.3
PACKAGE_NAME="gdal-${GDAL_VERSION}-sfcgal-2.2-geos-3.14.1-ubuntu-24.04"
PACKAGE="$RUNNER_TEMP/${PACKAGE_NAME}.tar.gz"
EXTRACT_DIR="$RUNNER_TEMP/gdal-extract"
echo "========================================"
echo "INSTALLATION GDAL"
echo "========================================"
# ========================================================
# Vérification du package
# ========================================================
if [ ! -f "$PACKAGE" ]; then
echo "ERREUR : package introuvable :"
echo "$PACKAGE"
exit 1
fi
# ========================================================
# Extraction
# ========================================================
rm -rf "$EXTRACT_DIR"
mkdir -p "$EXTRACT_DIR"
tar -xzf "$PACKAGE" -C "$EXTRACT_DIR"
# ========================================================
# Affichage STRUCTURE REELLE
# ========================================================
echo ""
echo "========================================"
echo "STRUCTURE DU PACKAGE"
echo "========================================"
find "$EXTRACT_DIR" -maxdepth 6 -print | sort
# ========================================================
# Répertoire racine
# ========================================================
GDAL_DIR="$EXTRACT_DIR/$PACKAGE_NAME"
if [ ! -d "$GDAL_DIR" ]; then
echo ""
echo "ERREUR : répertoire package absent :"
echo "$GDAL_DIR"
exit 1
fi
echo ""
echo "GDAL_DIR :"
echo "$GDAL_DIR"
# ========================================================
# Vérification structure attendue
# ========================================================
echo ""
echo "========================================"
echo "VERIFICATION"
echo "========================================"
if [ ! -d "$GDAL_DIR/bin" ]; then
echo "ERREUR : bin/ absent"
exit 1
fi
if [ ! -d "$GDAL_DIR/lib" ]; then
echo "ERREUR : lib/ absent"
exit 1
fi
# ========================================================
# Installation BIN
# ========================================================
echo ""
echo "Installation bin/..."
cp -av "$GDAL_DIR/bin/." "$CONDA_PREFIX/bin/"
# ========================================================
# Installation LIB
# ========================================================
echo ""
echo "Installation lib/..."
cp -av "$GDAL_DIR/lib/." "$CONDA_PREFIX/lib/"
# ========================================================
# Installation SHARE
# ========================================================
if [ -d "$GDAL_DIR/share" ]; then
echo ""
echo "Installation share/..."
cp -av "$GDAL_DIR/share/." "$CONDA_PREFIX/share/"
fi
# ========================================================
# Vérification GDAL
# ========================================================
echo ""
echo "========================================"
echo "VERIFICATION GDAL"
echo "========================================"
echo ""
echo "gdalinfo :"
which gdalinfo
gdalinfo --version
echo ""
echo "GDAL Python :"
python - <<'PY'
from osgeo import gdal
from osgeo import ogr
print("GDAL :", gdal.VersionInfo("--version"))
print("OGR drivers :", ogr.GetDriverCount())
PY
echo ""
echo "========================================"
echo "GDAL INSTALLE"
echo "========================================"
# ============================================================
# 10. Vérification GDAL
# ============================================================
- name: 10. Vérification GDAL
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
set -e
echo "========================================"
echo "GDAL"
echo "========================================"
which gdalinfo
gdalinfo --version
echo ""
echo "Python GDAL :"
python - <<'PY'
from osgeo import gdal
print(gdal.VersionInfo("--version"))
print()
print(gdal.VersionInfo("--build-info"))
PY
# ============================================================
# 11. Vérification SFCGAL
# ============================================================
- name: 11. Vérification SFCGAL
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
set -e
echo "========================================"
echo "GDAL / SFCGAL"
echo "========================================"
ldd "$CONDA_PREFIX/lib/libgdal.so" | grep -Ei "sfcgal|geos"
# ============================================================
# 12. Test SFCGAL
# ============================================================
- name: 12. Test SFCGAL
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
python - <<'PY'
from osgeo import ogr
from osgeo import gdal
print("========================================")
print("TEST SFCGAL")
print("========================================")
print(gdal.VersionInfo("--version"))
wkt = """
POLYHEDRALSURFACE Z (
(
(0 0 0,
10 0 0,
10 10 0,
0 10 0,
0 0 0)
)
)
"""
geom = ogr.CreateGeometryFromWkt(wkt)
if geom is None:
raise RuntimeError(
"Impossible de créer la géométrie 3D"
)
print("Type :", geom.GetGeometryName())
print("Geometry type :", geom.GetGeometryType())
print("Has Z :", ogr.GT_HasZ(geom.GetGeometryType()))
valid = geom.IsValid()
print("IsValid() :", valid)
print()
print("SFCGAL accessible par OGR : OK")
PY
# ============================================================
# 13. Dépendances Python
# ============================================================
- name: 13. Dépendances Python
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.sql == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
python -m pip install --upgrade pip
pip install alive-progress
pip install pandas
pip install matplotlib
# ============================================================
# 14. Affichage des SHP
# ============================================================
- name: 14. Affichage des SHP
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
echo "========================================"
echo "SHP INPUT"
echo "========================================"
find Outputs/SHP -maxdepth 1 -type f -name "*.shp" -print
# ============================================================
# 15. Exécution pyThtoQgis
# ============================================================
- name: 15. Exécution pyThtoQgis
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
set -e
echo "========================================"
echo "pyThtoQgis"
echo "========================================"
cd Scripts/pyThtoQgis
python pyThtoQgis.py --folder "../../Outputs/SHP/"
# ============================================================
# 16. Exécution pyThStat
# ============================================================
- name: 16. Exécution pyThStat
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
shell: bash -el {0}
run: |
set -e
echo "========================================"
echo "pyThStat"
echo "========================================"
ls -l
cd Scripts/pyThStat
echo "========================================"
ls -l
python pythStat.py --file "../../Outputs/DB_Synthese-PSM_LARRA.sql"
# ============================================================
# 17. Fichiers produits
# ============================================================
- name: 17. Fichiers produits
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.sql == 'true' || steps.changed.outputs.manual == 'true'
shell: bash
run: |
echo "========================================"
echo "GENERATED FILES"
echo "========================================"
git status --short
echo ""
git diff --stat
# ============================================================
# 18. Vérification et configuration de PUSH_TOKEN
# ============================================================
- name: 18. Vérifier PUSH_TOKEN
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.sql == 'true' || steps.changed.outputs.manual == 'true'
shell: bash
env:
PUSH_TOKEN: ${{ secrets.PUSH_TOKEN }}
GH_TOKEN: ${{ secrets.PUSH_TOKEN }}
run: |
set -euo pipefail
REPOSITORY="Alex38Lyon/Synthese-PSM_LARRA"
echo "========================================"
echo "VÉRIFICATION DE PUSH_TOKEN"
echo "========================================"
# 18.1 Vérifier la présence du secret
if [ -z "${PUSH_TOKEN}" ]; then
echo "::error::Le secret PUSH_TOKEN est absent ou vide."
exit 1
fi
# 18.2. Vérifier la validité du jeton
echo "Vérification de l'identité GitHub..."
if ! GH_USER="$(gh api user --jq '.login')"; then
echo "::error::PUSH_TOKEN est invalide ou ne permet pas d'interroger l'API GitHub."
exit 1
fi
echo "Jeton reconnu pour le compte : ${GH_USER}"
# 18.3. Vérifier l'accès au dépôt et les droits du compte
echo "Vérification des droits sur le dépôt..."
if ! PUSH_PERMISSION="$(gh api "repos/${REPOSITORY}" --jq '.permissions.push // false')"; then
echo "::error::Impossible de vérifier les droits sur ${REPOSITORY}."
exit 1
fi
if [ "${PUSH_PERMISSION}" != "true" ]; then
echo "::error::Le compte ${GH_USER} ne dispose pas du droit push sur ${REPOSITORY}."
exit 1
fi
echo "Le compte dispose des droits push sur le dépôt."
# 18.4. Vérifier que le dépôt Git courant est bien le dépôt attendu
REMOTE_URL="$(git remote get-url origin)"
case "${REMOTE_URL}" in
"https://github.com/${REPOSITORY}"|"https://github.com/${REPOSITORY}.git"|"git@github.com:${REPOSITORY}.git")
echo "Dépôt Git vérifié : ${REPOSITORY}"
;;
*)
echo "::error::Le dépôt distant configuré ne correspond pas à ${REPOSITORY}."
exit 1
;;
esac
# 18.5. Retirer les éventuels en-têtes Authorization configurés dans les niveaux local, global et système.
# Ne jamais afficher la valeur des en-têtes.
for SCOPE in local global system; do
git config "--${SCOPE}" --unset-all http.https://github.com/.extraheader 2>/dev/null || true
done
# 6. Configurer un unique en-tête pour PUSH_TOKEN
AUTH_HEADER="$(printf 'x-access-token:%s' "${PUSH_TOKEN}" | base64 -w 0)"
git config --local http.https://github.com/.extraheader "AUTHORIZATION: basic ${AUTH_HEADER}"
unset AUTH_HEADER
echo "Authentification Git configurée pour PUSH_TOKEN."
# ============================================================
# 19. Commit
# ============================================================
- name: 19. Commit
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.sql == 'true' || steps.changed.outputs.manual == 'true'
uses: stefanzweifel/git-auto-commit-action@v7
env:
COMMIT_DATE: ${{ steps.changed.outputs.commit_date }}
with:
commit_message: "Auto-généré par ScriptPython - ${{ env.COMMIT_DATE }} GMT"
commit_user_name: "github-actions[bot]"
commit_user_email: "41898282+github-actions[bot]@users.noreply.github.com"
commit_author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
# ============================================================
# 20. MerginMaps Hook
# ============================================================
- name: 20. MerginMaps Hook
if: steps.changed.outputs.shp == 'true' || steps.changed.outputs.manual == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
gh api "repos/${GH_REPO}/dispatches" -f event_type=mergin-sync