pipeline {
    agent {
        kubernetes {
            yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: jnlp
    image: jenkins/inbound-agent:latest
  - name: docker
    image: docker:latest
    command:
    - cat
    tty: true
    volumeMounts:
    - name: docker-sock
      mountPath: /var/run/docker.sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
"""
        }
    }
pipeline {
    agent any // Runs on an Ubuntu-based Jenkins agent

    environment {
        REGISTRY_URL = 'git.winteraccess.id'
        IMAGE_NAME = 'winter-access/backend_nam'
        KUBE_CONFIG_ID = '1c8bd8d9-1590-468c-afc7-24495d4330dc'
        CREDENTIALS_ID = '45c25ade-b1f8-455b-bdd6-e11ff141b70c'
        DOCKERUSRPASS  = '08063d26-0005-4942-9b87-9d819a13b973'
    }

    stages {
        stage('Checkout Code') {
            steps {
                container('jnlp') {
                    git branch: 'dev', url: 'https://git.winteraccess.id/winter-access/backend_nam.git', credentialsId: "${CREDENTIALS_ID}"
                }
            }
        }

        stage('Get Short SHA') {
            steps {
                container('jnlp') {
                    script {
                        env.SHORT_SHA = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                    }
                }
            }
        }

        stage('Login to Registry') {
            steps {
                container('docker') {
                    script {
                        withCredentials([usernamePassword(credentialsId: "${DOCKERUSRPASS}", usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_TOKEN')]) {
                            sh """
                            echo "$REGISTRY_TOKEN" | docker login "$REGISTRY_URL" --username ${REGISTRY_USER} --password-stdin
                            """
                        }
                    }
                }
            }
        }
        
        stage('Build and Push Docker Image') {
            steps {
                container('docker') {
                    script {
                        def imageTag = "dev-${env.SHORT_SHA}"
                        sh """
                        docker build -t ${REGISTRY_URL}/${IMAGE_NAME}:${imageTag} \
                                     -t ${REGISTRY_URL}/${IMAGE_NAME}:dev \
                                     -t ${REGISTRY_URL}/${IMAGE_NAME}:latest \
                                     -f deploy/docker/Dockerfile .
                        docker push ${REGISTRY_URL}/${IMAGE_NAME}:${imageTag}
                        docker push ${REGISTRY_URL}/${IMAGE_NAME}:dev
                        docker push ${REGISTRY_URL}/${IMAGE_NAME}:latest
                        """
                    }
                }
            }
        }

        stage('Deploy to Kubernetes') {
            steps {
                container('jnlp') {
                    script {
                        withCredentials([string(credentialsId: "${KUBE_CONFIG_ID}", variable: 'KUBE_CONFIG')]) {
                            writeFile file: 'kubeconfig', text: KUBE_CONFIG
                            sh """
                            kubectl apply -f deploy/kubernetes/dev.yaml --kubeconfig=kubeconfig
                            kubectl set image deployment/backend backend=${REGISTRY_URL}/${IMAGE_NAME}:dev-${env.SHORT_SHA} -n nam-backend-dev --kubeconfig=kubeconfig
                            """
                        }
                    }
                }
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
