pipeline {
    agent any
    options {
        // This is required if you want to clean before build
        skipDefaultCheckout(true)
    }
    stages {
        stage('Checkout GitLab') {
               steps {
                  // Clean before build
                   cleanWs()
                   // Tạo jenkins credential với username + password gitlab sau đó lấy ID để config vào đây.
                   // Tiếp theo tạo item jenkins pipeline cấu hình url gitlab để kéo file Jenkinfile
                   checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '2b2eb7f0-62df-4781-9fe6-34bc9b616361', url: 'https://gitlab.com/thanhdevapp/myhome-web.git']])
               }
        }

        stage('Install Package') {
            steps {
                dir('') {
                    sh 'npm install --force'
                }
            }
        }

        stage('Build') {
            steps {
                dir('') {
                    sh 'npm run build-jenkins'
                }
            }
        }

        // cần config cấu user và group để chown trên host ubuntu
        stage('Deploy') {
            steps {
                sh 'sudo rm -rf /var/www/vhosts/myhomevietnam.com/httpdocs/nuxt-build'
                sh 'sudo rsync --chown=myhomevietnam.com_e53v7ufwbwh:psacln --update -raz --progress --exclude=node_modules --exclude=.git --exclude=".env" /var/lib/jenkins/jobs/myhomevietnam.com/workspace/* /var/www/vhosts/myhomevietnam.com/httpdocs'
            }
        }
       // stage('Restart PM2')
       // {
       //     steps {
       //         sh '/var/www/vhosts/myhomevietnam.com/httpdocs/restart.sh'
       //     }
       // }

    }

    post {
         // Clean after build
        always {
            cleanWs()
        }
    }
}

