#!groovy
// StarPU --- Runtime system for heterogeneous multicore architectures.
//
// Copyright (C) 2018-2021  Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
//
// StarPU is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.
//
// StarPU is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License in COPYING.LGPL for more details.
//

def statusHasChanged = false

pipeline
{
	agent none

	// Trigger the build
	triggers
	{
		// Poll SCM explicitly every past-half hour
		pollSCM('30 * * * *')
	}

	stages
	{
		stage('Tarball')
		{
			steps
			{
				node('autotools')
				{
					checkout scm
					sh 'contrib/ci.inria.fr/job-0-tarball.sh'
					script
					{
					       env.tarballgz = sh (script: 'ls *.tar.gz', returnStdout: true).trim()
					}
					stash includes: "${env.tarballgz}", name: 'tarballgz'
					stash includes: "starpu.pdf", name: 'doc'
					stash includes: "starpu_dev.pdf", name: 'doc_dev'
					// Stash those scripts because they are not in make dist
					dir('contrib/ci.inria.fr')
					{
						stash includes: "job-1-check.sh", name: 'script-unix-check'
					}
					archiveArtifacts artifacts: "${env.tarballgz},starpu.pdf,starpu_dev.pdf", fingerprint: true, onlyIfSuccessful: true
					deleteDir()

				}
			}
		}
		stage('Check')
		{
			steps
			{
				script
				{
					labelToSelect = 'bsd'
					listOfNodeNames = jenkins.model.Jenkins.instance.nodes.collect
					{
						node -> node.getLabelString().contains(labelToSelect) ? node.name : null
					}
					listOfNodeNames.removeAll(Collections.singleton(null))

					def p = listOfNodeNames.collectEntries
					{
						[ (it):
						{
							node(it)
							{
								dir('check-unix')
								{
									unstash 'tarballgz'
									unstash 'script-unix-check'
									sh 'chmod 755 job-1-check.sh && ./job-1-check.sh'
									deleteDir()
								}
							}
						}
					]}
					parallel p;
				}
			}
		}
	}

	post
	{
		// hooks are called in order: always, changed, aborted, failure, success, unstable
		changed
		{
			echo "Build status has changed."
			script
			{

				statusHasChanged = true
			}
		}
		success
		{
			echo "Build success."
			// email when changed to success
			script
			{
				if (statusHasChanged)
				{
					emailext(body: '${DEFAULT_CONTENT}',
						 subject: '${DEFAULT_SUBJECT}',
						 replyTo: '$DEFAULT_REPLYTO',
						 to: '$DEFAULT_RECIPIENTS')
				}
			}
		}
		failure
		{
			echo "Build failure."
			// always email on failure
			emailext(body: '${DEFAULT_CONTENT}',
				 subject: '${DEFAULT_SUBJECT}',
				 replyTo: '$DEFAULT_REPLYTO',
				 to: '$DEFAULT_RECIPIENTS')
		}
	}
}
