apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
  labels:
    app: jenkins
spec:
  selector:
    matchLabels:
      app: jenkins
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      serviceAccountName: jenkins
      initContainers:
        - name: defaultdata
          image: public.ecr.aws/n4q1q0z2/pharmaledger-jenkins-predefined-data:1.8
          imagePullPolicy: Always
          command: [ "sh" ]
          args:
            - "-cx"
            - "
               ls /var/jenkins-backup;
               cd /var/jenkins_home;
               rm -r *;
               echo \"Jenkins home data\";
               ls;
               cd /var/jenkins-backup;
               echo \"Copy data to Jenkins home\";
               cp -a . /var/jenkins_home;
               cd /var/jenkins_home;
               echo \"Jenkins home data content\";
               ls;
               cd users;
               ls;
               cd ../plugins;
               ls;
               echo \"Restored from the predefined data\" > restored.data;
              "
          volumeMounts:
            - mountPath: /var/jenkins_home
              name: jenkins
      containers:
        - name: jenkins
          image: jenkins/jenkins:2.263.4-lts-alpine
          imagePullPolicy: IfNotPresent
          tty: true
          stdin: true
          env:
            - name: JAVA_OPTS
              value: -Xmx2048m -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-scripts;"
          ports:
            - containerPort: 8080
              protocol: TCP
            - containerPort: 50000
              protocol: TCP
          volumeMounts:
            - mountPath: /var/jenkins_home
              name: jenkins
      restartPolicy: Always
      securityContext:
        runAsUser: 0
      terminationGracePeriodSeconds: 30
      volumes:
        - name: jenkins
          persistentVolumeClaim:
            claimName: jenkins-claim
