{{ if .Values.pipelines.deployableResources.tasks }}
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: create-ecr-repository
  labels:
    app.kubernetes.io/based-on: "0.6"
    {{- include "edp-tekton.labels" . | nindent 4 }}
  annotations:
    tekton.dev/pipelines.minVersion: "0.17.0"
    tekton.dev/categories: Image Build
    tekton.dev/tags: image-build
    tekton.dev/displayName: "Init ECR repository"
    tekton.dev/platforms: "linux/amd64"
spec:
  description: >-
   This task creates an Amazon ECR repository if it doesn't already exist, based on the provided repository name and AWS region configuration.
  params:
    - name: REPO_NAME
      description: "The name of the ecr repository where we are going to push the image"
    - name: edp-config
      type: string
      description: "This configmap holds aws_region parameter"
      default: edp-config
  steps:
    - name: init-repository
      image: {{ include "edp-tekton.registry" . }}/amazon/aws-cli:2.7.35
      {{ if ne .Values.global.platform "openshift" }}
      env:
        - name: REPO_NAME
          value: "$(params.REPO_NAME)"
        - name: AWS_DEFAULT_REGION
          valueFrom:
            configMapKeyRef:
              name: "$(params.edp-config)"
              key: 'aws_region'
      command: ["/bin/sh"]
      args:
        - "-c"
        - |
          ECR_REPO_NAME=$(echo "${REPO_NAME}" | cut -d'/' -f2-)
          aws ecr describe-repositories --repository-names "$ECR_REPO_NAME" || aws ecr create-repository --repository-name "$ECR_REPO_NAME"
      {{ else }}
      command: [ "/bin/sh" ]
      args: [ "-c", "echo 'Openshift cluster, create-ecr-repository stage skipped'" ]
      {{ end }}
{{ end }}
