cache: &global_cache
    key: one-key-to-rule-them-all
    paths: 
        - .cargo_home/registry/cache
        - .cargo_home/registry/index
        - .cargo_home/git/db
        - target/debug/deps
        - target/debug/incremental
        - target/debug/build

variables:
    CARGO_HOME: .cargo_home

stages:
  - test
  - build
  - lint

.deploy-production:
    needs: ["test-rust-stable"]
    cache: {}
    script:
        - echo "SSH to the target machine and deploy a NixOS container from this exact flake"
        - echo "Not yet implemented"
        - exit 1
    environment:
        name: production
        url: https://fireburn.ru/
    when: ["manual"]


build-x86_64-musl:
    needs: ["test-rust-stable"]
    stage: build
    image: registry.gitlab.com/rust_musl_docker/image:stable-latest
    cache: {}
    variables:
        RUSTFLAGS: -C target-feature=+crt-static
    script:
        - cargo build --release --target x86_64-unknown-linux-musl
    artifacts:
        expire_in: 30 days
        paths:
            - target/x86_64-unknown-linux-musl/release/kittybox
            - target/x86_64-unknown-linux-musl/release/kittybox-bulk-import
            - target/x86_64-unknown-linux-musl/release/pyindieblog-export

build-aarch64-musl:
    needs: ["test-rust-stable"]
    stage: build
    image: messense/rust-musl-cross:aarch64-musl
    cache: {}
    variables:
        RUSTFLAGS: -C target-feature=+crt-static
        CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER: aarch64-unknown-linux-musl-gcc
    script:
        - cargo build --release --target aarch64-unknown-linux-musl
    artifacts:
        expire_in: 30 days
        paths:
            - target/aarch64-unknown-linux-musl/release/kittybox
            - target/aarch64-unknown-linux-musl/release/kittybox-bulk-import
            - target/aarch64-unknown-linux-musl/release/pyindieblog-export

.build-docker:
    stage: build
    cache: {}
    image: 

.test: &test
    stage: test
    before_script:
        - apt update
        - apt install -y redis
    script:
        - cargo test --verbose

test-rust-nightly:
    needs: []
    image: rustlang/rust:nightly
    cache:
        <<: *global_cache
        policy: pull
    allow_failure: true
    <<: *test

test-rust-stable:
    needs: []
    image: rust:latest
    <<: *test

rustfmt:
    stage: lint
    needs: []
    image: rust:latest
    cache:
        <<: *global_cache
        policy: pull
    before_script:
        - rustup component add rustfmt
    script:
        - cargo fmt -- --check
    allow_failure: true
    interruptible: true

clippy:
    stage: lint
    needs: []
    image: rust:latest
    cache:
        <<: *global_cache
        policy: pull
    before_script:
        - rustup component add clippy
    script:
        - cargo clippy
    allow_failure: true
    interruptible: true