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('prepare') {
            steps {
                deleteDir()
                step([
                    $class: 'CopyArtifact',
                    filter: 'dist/*.whl',
                    fingerprintArtifacts: true,
                    projectName: 'tala-latest',
                    selector: [
                        $class: 'StatusBuildSelector',
                        stable: false
                    ],
                    target: '.'
                ])
            }
        }

        stage('build') {
            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
                    '''
                }
            }
        }
    }
}
