stages:
 - prebuild
 - build
 - postbuild
prebuild:
 stage: prebuild
# rules:
#    - if: '$CI_COMMIT_TAG'
 tags:
  - Builder-1
 script:
  - eval $(ssh-agent -s)
  - echo "$BASE64_TILEOS_ISO_STORAGE_KEY" | base64 -d > ./id_rsa
  - docker build --no-cache -f Dockerfile-builder -t builder:latest .

tileos-sway-amd64:
 when: manual
 stage: build
 variables:
   DESKTOP: sway
 tags:
  - Builder-1
 script:
  - eval $(ssh-agent -s)
  - mkdir /root/.ssh
  - echo "$BASE64_TILEOS_ISO_STORAGE_KEY" | base64 -d > ~/.ssh/id_rsa
  - chmod 400 ~/.ssh/id_rsa
  - rm -rf ${CI_PROJECT_DIR}/output/*
  - docker run --privileged --rm -i -v ${CI_PROJECT_DIR}/output:/builder/builds -v /proc:/proc builder:latest ./build --desktop sway --release testing
  - ssh -o StrictHostKeyChecking=no ${STORAGE_USER}@${STORAGE_HOST} -p ${STORAGE_PORT} -i ~/.ssh/id_rsa rm -rf /media/iso/tile-os/testing/sway/*
  - scp -o StrictHostKeyChecking=no -o Port=${STORAGE_PORT} -o IdentityFile=/builder/id_rsa -p -B ${CI_PROJECT_DIR}/output/amd64/* ${STORAGE_USER}@${STORAGE_HOST}:/media/iso/tile-os/testing/sway/

tileos-river-amd64:
 #rules:
 #   - if: '$CI_COMMIT_TAG'
 when: manual
 stage: build
 variables:
   DESKTOP: sway
 tags:
  - Builder-1
 script:
  - eval $(ssh-agent -s)
  - mkdir /root/.ssh
  - echo "$BASE64_TILEOS_ISO_STORAGE_KEY" | base64 -d > ~/.ssh/id_rsa
  - chmod 400 ~/.ssh/id_rsa
  - rm -rf ${CI_PROJECT_DIR}/output/*
  - docker run --privileged --rm -i -v ${CI_PROJECT_DIR}/output:/builder/builds -v /proc:/proc builder:latest ./build --desktop river --release testing
  - ssh -o StrictHostKeyChecking=no ${STORAGE_USER}@${STORAGE_HOST} -p ${STORAGE_PORT} -i ~/.ssh/id_rsa rm -rf /media/iso/tile-os/testing/river/*
  - scp -o StrictHostKeyChecking=no -o Port=${STORAGE_PORT} -o IdentityFile=/builder/id_rsa -p -B ${CI_PROJECT_DIR}/output/amd64/* ${STORAGE_USER}@${STORAGE_HOST}:/media/iso/tile-os/testing/river/

mrproper:
 #rules:
 #   - if: '$CI_COMMIT_TAG'
 stage: postbuild
 when: on_success
 tags:
  - Builder-1
 needs:
  - tileos-sway-amd64
  - tileos-river-amd64
 script:
  - docker system prune -a -f
