# Copyright 2019 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: tekton-release-cd-trigger
spec:
  schedule: "12 * * * *"  # Houly at *:12
  jobTemplate:
    spec:
      template:
        spec:
          volumes:
          - name: workspace
            emptyDir: {}
          containers:
          - name: trigger
            image: curlimages/curl
            command:
              - /bin/sh
            args:
              - -ce
              - |
                cat <<EOF > /workspace/post-body.json
                {
                  "trigger-template": "tekton",
                  "params": {
                    "git": {
                      "repository": "$GIT_REPOSITORY",
                      "revision": "$GIT_REVISION"
                    },
                    "target": {
                      "namespace": "$NAMESPACE",
                      "cluster-resource": "$CLUSTER_RESOURCE"
                    },
                    "tekton": {
                      "project": "$TEKTON_PROJECT",
                      "version": "$TEKTON_VERSION",
                      "environment": "$TEKTON_CLUSTER",
                      "bucket": "$RELEASE_BUCKET",
                      "file": "$RELEASE_FILE",
                      "post-file": "$POST_RELEASE_FILE",
                      "extra-path": "$RELEASE_EXTRA_PATH"
                    },
                    "plumbing": {
                      "repository": "$PLUMBING_REPOSITORY",
                      "revision": "$PLUMBING_REVISION"
                    }
                  }
                }
                EOF
                curl -d @/workspace/post-body.json $SINK_URL
            volumeMounts:
            - mountPath: /workspace
              name: workspace
            env:
              - name: SINK_URL
                value: "http://el-tekton-cd.default.svc.cluster.local:8080"
              - name: PLUMBING_REPOSITORY
                value: "github.com/tektoncd/plumbing"
              - name: PLUMBING_REVISION
                value: "main"
              - name: RELEASE_BUCKET
                value: "gs://tekton-releases"
              - name: RELEASE_FILE
                value: "release.yaml"
              - name: POST_RELEASE_FILE
                value: ""
              - name: RELEASE_EXTRA_PATH
                value: ""
              - name: NAMESPACE
                value: "tekton-pipelines"
              - name: CLUSTER_RESOURCE
                value: "not-a-real-cluster"
              - name: TEKTON_PROJECT
                value: "invalid"
              - name: TEKTON_VERSION
                value: "invalid"
              - name: TEKTON_CLUSTER
                value: "invalid"
          restartPolicy: Never
