stages:
  - build
  - test
  - deploy
  - build_container

build_dev_images:
  stage: build
  dependencies: []
  only:
    - master
  image:
    name: tollerort.desy.de/gcr.io/kaniko-project/executor:debug
    entrypoint: [ "" ]

  variables:
    IMAGE_TAG_LATEST: "latest"
    IMAGE_TAG_SHA: $CI_COMMIT_SHORT_SHA
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""

  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - >
      /kaniko/executor
      --context $CI_PROJECT_DIR
      --dockerfile $CI_PROJECT_DIR/deployment/docker/Dockerfile_$IMAGE
      --destination $CI_REGISTRY_IMAGE/$IMAGE:$IMAGE_TAG_LATEST
      --destination $CI_REGISTRY_IMAGE/$IMAGE:$IMAGE_TAG_SHA
      --build-arg HELM_VERSION=$HELM_VERSION
      --build-arg RANCHER_CLI_VERSION=$RANCHER_CLI_VERSION

  tags:
    - kubernetes

  parallel:
    matrix:
     - IMAGE: ["centos7","alma9","fedora","ubuntu"]

  when: manual

build-and-test:
  image: gitlab.desy.de:5555/thomas.white/crystfel/$PLATFORM
  variables:
    GIT_STRATEGY: clone

  script:
    - meson setup build --prefix $CFPREFIX
    - ninja -v -C build
    - ninja -v -C build test
    - ninja -C build install
     
    # Tweak CrystFEL GUI so that it can find syminfo.lib
    - mv $CFPREFIX/bin/crystfel $CFPREFIX/bin/crystfel.real
    - echo '#!/bin/sh' > $CFPREFIX/bin/crystfel
    - echo "export SYMINFO=$CFPREFIX/share/ccp4/syminfo.lib" >> $CFPREFIX/bin/crystfel
    - echo "$CFPREFIX/bin/crystfel.real \"\$@\"" >> $CFPREFIX/bin/crystfel
    - chmod +x $CFPREFIX/bin/crystfel
    
    # Tweak get_hkl in the same way
    - mv $CFPREFIX/bin/get_hkl $CFPREFIX/bin/get_hkl.real
    - echo '#!/bin/sh' > $CFPREFIX/bin/get_hkl
    - echo "export SYMINFO=$CFPREFIX/share/ccp4/syminfo.lib" >> $CFPREFIX/bin/get_hkl
    - echo "$CFPREFIX/bin/get_hkl.real \"\$@\"" >> $CFPREFIX/bin/get_hkl
    - chmod +x $CFPREFIX/bin/get_hkl
    
    # Mosflm (tweaked to find syminfo.lib and not load environ.def/default.def)
    - wget -nv https://www.mrc-lmb.cam.ac.uk/mosflm/mosflm/ver740/pre-built/mosflm-linux-64-noX11.zip
    - unzip mosflm-linux-64-noX11.zip
    - mv mosflm-linux-64-noX11 $CFPREFIX/bin/mosflm.real
    - echo '#!/bin/sh' > $CFPREFIX/bin/mosflm
    - echo "export SYMINFO=$CFPREFIX/share/ccp4/syminfo.lib" >> $CFPREFIX/bin/mosflm
    - echo "$CFPREFIX/bin/mosflm.real -n \"\$@\"" >> $CFPREFIX/bin/mosflm
    - chmod +x $CFPREFIX/bin/mosflm

    - cp -Rp /software $CI_PROJECT_DIR/$PLATFORM

  artifacts:
    paths:
      - $PLATFORM
      - build/meson-logs/testlog.junit.xml
    reports:
      junit: build/meson-logs/testlog.junit.xml
    when: on_success
    expire_in: 1 day

  parallel:
    matrix:
     - PLATFORM:  ["centos7","alma9","fedora","ubuntu"]

build-and-test-minimal:
  image: tollerort.desy.de/hub.docker.com/$IMAGE:latest
  script:
    - dnf update -y && dnf group install -y development-tools &&
      dnf install -y gsl-devel flex bison cmake meson
    - meson setup build -Dhdf5=disabled --wrap-mode=nofallback && ninja -C build && ninja -C build test

  artifacts:
    when: always
    paths:
      - build/meson-logs/testlog.junit.xml
    reports:
      junit: build/meson-logs/testlog.junit.xml

  parallel:
    matrix:
     - IMAGE: ["fedora"]

build-brew-macos:
  dependencies: []
  tags:
    - macOS
  variables:
    GIT_STRATEGY: clone
    HOMEBREW_DEVELOPER: 1
  script:
    - brew uninstall -v -f crystfel
    - brew install -v -s ./crystfel.rb --HEAD
    - brew test crystfel
    - brew uninstall -v -f crystfel

deploy-remote-maxwell:
  stage: deploy
  image: $CI_REGISTRY_IMAGE/alma9
  dependencies:
    - "build-and-test: [alma9]"
  variables:
    GIT_STRATEGY: clone
  tags:
    - DESY-intern
  before_script:
  ##
  ## Run ssh-agent (inside the build environment)
  ## Give the right permissions, otherwise ssh-add will refuse to add files
  ## Add the SSH key stored in MAXWELL_SSH_PRIVATE_KEY file type CI/CD variable to the agent store
  ## Create the SSH directory and give it the right permissions
  ## If MAXWELL_SSH_PRIVATE_KEY is not set then use $MAXWELL_USER_NAME and $MAXWELL_ACCOUNT_PASSWORD
  ## to connect using Kerberos.
    - HOST="max-wgs";
    - PREFIX="alma9";
    - if [ $MAXWELL_SSH_PRIVATE_KEY ]; then
        eval $(ssh-agent -s);
        chmod 400 "$MAXWELL_SSH_PRIVATE_KEY";
        ssh-add "$MAXWELL_SSH_PRIVATE_KEY";
      else
        echo $MAXWELL_ACCOUNT_PASSWORD | kinit $MAXWELL_USER_NAME@DESY.DE;
      fi

  script:
    - deployment/deploy-to-maxwell.sh "$MAXWELL_USER_NAME" "$HOST" "$PREFIX"

  rules:
    - if: $CI_COMMIT_BRANCH == "master"

build_container_image:
  stage: build_container
  dependencies: []
  only:
    - master
  image:
    name: tollerort.desy.de/gcr.io/kaniko-project/executor:debug
    entrypoint: [ "" ]

  variables:
    IMAGE: "crystfel"
    IMAGE_TAG_LATEST: "latest"
    IMAGE_TAG_SHA: $CI_COMMIT_SHORT_SHA
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""

  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - >
      /kaniko/executor
      --context $CI_PROJECT_DIR
      --dockerfile $CI_PROJECT_DIR/deployment/docker/Dockerfile
      --destination $CI_REGISTRY_IMAGE/$IMAGE:$IMAGE_TAG_LATEST
      --destination $CI_REGISTRY_IMAGE/$IMAGE:$IMAGE_TAG_SHA
      --build-arg HELM_VERSION=$HELM_VERSION
      --build-arg RANCHER_CLI_VERSION=$RANCHER_CLI_VERSION

  tags:
    - kubernetes

  when: manual

build_container_image_tag:
  stage: build_container
  image:
    name: tollerort.desy.de/gcr.io/kaniko-project/executor:debug
    entrypoint: [ "" ]

  variables:
    IMAGE: "crystfel"
    IMAGE_TAG_VER: $CI_COMMIT_TAG
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""

  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - >
      /kaniko/executor
      --context $CI_PROJECT_DIR
      --dockerfile $CI_PROJECT_DIR/deployment/docker/Dockerfile
      --destination $CI_REGISTRY_IMAGE/$IMAGE:$IMAGE_TAG_VER
      --build-arg HELM_VERSION=$HELM_VERSION
      --build-arg RANCHER_CLI_VERSION=$RANCHER_CLI_VERSION

  tags:
    - kubernetes

  rules:
    - if: $CI_COMMIT_TAG
      when: manual
