image: "rust:1.89.0"

stages:
  - build
  - publish
  - release

variables:
  CONTINUOUS_TAG: "continuous-master"
  CARGO_HOME: ${CI_PROJECT_DIR}/.cargo
  RUSTFLAGS: -Dwarning
  ARTIFACT_X64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}"
  ARTIFACT_ARM64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}-arm64"
  CONTINUOUS_ARTIFACT_X64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/continuous-master/${CI_PROJECT_NAME}"
  CONTINUOUS_ARTIFACT_ARM64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/continuous-master/${CI_PROJECT_NAME}-arm64"
  APPIMAGE_FILENAME: "caracal-x86_64.AppImage"
  APPIMAGE_CONTINUOUS_ARTIFACT_X64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/continuous-master/${CI_PROJECT_NAME}-x86_64.AppImage"
  APPIMAGE_ARTIFACT_X64_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}-x86_64.AppImage"

default:
  interruptible: true
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - .cargo
      - target

build-x64:
  stage: build
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - apt-get -y update
    - apt-get install -y curl libfuse2 fuse fuseiso
    - cargo build --release
    - .gitlab/appimage-build.sh
  artifacts:
    paths:
      - target/release/${CI_PROJECT_NAME}
      - caracal*.AppImage

build-arm64:
  stage: build
  tags:
    - saas-linux-small-arm64
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - apt-get -y update
    - apt-get install -y curl libfuse2 fuse fuseiso
    - cargo build --release
    - mv target/release/${CI_PROJECT_NAME} target/release/${CI_PROJECT_NAME}-arm64
  artifacts:
    paths:
      - target/release/${CI_PROJECT_NAME}-arm64

publish:
  image: rust:latest
  stage: publish
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - cargo login ${CRATES_IO_TOKEN}
    - cargo publish

release-continuous:
  image: registry.gitlab.com/gitlab-org/release-cli
  stage: release
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - apk add curl

    - >
      curl
      --request DELETE
      --header "JOB-TOKEN: $CI_JOB_TOKEN"
      --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}"
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${CONTINUOUS_TAG}"

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     target/release/${CI_PROJECT_NAME}
     ${CONTINUOUS_ARTIFACT_X64_URL}

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     target/release/${CI_PROJECT_NAME}-arm64
     ${CONTINUOUS_ARTIFACT_ARM64_URL}

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     caracal*.AppImage
     ${APPIMAGE_CONTINUOUS_ARTIFACT_X64_URL}

    - >
      release-cli create
      --name release-${CI_COMMIT_SHA}
      --description README.md
      --tag-name "${CONTINUOUS_TAG}"
      --ref $CI_COMMIT_SHA
      --assets-link
      "{\"name\": \"caracal\", \"filepath\": \"/caracal\", \"url\": \"${CONTINUOUS_ARTIFACT_X64_URL}\"}"
      --assets-link
      "{\"name\": \"caracal-arm64\", \"filepath\": \"/caracal-arm64\", \"url\": \"${CONTINUOUS_ARTIFACT_ARM64_URL}\"}"
      --assets-link
      "{\"name\": \"${APPIMAGE_FILENAME}\", \"filepath\": \"/${APPIMAGE_FILENAME}\", \"url\": \"${APPIMAGE_CONTINUOUS_ARTIFACT_X64_URL}\"}"

release-tag:
  image: registry.gitlab.com/gitlab-org/release-cli
  stage: release
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - apk add curl

    - >
      curl
      --request DELETE
      --header "JOB-TOKEN: $CI_JOB_TOKEN"
      --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}"
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${CI_COMMIT_TAG}"

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     target/release/${CI_PROJECT_NAME}
     ${ARTIFACT_X64_URL}

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     target/release/${CI_PROJECT_NAME}-arm64
     ${ARTIFACT_ARM64_URL}

    - >
     curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
     --upload-file
     caracal*.AppImage
     ${APPIMAGE_ARTIFACT_X64_URL}

    - >
      release-cli create
      --name release-${CI_COMMIT_SHA}
      --description README.md
      --tag-name ${CI_COMMIT_TAG}
      --ref $CI_COMMIT_SHA
      --assets-link
      "{\"name\": \"caracal\", \"filepath\": \"/caracal\", \"url\": \"${ARTIFACT_X64_URL}\"}"
      --assets-link
      "{\"name\": \"caracal-arm64\", \"filepath\": \"/caracal-arm64\", \"url\": \"${ARTIFACT_ARM64_URL}\"}"
      --assets-link
      "{\"name\": \"${APPIMAGE_FILENAME}\", \"filepath\": \"/${APPIMAGE_FILENAME}\", \"url\": \"${APPIMAGE_ARTIFACT_X64_URL}\"}"
