# Note: change to batch/v1beta1 once we bump to k8s 1.8
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: gitops-sync
spec:
  schedule: "*/5 * * * *"
  successfulJobsHistoryLimit: 2
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: gen3job
        spec:
          restartPolicy: Never
          serviceAccountName: jenkins-service
          containers:
            - name: awshelper
              image: quay.io/cdis/awshelper:master
              imagePullPolicy: Always
              env:
                - name: slackWebHook
                  valueFrom:
                    configMapKeyRef:
                      name: global
                      key: slack_webhook
                      optional: true
                - name: gen3Env
                  valueFrom:
                    configMapKeyRef:
                      name: global
                      key: environment
                - name: JENKINS_HOME
                  value: ""
                - name: GEN3_NOPROXY
                  value: none
                - name: KUBECTL_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
              command: ["/bin/bash" ]
              args:
                - "-c"
                - |
                  set -i
                  source ~/.bashrc
                  export JENKINS_HOME=none
                  export vpc_name="$(g3kubectl get configmap global -o json | jq -r .data.environment)"
                  cd "$GEN3_HOME"
                  git pull
                  env
                  if gen3 klock lock gitops-sync gitops 2700; then
                    gen3 gitops sync --slack
                    gen3 klock unlock gitops-sync gitops
                  else
                    echo "Failed to acquire gitops-sync lock: exiting without attempting to sync"
                  fi
                  echo "Exit code: $?"
