apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: argocd-task-sync-and-wait
  namespace: default
spec:
  description: |-
    This task syncs (deploys) an Argo CD application and waits for it to be healthy.
    To do so, it requires the address of the Argo CD server and some form of authentication either a username/password or an authentication token.
  params:
  - description: name of the application to sync
    name: application-name
    type: string
  - default: HEAD
    description: the revision to sync to
    name: revision
    type: string
  - default: --
    name: flags
    type: string
  - default: v2.2.2
    name: argocd-version
    type: string
  stepTemplate:
    computeResources: {}
    envFrom:
    - configMapRef:
        name: argocd-env-configmap
    - secretRef:
        name: argocd-env-secret
  steps:
  - computeResources: {}
    image: argoproj/argocd
    name: login
    script: |
      echo "ARGOCD_SERVER   = " $ARGOCD_SERVER
      echo "ARGOCD_USERNAME = " $ARGOCD_USERNAME
      echo "ARGOCD_PASSWORD = " $ARGOCD_PASSWORD
      if [ -z "$ARGOCD_AUTH_TOKEN" ]; then
        yes | argocd login "$ARGOCD_SERVER" --username="$ARGOCD_USERNAME" --password="$ARGOCD_PASSWORD";
      fi
      argocd app sync "$(params.application-name)" --revision "$(params.revision)" "$(params.flags)"
      argocd app wait "$(params.application-name)" --health "$(params.flags)"
