{{ if .Values.pipelines.deployableResources.tasks }}
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: edp-gradle
  labels:
    app.kubernetes.io/version: "0.2"
  annotations:
    tekton.dev/pipelines.minVersion: "0.12.1"
    tekton.dev/displayName: Gradle
    tekton.dev/categories: Build Tools
    tekton.dev/tags: build-tool
    tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
spec:
  description: >-
   This task compiles, tests, and builds a Gradle project with configurable integration for Nexus, using custom Gradle settings and caching.
  workspaces:
    - name: source
      description: The workspace consisting of the gradle project.
  volumes:
    - name: settings-gradle
      configMap:
        name: {{ .Values.tekton.configs.gradleConfigMap }}
  params:
    - name: BASE_IMAGE
      description: Gradle base image.
      type: string
      default: {{ include "edp-tekton.registry" . }}/gradle:7.6.1-jdk11
    - name: PROJECT_DIR
      description: The directory containing build.gradle
      type: string
      default: "source"
    - name: ci-nexus
      type: string
      description: name of the secret for the Nexus integration
      default: ci-nexus
    - name: GRADLE_USER_CACHE
      description: Gradle user cache directory
      default: /workspace/source/cache
  steps:
    - name: compile
      image: $(params.BASE_IMAGE)
      volumeMounts:
        - name: settings-gradle
          mountPath: /var/configmap
      {{- if .Values.tekton.packageRegistriesSecret.enabled }}
      envFrom:
        - secretRef:
            name: {{ .Values.tekton.packageRegistriesSecret.name }}
      {{- end }}
      workingDir: $(workspaces.source.path)/$(params.PROJECT_DIR)
      script: |
        #!/bin/bash
        set -e

        gradle \
          -I \
          /var/configmap/init.gradle \
            clean \
            compileJava \
            -x test
      env:
        - name: XDG_CONFIG_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: GRADLE_USER_HOME
          value: $(params.GRADLE_USER_CACHE)
        - name: CI_USERNAME
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: username
        - name: CI_PASSWORD
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: password
        - name: NEXUS_HOST_URL
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: url
    - name: test
      image: $(params.BASE_IMAGE)
      volumeMounts:
        - name: settings-gradle
          mountPath: /var/configmap
      {{- if .Values.tekton.packageRegistriesSecret.enabled }}
      envFrom:
        - secretRef:
            name: {{ .Values.tekton.packageRegistriesSecret.name }}
      {{- end }}
      workingDir: $(workspaces.source.path)/$(params.PROJECT_DIR)
      script: |
        #!/bin/bash
        set -e

        gradle \
          -I \
          /var/configmap/init.gradle \
            test \
            jacocoTestReport
      env:
        - name: XDG_CONFIG_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: GRADLE_USER_HOME
          value: $(params.GRADLE_USER_CACHE)
        - name: CI_USERNAME
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: username
        - name: CI_PASSWORD
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: password
        - name: NEXUS_HOST_URL
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: url
    - name: build
      image: $(params.BASE_IMAGE)
      volumeMounts:
        - name: settings-gradle
          mountPath: /var/configmap
      {{- if .Values.tekton.packageRegistriesSecret.enabled }}
      envFrom:
        - secretRef:
            name: {{ .Values.tekton.packageRegistriesSecret.name }}
      {{- end }}
      workingDir: $(workspaces.source.path)/$(params.PROJECT_DIR)
      script: |
        #!/bin/bash
        set -e

        gradle \
          -I \
          /var/configmap/init.gradle \
            build -x test
      env:
        - name: XDG_CONFIG_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: GRADLE_USER_HOME
          value: $(params.GRADLE_USER_CACHE)
        - name: CI_USERNAME
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: username
        - name: CI_PASSWORD
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: password
        - name: NEXUS_HOST_URL
          valueFrom:
            secretKeyRef:
              name: $(params.ci-nexus)
              key: url
{{- include "resources" . | nindent 6 }}
{{ end }}
