// Jenkinsfile
pipeline {
  // Kubernetes Agent를 사용하여 파이프라인 실행
  agent {
    kubernetes {
      // Kaniko 빌드를 위한 Pod 템플릿 정의
      yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    command: ['sleep']
    args: ['infinity']
    volumeMounts:
    - name: registry-credentials
      mountPath: /kaniko/.docker
  volumes:
  - name: registry-credentials
    secret:
      secretName: registry-credentials
      items:
      - key: .dockerconfigjson
        path: config.json
'''
    }
  }

  stages {
    stage('Checkout Source Code') {
      steps {
        // Pipeline에 정의된 Git 저장소로부터 소스 코드를 체크아웃합니다.
        checkout scm
      }
    }
    stage('Build and Push with Kaniko') {
      steps {
        // kaniko 컨테이너 내에서 이미지 빌드 및 푸시 명령을 실행합니다.
        container(name: 'kaniko') {
          sh '''
            /kaniko/executor \\
              --context=dir://. \\
              --dockerfile=Dockerfile \\
              --destination=hol3imgreg-vlutfsnm.scr.private.kr-west1.e.samsungsdscloud.com/web-app/my-app:latest
          '''
        }
      }
    }
  }
}
