#!groovy

pipeline {
  agent none

  environment {
    BUILD_ARGS = '--build . -j 8'
    CONFIGURE_ARGS = '../.. -DCMAKE_BUILD_TYPE=Release -DREPERTORY_ENABLE_S3=ON' 
    CONFIGURE_MINGW64_ARGS = '../.. -DCMAKE_BUILD_TYPE=Release -DREPERTORY_ENABLE_S3=ON -DCMAKE_TOOLCHAIN_FILE=${WORKSPACE}/cmake/mingw-w64-x86_64.cmake' 
    REPERTORY_TEST_DIR = "${HOME}/.ci/cfg"
  }

  options {
    disableConcurrentBuilds()
    retry(2)
  }

  stages {
    stage('clean') {
      agent any

      steps {
        sh 'mkdir -p build/'
        sh 'rm -f build/*/librepertory.a'
        sh 'rm -f build/*/repertory'
        sh 'rm -f build/*/unittests'
      }
    }

    stage('alpine') {
      agent {
        dockerfile {
          filename 'alpine'
          dir 'docker/64_bit'
        }
      }

      steps {
        retry(2) {
          sleep time: 5, unit: 'SECONDS'
          cmake arguments: "${env.CONFIGURE_ARGS}", installation: 'InSearchPath', workingDir: 'build/alpine'
          cmake arguments: "${env.BUILD_ARGS}", installation: 'InSearchPath', workingDir: 'build/alpine'
          sh 'strip build/alpine/repertory'
        }
      }
    }

    stage('alpine_arm64') {
      agent {
        dockerfile {
          filename 'alpine'
          dir 'docker/aarch64'
          args '-v /usr/bin/qemu-aarch64-static:/usr/bin/qemu-aarch64-static'
        }
      }

      steps {
        retry(2) {
          sleep time: 5, unit: 'SECONDS'
          cmake arguments: "${env.CONFIGURE_ARGS}", installation: 'InSearchPath', workingDir: 'build/alpine_arm64'
          cmake arguments: "${env.BUILD_ARGS}", installation: 'InSearchPath', workingDir: 'build/alpine_arm64'
          sh 'strip build/alpine_arm64/repertory'
        }
      }
    }

    stage('mingw64') {
      agent {
        dockerfile {
          filename 'mingw64'
          dir 'docker/64_bit'
        }
      }

      steps {
        retry(2) {
          sleep time: 5, unit: 'SECONDS'
          cmake arguments: "${env.CONFIGURE_MINGW64_ARGS}", installation: 'InSearchPath', workingDir: 'build/mingw64'
          cmake arguments: "${env.BUILD_ARGS}", installation: 'InSearchPath', workingDir: 'build/mingw64'
          sh 'strip build/mingw64/repertory.exe'
        }
      }
    }

    stage('deliver') {
      agent any

      steps {
        sh 'scripts/make_package.sh alpine /home/sgraves/cert build/alpine_arm64 . /mnt/filebase 1'
        sh 'scripts/make_package.sh alpine /home/sgraves/cert build/alpine . /mnt/filebase'
        sh 'scripts/make_package.sh mingw64 /home/sgraves/cert build/mingw64 . /mnt/filebase'
      }
    }
  }
}
