---
kind: pipeline
type: docker
name: default

steps:
# Needs a full git clone
- name: fetch
  image: alpine/git
  commands:
  - git fetch --tags

# Frontend
- name: web_build
  image: node:23
  depends_on:
  - fetch
  volumes:
    - name: web_app
      path: /tmp/web_build
  commands:
  - cd moneymgr_web
  - npm install
  - npm run lint
  - npm run build
  - mv dist /tmp/web_build

# Backend
- name: backend_fetch_deps
  image: rust
  depends_on:
  - fetch
  volumes:
    - name: rust_registry
      path: /usr/local/cargo/registry
  commands:
    - cd moneymgr_backend
    - cargo fetch

- name: backend_code_quality
  image: rust
  volumes:
    - name: rust_registry
      path: /usr/local/cargo/registry
  depends_on:
    - backend_fetch_deps
  commands:
    - cd moneymgr_backend
    - rustup component add clippy
    - cargo clippy -- -D warnings
    - cargo clippy --example api_curl -- -D warnings

- name: backend_test
  image: rust
  volumes:
    - name: rust_registry
      path: /usr/local/cargo/registry
  depends_on:
    - backend_code_quality
  commands:
    - cd moneymgr_backend
    - cargo test


- name: backend_build
  image: rust
  when:
    event:
      - tag
  volumes:
  - name: rust_registry
    path: /usr/local/cargo/registry
  - name: web_app
    path: /tmp/web_build
  - name: release
    path: /tmp/release
  depends_on:
  - backend_test
  - web_build
  commands:
  - cd moneymgr_backend
  - mv /tmp/web_build/dist static
  - cargo build --release
  - cargo build --release --example api_curl
  - ls -lah target/release/moneymgr_backend target/release/examples/api_curl
  - cp target/release/moneymgr_backend target/release/examples/api_curl /tmp/release

# Mobile app code quality
- name: mobile_app_code_quality
  image: ghcr.io/cirruslabs/flutter:latest
  depends_on:
  - fetch
  commands:
  - echo "Build version:" $(git describe --tags --abbrev=0)
  - echo "Build number:" $(git rev-list --count $(git describe --tags --abbrev=0))
  - cd moneymgr_mobile
  - flutter --disable-analytics
  - flutter pub get --enforce-lockfile
  - dart run build_runner build
  - flutter analyze

# Mobile app build
- name: mobile_app_build
  image: ghcr.io/cirruslabs/flutter:latest
  depends_on:
  - backend_build # prevent synchronous backend & frontend build
  - mobile_app_code_quality
  when:
    event:
      - tag
  environment:
    JKS_KEYSTORE:
      from_secret: JKS_KEYSTORE
    JKS_KEYSTORE_PASSWORD:
      from_secret: JKS_KEYSTORE_PASSWORD
  volumes:
  - name: release
    path: /tmp/release
  commands:
  - cd moneymgr_mobile
  - flutter --disable-analytics
  - bash android/ci_write_keystore.sh
  - flutter pub get --enforce-lockfile
  - dart run build_runner build
  - flutter build apk
    --release
    --flavor publish
    --build-name $(git describe --tags --abbrev=0)
    --split-per-abi
    --target-platform android-arm64
    --build-number $(git rev-list --count $(git describe --tags --abbrev=0))
  - cp build/app/outputs/flutter-apk/app-arm64-v8a-publish-release.apk /tmp/release/moneymgr_mobile_arm64-v8a.apk

# Release
- name: gitea_release
  image: plugins/gitea-release
  depends_on:
  - backend_build
  - mobile_app_build
  when:
    event:
      - tag
  volumes:
  - name: release
    path: /tmp/release
  environment:
    PLUGIN_API_KEY:
      from_secret: API_KEY
  settings:
    base_url: https://gitea.communiquons.org
    files: /tmp/release/*
    checksum: sha512

volumes:
- name: rust_registry
  temp: {}
- name: web_app
  temp: {}
- name: release
  temp: {}