pipeline {
    agent { label 'docker && tala && linux' }

    options {
        buildDiscarder(
            logRotator(
                artifactDaysToKeepStr: '',
                artifactNumToKeepStr: '',
                daysToKeepStr: '',
                numToKeepStr: '10'
            )
        )
        timestamps()
        timeout(time: 5, unit: 'MINUTES')
    }

    triggers {
        upstream 'tala-latest'
    }

    stages {
        stage('clean') {
            steps {
                step([$class: 'WsCleanup'])
            }
        }

        stage('checkout') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: '*/master']],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [
                        [
                            $class: 'SubmoduleOption',
                            disableSubmodules: false,
                            parentCredentials: true
                        ]
                    ],
                    submoduleCfg: [],
                    userRemoteConfigs: [[
                        credentialsId: 'jenkinsatgerrit',
                        url: 'ssh://jenkins@gerrit.talkamatic.se:29418/tala'
                    ]]
                ])
            }
        }

        stage('build wheel') {
            steps {
                sh '''#!/bin/bash -ex
                    virtualenv venv
                    source venv/bin/activate

                    pip wheel --wheel-dir dist --no-deps .
                '''
            }
        }

        stage('build docker') {
            steps {
                sh '''#!/bin/bash -ex
                    docker build . -t talkamatic/tala:nightly
                '''
            }
        }

        stage('push') {
            steps {
                withCredentials([[$class: 'StringBinding', credentialsId: 'dockerhubcred', variable: 'pw']]) {
                    sh '''#!/bin/bash -ex
                        echo $pw | docker login -u zigit --password-stdin
                        docker push talkamatic/tala:nightly
                    '''
                }
            }
        }
    }
}
