pipeline {
    agent {
        dockerfile {
			 additionalBuildArgs "--build-arg KAGGLE_USERNAME=${params.KAGGLE_USERNAME} --build-arg KAGGLE_KEY=${params.KAGGLE_KEY} --build-arg CUTOFF=${params.CUTOFF} -t sebastianwalesa"
        }
    }
    parameters {
	    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'feature', name: 'BRANCH', type: 'PT_BRANCH'
		buildSelector(
			defaultSelector: lastSuccessful(),
			description: 'Which build to use for copying artifacts',
			name: 'BUILD_SELECTOR'
		)
	}
    stages {
        stage('Evaluate'){
		steps {
                copyArtifacts filter: '*', projectName: 's478839-create-dataset', selector: buildParameter('BUILD_SELECTOR')
                copyArtifacts filter: '*', projectName: 's478839-training/${BRANCH}', selector: buildParameter('BUILD_SELECTOR')
                copyArtifacts filter: '*', projectName: 's478839-evaluation/feature', selector: buildParameter('BUILD_SELECTOR'), optional: true
			    sh 'python3 ./ml_pytorch_results.py'
			    archiveArtifacts artifacts: 'metrics.txt, MSE.txt, RMSplot.png', followSymlinks: false
                script {
                       	    	MSE = sh (
                            	script: 'tail -1 MSE.txt',
                        	returnStdout: true
                            	).trim()
                    	}
		}
	}
    }
    post {
        success {
            emailext body: "SUCCESS, MSE = ${MSE}", subject: 's478839-evaluation', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms'
        }
        failure {
            emailext body: 'FAILURE', subject: 's478839-evaluation', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms'
        }
        unstable {
            emailext body: 'UNSTABLE', subject: 's478839-evaluation', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms'
        }
        changed {
            emailext body: 'CHANGED', subject: 's478839-evaluation', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms'
        }		
    }

}