#!groovy

pipeline {
	agent {
        kubernetes {
            defaultContainer 'jnlp'
            yaml """
apiVersion: v1
kind: Pod
metadata:
  name: wedding-fe-"""+BRANCH_NAME+"""-build
spec:
  nodeSelector:
    kubernetes.io/arch: arm64
  serviceAccountName: jenkins-admin
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    command: ["tail"]
    args: ["-f", "/dev/null"]
    volumeMounts:
    - name: kaniko-secret
      mountPath: /kaniko/.docker
  - name: deploy
    image: 192.168.195.195:30000/nginx:latest
    command: ["tail"]
    args: ["-f", "/dev/null"]
  restartPolicy: Never
  volumes:
  - name: kaniko-secret
    secret:
      secretName: dockercred
      items:
        - key: .dockerconfigjson
          path: config.json
            """
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo "No build steps configured."'
            }
        }
        stage('Test') {
            steps {
                sh 'echo "No test steps configured."'
            }
        }
        stage('Docker Build and Push') {
            steps {
                container("kaniko") {
                    sh '/kaniko/executor --dockerfile `pwd`/cicd/'+env.BRANCH_NAME+'/Dockerfile --context `pwd` --destination 192.168.195.195:30000/wedding-fe:'+env.BRANCH_NAME
                }
            }
        }
        stage('Kubernetes Deploy') {
                steps {
                    container("deploy") {
                        sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.24.3/bin/linux/arm64/kubectl"'  
                        sh 'chmod u+x ./kubectl'  
                        sh 'sed -ie "s/{{replaceMe}}/`date +%s`/g" `pwd`/cicd/'+env.BRANCH_NAME+'/deployment.yaml'
                        sh './kubectl apply -f `pwd`/cicd/'+env.BRANCH_NAME+'/deployment.yaml'
                    }
                }
            }
  }
}