node{ 
   stage('Prepare Node and NPM') { // confirm nodejs plugin and setting
   env.NODEJS_HOME = "${tool 'NodeJS'}"
    //on linux / mac
    env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
    // on windows
    // env.PATH="${env.NODEJS_HOME};${env.PATH}"
    sh 'npm --version'
   
   }

    
     
    stage('checkout'){
        deleteDir()
        checkout scm
        echo 'Pulling..................' + env.BRANCH_NAME
    }
    stage('Stop Container'){
        sh 'docker ps | grep "workflow_" -v | awk -F " " \'{if(NR>1)print $1}\' | xargs docker kill | xargs docker rm || true'
        sh 'docker system prune -f'        
    }
    //stage('Build Container')
    // {
    //     imageApache=docker.build('server-apache-dev', '--no-cache -f build/docker/apache/Dockerfile .')
    //     imageSql=docker.build('server-mysql-dev', '--no-cache -f build/docker/mysql/Dockerfile .')
    //     containerMysql=imageSql.run('--name server-mysql-dev')
    //     containerApache=imageApache.run('-p 8080:80 --link server-mysql-dev:mysql')
    // }
    // stage('Waiting Container'){
    //     waitUntil{
    //         try{
    //             sh "docker exec ${containerApache.id} php -v"
    //         return true
    //     } catch(Exception $e){
    //         return false
    //     }
    //  }
    // }
    stage('stage 1bis'){
        sh 'docker -v'
        sh 'docker ps' 
    }    
    
    stage('stage 2bis'){
        sh 'ls -ls'
    }
    // stage('Test') 
    // {
        
    //    dir("${env.WORKSPACE}/angular-client")
    //    {
    //         // sh 'npm i @angular-devkit/build-angular'
          		
	// 		// try
	// 		// {
	// 		// 	sh 'npm run test-ci'
	// 		// } 
	// 		// catch(err) 
	// 		// {
	// 		// 	sh 'echo TEST FAILED'
	// 		// 	junit 'target/surefire-reports/TESTS-TestSuite.xml/*.xml'
	// 		// 	throw err
	// 		// }
            
    //         def karma = docker.image('trion/ng-cli-karma')
    //         karma.pull()

    //         try 
    //         {
    //             karma.run(' -u $(id -u) -v ${WORKSPACE}:/app trion/ng-cli-karma ')
    //             karma.inside 
    //             {
    //                 sh 'npm install'
                
    //                 try 
    //                 {
    //                     sh ('karma start karma.conf.js')
    //                 }
    //                 catch(err) 
    //                 {
    //                     sh 'echo TEST FAILED'
    //                     step([$class: 'JUnitResultArchiver', testResults: 'target/surefire-reports/TESTS-TestSuite.xml/*.xml', healthScaleFactor: 1.0])
    //                     throw err
    //                 }
    //                 sh 'echo DO SOMETHING ELSE AFTER TEST'
    //             }
    //             sh 'ls -al '
    //         } 
    //         catch(err) 
    //         {
    //         sh 'echo RUN DOCKER FAILED'
    //         throw err
    //         }
    //    }
    // }


    // def imageApache = stage("Build apache preprod")
	// {
	// 	docker.build("server-apache-preprod", "--no-cache --label traefik.enable=true --label traefik.http.routers.server-apache-preprod.rule='Host(`angular-preprod.foodgame.fr`)' --label traefik.http.routers.server-apache-preprod.entrypoints=websecure --label traefik.http.routers.server-apache-preprod.tls.certresolver=myresolver -f build/docker/httpd/Dockerfile .")
	// }
	
	// def imageExpress = stage("Build express preprod")
	// {
	// 	docker.build("server-express-preprod", "--no-cache   -f express-server/Dockerfile express-server")
	// }

    /*stage("Run Container express preprod")
	{
		containerSql=imageExpress.run("--name server-express-preprod -v /var/log:/var/www/app/log/root  --link database_preprod:mongo --network=web")
	}
    stage("Run Container apache preprod")
	{
		containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
	}*/


	/*stage("Run Container express preprod")
	{
		containerSql=imageExpress.run("--name server-express-preprod -v /home/projet/fatboar/${e}/mysql:/var/lib/mysql --network=web")
	}
	
	stage("Run Container apache preprod")
	{
		containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
	}*/


    // stage("Push preprod images to nexus")
	// {
	// 	/*docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
	// 	{
	// 		imageApache.push('latest')
	// 		imageExpress.push('latest')
	// 	}*/
	// }
    stage('SonarQube analysis') 
    {  
         def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
        withSonarQubeEnv('SonarQube') 
        { 
        // If you have configured more than one global server connection, you can specify its name
        sh "${scannerHome}/bin/sonar-scanner  \
        -Dsonar.projectKey=FatboarProject \
        -Dsonar.sources=. \
        -Dsonar.host.url=https://sonarqube.foodgame.fr \
        -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
        }
    }
	

    // stage('Build Docker MEAN Stack(PreProduction Deployment)') 
    // {
    //     sh 'docker-compose -v'
    //     sh 'docker-compose build'
    //     sh 'docker-compose up -d'
    // }
   
    stage('Fin du Pipeline') 
    {
       sh 'echo "Félicitation tout c\'est bien déroulé!"'  
   }
}