| 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 | json_files = sorted(data_dir.rglob("*.json")) |
| 41 | if not json_files: |
| 42 | print(f"ERROR: No .json files found under {data_dir}") |
| 43 | sys.exit(1) |
| 44 | |
| 45 | errors = [] |
| 46 | checked = 0 |
| 47 | |
| 48 | for jf in json_files: |
| 49 | checked += 1 |
| 50 | rel = jf.as_posix() |
| 51 | |
| 52 | # 1) JSON syntax check |
| 53 | try: |
| 54 | obj = json.loads(jf.read_text(encoding="utf-8")) |
| 55 | except Exception as e: |
| 56 | errors.append(f"{rel}: invalid JSON ({e})") |
| 57 | continue |
| 58 | |
| 59 | if not isinstance(obj, dict): |
| 60 | errors.append(f"{rel}: JSON root must be an object") |
| 61 | continue |
| 62 | |
| 63 | # 2) type exists and is valid |
| 64 | t = obj.get("type") |
| 65 | if not isinstance(t, str) or not t.strip(): |
| 66 | errors.append(f"{rel}: missing or invalid 'type'") |
| 67 | continue |
| 68 | |
| 69 | t = t.strip() |
| 70 | |
| 71 | # 3) template exists |
| 72 | tpl = templates_dir / f"{t}.template" |
| 73 | if not tpl.is_file(): |
| 74 | errors.append( |
| 75 | f"{rel}: type '{t}' has no matching template ({tpl.as_posix()})" |
| 76 | ) |
| 77 | |
| 78 | if errors: |
| 79 | print("\nJSON VALIDATION FAILED\n") |
| 80 | for e in errors: |
| 81 | print(" - " + e) |
| 82 | print(f"\nChecked {checked} file(s), found {len(errors)} issue(s).") |
| 83 | sys.exit(1) |
| 84 | |
| 85 | print(f"OK: {checked} JSON file(s) validated successfully.") |
| 86 | PY |
| 87 | |
| 88 | ftp_upload: |
| 89 | stage: deploy |
| 90 | variables: |
| 91 | timftpuser: $timftpuser |
| 92 | timftphost: $timftphost |
| 93 | timftppassword: $timftppassword |
| 94 | projectftp: $projectftp |
| 95 | script: |
| 96 | - apt-get update -qy |
| 97 | - apt-get install -y lftp |
| 98 | - lftp -u $timftpuser,$timftppassword $timftphost -e "set ssl:verify-certificate no; set sftp:auto-confirm yes; rm -r $projectftp; mirror -R web $projectftp" |
| 99 | artifacts: |
| 100 | paths: |
| 101 | - web |
| 102 | needs: |
| 103 | - job: validate_json |
| 104 | artifacts: false |
| 105 | only: |
| 106 | - master # Run this job only when changes are pushed to the master branch |
| 107 | except: |
| 108 | - merge_requests # Do not run this job on merge requests |