{{ if .Values.pipelines.deployableResources.tasks }}
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: 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 runs a Gradle build in a specified project directory with configurable integration for Nexus and SonarQube, using custom settings and extra build arguments.
  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: "."
    - name: ci-nexus
      type: string
      description: name of the secret for the Nexus integration
      default: ci-nexus
    - name: ci-sonarqube
      type: string
      description: name of the secret for the Sonarqube integration
      default: "ci-sonarqube"
    - name: EXTRA_ARGS
      description: Extra arguments to add to the gradle build
      default: |
        -Dorg.gradle.internal.publish.checksums.insecure=true \
        publish
  steps:
    - name: gradle-tasks
      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 \
          -PnexusLogin=${CI_USERNAME} \
          -PnexusPassword=${CI_PASSWORD} \
          $(params.EXTRA_ARGS)
      env:
        - name: XDG_CONFIG_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: GRADLE_USER_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: SONAR_USER_HOME
          value: $(workspaces.source.path)/$(params.PROJECT_DIR)
        - name: SONAR_TOKEN
          valueFrom:
            secretKeyRef:
              name: $(params.ci-sonarqube)
              key: token
        - name: SONAR_HOST_URL
          valueFrom:
            secretKeyRef:
              name: $(params.ci-sonarqube)
              key: url
        - 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: SNAPSHOTS_REPO_PATH
          valueFrom:
            configMapKeyRef:
              name: {{ .Values.tekton.configs.gradleConfigMap }}
              key: SNAPSHOTS_REPO_PATH
              optional: true
        - name: RELEASES_REPO_PATH
          valueFrom:
            configMapKeyRef:
              name: {{ .Values.tekton.configs.gradleConfigMap }}
              key: RELEASES_REPO_PATH
              optional: true
{{- include "resources" . | nindent 6 }}
{{ end }}
