default:
  image: "registry.gitlab.com/xenia-group/foxbuild:main"

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == 'unstable'
    - if: $CI_COMMIT_BRANCH == 'main'
    - if: $CI_PIPELINE_SOURCE == 'schedule'

stages:
  - test
  - build
  - deploy

test-spec-ext:
  stage: test
  script:
    - if [ "$BUILD_ALL" == 1 ]; then wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_all.toml?ref_type=heads -O build.toml; else wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_changed.toml?ref_type=heads -O build.toml; fi
    - if [ "$CI_COMMIT_BRANCH" == "" ]; then BRANCH="$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"; else BRANCH="$CI_COMMIT_BRANCH"; fi
    - sed -i "s/unstable/$BRANCH/g" build.toml
    - sed -i "s@https://gitlab.com/xenia-group/catalyst.git@$CI_PROJECT_URL@g" build.toml
    - cat build.toml
    - foxbuild -nc build.toml
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID != '42950031' 

test-spec:
  tags:
    - catalyst
  stage: test
  before_script:
    - nohup tailscaled &
    - tailscale up --authkey $TS_KEY
    - mkdir -p /var/tmp/catalyst/packages
    - mkdir -p /var/foxbuild/output
    - mkdir -p /var/tmp/catalyst/builds/default
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/packages /var/tmp/catalyst/packages
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/output /var/foxbuild/output
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/stage3 /var/tmp/catalyst/builds/default
  script:
    - if [ "$BUILD_ALL" == 1 ]; then wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_all.toml?ref_type=heads -O build.toml; else wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_changed.toml?ref_type=heads -O build.toml; fi
    - if [ "$CI_COMMIT_BRANCH" == "" ]; then BRANCH="$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"; else BRANCH="$CI_COMMIT_BRANCH"; fi
    - sed -i "s/unstable/$BRANCH/g" build.toml
    - sed -i "s@https://gitlab.com/xenia-group/catalyst.git@$CI_PROJECT_URL@g" build.toml
    - cat build.toml
    - foxbuild -nc build.toml
  allow_failure: false
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == 'unstable'
    - if: $CI_COMMIT_BRANCH == 'main'
    - if: $CI_PIPELINE_SOURCE == 'schedule'
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID == '42950031' # internal merge request, use our caches

build-xenia:
  tags: 
    - catalyst
  stage: build
  before_script:
    - nohup tailscaled &
    - tailscale up --authkey $TS_KEY
    - mkdir -p /var/tmp/catalyst/packages
    - mkdir -p /var/foxbuild/output
    - mkdir -p /var/tmp/catalyst/builds/default
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/packages /var/tmp/catalyst/packages
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/output /var/foxbuild/output
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/stage3 /var/tmp/catalyst/builds/default
  script:
    - if [ "$BUILD_ALL" == 1 ]; then wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_all.toml?ref_type=heads -O build.toml; else wget https://gitlab.com/xenia-group/foxbuild/-/raw/main/build_changed.toml?ref_type=heads -O build.toml; fi
    - if [ "$CI_COMMIT_BRANCH" == "" ]; then BRANCH="$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"; else BRANCH="$CI_COMMIT_BRANCH"; fi
    - sed -i "s/unstable/$BRANCH/g" build.toml
    - sed -i "s@https://gitlab.com/xenia-group/catalyst.git@$CI_PROJECT_URL@g" build.toml
    - cat build.toml
    - foxbuild -ic build.toml
  allow_failure: false
  resource_group: $CI_COMMIT_BRANCH
  rules:
    - if: $CI_COMMIT_TAG && $CI_PIPELINE_SOURCE != 'schedule'
    - if: $CI_COMMIT_BRANCH == 'unstable' && $CI_PIPELINE_SOURCE != 'schedule'
    - if: $CI_COMMIT_BRANCH == 'main' && $CI_PIPELINE_SOURCE != 'schedule'
    - if: $CI_PIPELINE_SOURCE == 'schedule' && $SCHEDULE_FULL == "1"
  retry: 2

deploy-xenia:
  tags:
    - catalyst
  stage: deploy
  before_script:
    - nohup tailscaled &
    - tailscale up --authkey $TS_KEY
    - mkdir -p /var/foxbuild/output
    - sshfs -o "StrictHostKeyChecking=accept-new" root@100.80.175.68:/root/xenia/output /var/foxbuild/output
    - eval $(ssh-agent -s)
    - chmod 400 "$REPO_SSH_KEY"
    - ssh-add "$REPO_SSH_KEY"
  script:
    - rsync -az -P -e "ssh -p 2 -o StrictHostKeyChecking=no" --exclude='Manifest.toml' --include='/*' --include="$CI_COMMIT_BRANCH**" --exclude='*' /var/foxbuild/output/* xenia@sakamoto.pl:/mnt/xenia/releases/
    - rsync -az -P -e "ssh -p 2 -o StrictHostKeyChecking=no" /var/foxbuild/output/Manifest.toml xenia@sakamoto.pl:/mnt/xenia/releases/
  allow_failure: false
  resource_group: $CI_COMMIT_BRANCH
  rules:
    - if: $CI_PIPELINE_SOURCE == 'schedule' && $SCHEDULE_FULL == "1"
      when: on_success
    - if: $CI_COMMIT_TAG && $CI_PIPELINE_SOURCE != 'schedule'
      when: manual
    - if: $CI_COMMIT_BRANCH == 'unstable' && $CI_PIPELINE_SOURCE != 'schedule'
      when: manual
    - if: $CI_COMMIT_BRANCH == 'main' && $CI_PIPELINE_SOURCE != 'schedule'
      when: manual
  retry: 2
