| 1 | stages: |
| 2 | - validate |
| 3 | - deploy |
| 4 | |
| 5 | validate_json: |
| 6 | stage: validate |
| 7 | image: python:3.12-slim |
| 8 | rules: |
| 9 | # Merge request pipelines |
| 10 | - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 11 | when: on_success |
| 12 | |
| 13 | # Pushes to master |
| 14 | - if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push" |
| 15 | when: on_success |
| 16 | |
| 17 | # Everything else: do not run |
| 18 | - when: never |
| 19 | |
| 20 | script: |
| 21 | - python -V |
| 22 | - | |
| 23 | python - <<'PY' |
| 24 | import json |
| 25 | import sys |
| 26 | from pathlib import Path |
| 27 | |
| 28 | root = Path("web") |
| 29 | data_dir = root / "data" |
| 30 | templates_dir = root / "templates" |
| 31 | |
| 32 | if not data_dir.is_dir(): |
| 33 | print(f"ERROR: data directory not found: {data_dir.resolve()}") |
| 34 | sys.exit(1) |
| 35 | |
| 36 | if not templates_dir.is_dir(): |
| 37 | print(f"ERROR: templates directory not found: {templates_dir.resolve()}") |
| 38 | sys.exit(1) |
| 39 | |
| 40 | # NEW: ensure all files under /data are .json (no other extensions, no extensionless files) |
| 41 | bad_files = [] |
| 42 | for p in sorted(data_dir.rglob("*")): |
| 43 | if p.is_file() and p.suffix.lower() != ".json": |
| 44 | bad_files.append(p.as_posix()) |
| 45 | |
| 46 | if bad_files: |
| 47 | print("\nDATA DIRECTORY VALIDATION FAILED\n") |
| 48 | print("ERROR: Non-JSON files found under web/data (only .json allowed):") |
| 49 | for bf in bad_files: |
| 50 | print(" - " + bf) |
| 51 | sys.exit(1) |
| 52 | |
| 53 | json_files = sorted(data_dir.rglob("*.json")) |
| 54 | if not json_files: |
| 55 | print(f"ERROR: No .json files found under {data_dir}") |
| 56 | sys.exit(1) |
| 57 | |
| 58 | errors = [] |
| 59 | checked = 0 |
| 60 | |
| 61 | for jf in json_files: |
| 62 | checked += 1 |
| 63 | rel = jf.as_posix() |
| 64 | |
| 65 | # 1) JSON syntax check |
| 66 | try: |
| 67 | obj = json.loads(jf.read_text(encoding="utf-8")) |
| 68 | except Exception as e: |
| 69 | errors.append(f"{rel}: invalid JSON ({e})") |
| 70 | continue |
| 71 | |
| 72 | if not isinstance(obj, dict): |
| 73 | errors.append(f"{rel}: JSON root must be an object") |
| 74 | continue |
| 75 | |
| 76 | # 2) type exists and is valid |
| 77 | t = obj.get("type") |
| 78 | if not isinstance(t, str) or not t.strip(): |
| 79 | errors.append(f"{rel}: missing or invalid 'type'") |
| 80 | continue |
| 81 | |
| 82 | t = t.strip() |
| 83 | |
| 84 | n = obj.get("name") |
| 85 | if not isinstance(n, str) or not n.strip(): |
| 86 | errors.append(f"{rel}: missing or invalid 'name'") |
| 87 | continue |
| 88 | |
| 89 | # 3) template exists |
| 90 | tpl = templates_dir / f"{t}.template" |
| 91 | if not tpl.is_file(): |
| 92 | errors.append( |
| 93 | f"{rel}: type '{t}' has no matching template ({tpl.as_posix()})" |
| 94 | ) |
| 95 | |
| 96 | if errors: |
| 97 | print("\nJSON VALIDATION FAILED\n") |
| 98 | for e in errors: |
| 99 | print(" - " + e) |
| 100 | print(f"\nChecked {checked} file(s), found {len(errors)} issue(s).") |
| 101 | sys.exit(1) |
| 102 | |
| 103 | print(f"OK: {checked} JSON file(s) validated successfully.") |
| 104 | PY |
| 105 | |
| 106 | ftp_upload: |
| 107 | stage: deploy |
| 108 | variables: |
| 109 | timftpuser: $timftpuser |
| 110 | timftphost: $timftphost |
| 111 | timftppassword: $timftppassword |
| 112 | projectftp: $projectftp |
| 113 | script: |
| 114 | - apt-get update -qy |
| 115 | - apt-get install -y lftp |
| 116 | # In-place sync instead of "rm -r + mirror": live folders never disappear during a deploy. |
| 117 | # - no --only-newer/--ignore-time: the fresh CI checkout gives every file a new mtime, so the |
| 118 | # default size+time comparison re-uploads all files (correct even if FTP mtimes are unreliable) |
| 119 | # - --delete removes remote files that no longer exist in web/ (runs after the transfers) |
| 120 | # - xfer:use-temp-file uploads to a temp name and renames, so a file is never served half-written |
| 121 | # - cmd:fail-exit makes the job fail if the mirror fails |
| 122 | - 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" |
| 123 | artifacts: |
| 124 | paths: |
| 125 | - web |
| 126 | needs: |
| 127 | - job: validate_json |
| 128 | artifacts: false |
| 129 | only: |
| 130 | - master # Run this job only when changes are pushed to the master branch |
| 131 | except: |
| 132 | - merge_requests # Do not run this job on merge requests |