pipeline {

    agent {
        label 'localhost_Agent'
    }
    tools {
        jdk 'Java17'
        maven 'Maven3'
    }
    environment {
        APP_NAME = "notificationsvc"
        RELEASE = "1.0.0"
        DOCKER_REGISTRY_URL = "http://172.29.3.41:5000"
        IMAGE_NAME = "172.29.3.41:5000/${APP_NAME}:${RELEASE}.${BUILD_NUMBER}"
    }
    stages {
        stage('Cleanup Workspace') {
            steps {
                cleanWs()
            }
        }
        stage('Checkout from SCM') {
            steps {
                git branch: 'master', credentialsId: 'gitLab', url: 'https://git.hiast.edu.sy/abdullh.alsoleman/notificationsvc.git'
            }
        }
        stage("Build & Push Docker Image") {
            steps {
                script {
                    def dockerImage = docker.build("${IMAGE_NAME}")
                    dockerImage.push()
                }
            }
        }
         stage('Update Manifest File') {
            steps {
                script {
                    def manifestRepoUrl = 'https://github.com/alisaeed24/argocd.git'
                    def credentialsId = 'github'

                    withCredentials([string(credentialsId: credentialsId, variable: 'GITHUB_TOKEN')]) {
                        sh "git clone ${manifestRepoUrl} manifest-repo"
                        dir('manifest-repo/deployment') {
                            sh """
                                sed -i 's|image: .*|image: ${IMAGE_NAME}|' Notification-Deployment.yaml
                            """
                            sh """
                                git config user.email "alisaeed23369@gmail.com"
                                git config user.name "alisaeed24"
                                git add Job-Deployment.yml
                                git commit -m "Update NotificationService image to ${IMAGE_NAME}"
                                git push https://${GITHUB_TOKEN}@github.com/alisaeed24/argocd.git master
                            """
                        }
                    }
                }
            }
        }
    }
}
