pipeline {
    environment {
    registry = "https://seporegistry.azurecr.io"
    registryCredential = "e4feeff0-0582-466d-ac00-f04bdbe42473"
    dockerImage = ''
    dockerImageTest = ''
    }
    agent any
    stages {
       stage ('Building base image') {
            steps {
                script {
                    dockerImageBase = docker.build("base_node", '-f ./docker/base/Dockerfile .')
                }
            }
       }
       stage ('Building test image') {
           steps{
               script {
                   dockerImageTest = docker.build("salvatti_frontend" + ":master-test", '-f ./docker/test/Dockerfile .')
                  }
               }
           }
       stage ('Run docker container npm install') {
           steps{
               script {
                        withEnv([
                            /* Override the npm cache directory to avoid: EACCES: permission denied, mkdir '/.npm' */
                            'npm_config_cache=npm-cache',
                            /* set home to our current directory because other bower
                            * nonsense breaks with HOME=/, e.g.:
                            * EACCES: permission denied, mkdir '/.config'
                            */
                            'HOME=.',
                        ]) {
                                           dockerImageTest.inside{
                                                sh 'npm ci'
                                                 }
                                              }
                                           }
                        }
           }
       stage ('Run docker container testing'){
           steps{
               script {
                   dockerImageTest.inside{
                        sh 'npm run test:unit'
                  }
                }
              }
           }
       stage ('Run docker container linter'){
           steps{
               script {
                   dockerImageTest.inside{
                    sh 'npm run lint'
                    }
                  }
               }
           }
       stage ('Building production image') {
            steps{
                script {
                    dockerImage = docker.build("salvatti_frontend" + ":master", '-f ./docker/prod/Dockerfile .')
                      }
                    }
            }
        stage('Deploy Image') {
            steps{
                script {
                 docker.withRegistry( registry, registryCredential ) {
                   dockerImage.push()
                 }
               }
             }
           }
        stage('Run application') {
           steps{
               script{
                    sh 'docker-compose -f /home/yellows/salvatti_frontend/docker-compose.yaml up -d'
               }
           }
       }
    }
}


