pipeline {
    agent any

    tools {
        nodejs "Node26"
	dockerTool "SpryDocker"
    }

    environment {
	REGISTRY_URL = 'http://localhost:5000'
        IMAGE_NAME = "spry-innovation-test"
        IMAGE_TAG = "${env.BUILD_NUMBER}"
    }

    stages {
        stage('Install') {
            steps {
                sh 'npm ci'
            }
        }

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

        stage('Push') {
            steps {
                script {
                    // Use withRegistry to handle the connection
                    docker.withRegistry("${REGISTRY_URL}") {
                        def myImage = docker.build("${IMAGE_NAME}:${env.BUILD_ID}")
                        myImage.push()
                        myImage.push('latest')
                    }
                }
            }
        }
	stage('Deploy') {
            steps {
                sh '''
                    docker stop spry-innovation-test || true
                    docker rm spry-innovation-test || true

                    docker pull localhost:5000/spry-innovation-test:latest

                    docker run -d \
                      --name spry-innovation-test \
                      -p 3000:80 \
                      localhost:5000/spry-innovation-test:latest
                '''
            }
        }
    }
}