stages: - validate - deploy validate_json: stage: validate image: python:3.12-slim rules: # Merge request pipelines - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: on_success # Pushes to master - if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push" when: on_success # Everything else: do not run - when: never script: - python -V - | python - <<'PY' import json import sys from pathlib import Path root = Path("web") data_dir = root / "data" templates_dir = root / "templates" if not data_dir.is_dir(): print(f"ERROR: data directory not found: {data_dir.resolve()}") sys.exit(1) if not templates_dir.is_dir(): print(f"ERROR: templates directory not found: {templates_dir.resolve()}") sys.exit(1) # NEW: ensure all files under /data are .json (no other extensions, no extensionless files) bad_files = [] for p in sorted(data_dir.rglob("*")): if p.is_file() and p.suffix.lower() != ".json": bad_files.append(p.as_posix()) if bad_files: print("\nDATA DIRECTORY VALIDATION FAILED\n") print("ERROR: Non-JSON files found under web/data (only .json allowed):") for bf in bad_files: print(" - " + bf) sys.exit(1) json_files = sorted(data_dir.rglob("*.json")) if not json_files: print(f"ERROR: No .json files found under {data_dir}") sys.exit(1) errors = [] checked = 0 for jf in json_files: checked += 1 rel = jf.as_posix() # 1) JSON syntax check try: obj = json.loads(jf.read_text(encoding="utf-8")) except Exception as e: errors.append(f"{rel}: invalid JSON ({e})") continue if not isinstance(obj, dict): errors.append(f"{rel}: JSON root must be an object") continue # 2) type exists and is valid t = obj.get("type") if not isinstance(t, str) or not t.strip(): errors.append(f"{rel}: missing or invalid 'type'") continue t = t.strip() n = obj.get("name") if not isinstance(n, str) or not n.strip(): errors.append(f"{rel}: missing or invalid 'name'") continue # 3) template exists tpl = templates_dir / f"{t}.template" if not tpl.is_file(): errors.append( f"{rel}: type '{t}' has no matching template ({tpl.as_posix()})" ) if errors: print("\nJSON VALIDATION FAILED\n") for e in errors: print(" - " + e) print(f"\nChecked {checked} file(s), found {len(errors)} issue(s).") sys.exit(1) print(f"OK: {checked} JSON file(s) validated successfully.") PY ftp_upload: stage: deploy variables: timftpuser: $timftpuser timftphost: $timftphost timftppassword: $timftppassword projectftp: $projectftp script: - apt-get update -qy - apt-get install -y lftp # In-place sync instead of "rm -r + mirror": live folders never disappear during a deploy. # - no --only-newer/--ignore-time: the fresh CI checkout gives every file a new mtime, so the # default size+time comparison re-uploads all files (correct even if FTP mtimes are unreliable) # - --delete removes remote files that no longer exist in web/ (runs after the transfers) # - xfer:use-temp-file uploads to a temp name and renames, so a file is never served half-written # - cmd:fail-exit makes the job fail if the mirror fails - lftp -u $timftpuser,$timftppassword $timftphost -e "set ssl:verify-certificate no; set sftp:auto-confirm yes; set xfer:use-temp-file yes; mkdir -p -f $projectftp; set cmd:fail-exit yes; mirror -R --delete --parallel=4 web $projectftp; quit" artifacts: paths: - web needs: - job: validate_json artifacts: false only: - master # Run this job only when changes are pushed to the master branch except: - merge_requests # Do not run this job on merge requests