variables:
  RUST_IMAGE: "rust:latest"
  NODE_IMAGE: "node:lts"
  CARGO_HOME: $CI_PROJECT_DIR/cargo

stages:
  - lint
  - build
  - test
  - release

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - target
    - cargo
    - webui/node_modules

lint:backend:
  stage: lint
  image: $RUST_IMAGE
  script:
    - rustup component add clippy
    - make backend_lint
  rules:
    - changes:
        - migrations/**/*
        - src/**/*
        - Cargo.lock
        - Makefile
      allow_failure: true

lint:backend_no_default_features:
  stage: lint
  image: $RUST_IMAGE
  script:
    - rustup component add clippy
    - make backend_lint_no_default
  rules:
    - changes:
        - migrations/**/*
        - src/**/*
        - Cargo.lock
        - Makefile
      allow_failure: true

lint:frontend:
 stage: lint
 image: $NODE_IMAGE
 script:
   - node --version && npm --version
   - make frontend_lint
 rules:
   - allow_failure: true

lint:frontend_lib:
 stage: lint
 image: $NODE_IMAGE
 script:
   - node --version && npm --version
   - make frontend_lib_lint
 rules:
   - allow_failure: true

build:backend:
  stage: build
  image: $RUST_IMAGE
  script:
    - rustc --version && cargo --version
    - make backend
  artifacts:
    paths:
      - target/release/upend
    expire_in: 1 day
  only:
    changes:
      - migrations/**/*
      - src/**/*
      - Cargo.lock
      - Makefile

build:frontend:
  stage: build
  image: $NODE_IMAGE
  script:
    - node --version && npm --version
    - make frontend
  artifacts:
    paths:
      - webui/dist
      - tools/upend_js
    expire_in: 1 day
  only:
    changes:
      - webui/**/*
      - Makefile

test:backend:
  stage: test
  image: $RUST_IMAGE
  script:
    - make backend_test
  only:
    changes:
      - migrations/**/*
      - src/**/*
      - Cargo.lock
      - Makefile

test:backend_no_default_features:
  stage: test
  image: $RUST_IMAGE
  script:
    - make backend_test_no_default
  only:
    changes:
      - migrations/**/*
      - src/**/*
      - Cargo.lock
      - Makefile
  allow_failure: true # remove at v1.0

package:
  stage: release
  image: $RUST_IMAGE
  before_script:
    - cd /tmp
    - wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
    - chmod +x linuxdeploy-x86_64.AppImage
    - ./linuxdeploy-x86_64.AppImage --appimage-extract
    - ln -s $PWD/squashfs-root/AppRun /usr/local/bin/linuxdeploy-x86_64.AppImage
    - cd -
  script:
    - make package
  artifacts:
    paths:
      - ./*.AppImage
  only:
    - tags
