@Library("p8-jenkins-utils@1.49")

def name = "task-handler"
def image = 'integration-demo-' + name
def repo = 'https://gitea.predic8.de/P8-Demo/integration-microservices-task-handler-deploy.git'


final def p8 = new de.predic8.jenkinsutils.P8(this, scm, image)

pipeline {
    agent {
        kubernetes {
            yamlFile 'ci-cd/predic8-cluster/KubernetesBuildPod.yaml'
        }
    }

    stages {

        stage('Git Checkout') {
            steps {
                script {
                    p8.git.checkTriggerCheckoutAndCheckTag()
                }
            }
        }

        stage('Build Buildah Image') {
            steps {
                container('buildah') {
                    script {
                        p8.oci.repoLogin()
                        p8.oci.buildV2('ci-cd/predic8-cluster/')
                    }
                }
            }
        }

        stage('Push Buildah Image') {
            steps {
                container('buildah') {
                    script {
                        p8.oci.pushV2()
                    }
                }
            }
        }

        stage('Deploy') {
            steps {
                dir('tmp') {
                    script {

                        p8.git.checkout url: repo

                        container('kubectl') {
                            p8.kubectl.patchKubernetesDeploymentFile(containerName: name, filePath:'auto/', fileName:'deploy.yaml')
                        }

                        p8.git.addCommitTagAndPush(url: repo)

                        container('argocd-client') {
                            p8.argo.syncAndWait(name)
                        }
                    }
                }
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
