pipeline {
    agent any

    environment {
        // Defines the output directory for the published application
        DOTNET_PUBLISH_DIR = 'publish'
    }

    stages {
        stage('Restore Dependencies') {
            steps {
                echo 'Restoring NuGet packages...'
                sh 'dotnet restore'
            }
        }

        stage('Build') {
            steps {
                echo 'Building the project in Release mode...'
                sh 'dotnet build --configuration Release --no-restore'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                // If you have a separate test project, specify its path
                // sh 'dotnet test YourTestProject/YourTestProject.csproj --no-build'
            }
        }

        stage('Publish') {
            steps {
                echo 'Publishing the application...'
                sh "dotnet publish --configuration Release --output ${DOTNET_PUBLISH_DIR} --no-build"
            }
        }

        stage('Deploy') {
         steps {
         echo 'Deploying to the server...'
         sshagent(credentials: ['my-server-credentials']) {
         sh """
     ssh -o StrictHostKeyChecking=no root@188.245.117.146 'mkdir -p /var/www/huna-server'
     scp -r ${DOTNET_PUBLISH_DIR}/* root@188.245.117.146:/var/www/huna-server/
     ssh root@188.245.117.146 'sudo systemctl restart huna-server'
        """
 }
}
}
    }

    post {
        always {
            echo 'Pipeline finished.'
            cleanWs() // Clean up the workspace
        }
    }
}