apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: git-checkout
  labels:
    dagweave.io/catalog-item: git-checkout
  annotations:
    # The catalog semver this manifest was authored at. Keep in sync with
    # metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.2"
    dagweave.io/catalog-category: "Git"
spec:
  entrypoint: checkout
  templates:
    - name: checkout
      inputs:
        parameters:
          # HTTPS clone URL of the repository.
          - name: repo-url
          # Branch, tag, or commit SHA to check out.
          - name: revision
            default: main
          # Directory to clone into.
          - name: path
            default: /work
          # Name of the Kubernetes Secret holding git credentials. For a public
          # repo the Secret may be absent (the credential env is optional).
          - name: secret-name
            default: git-credentials
          - name: username-key
            default: username
          - name: password-key
            default: password
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # alpine/git:2.54.0
        image: alpine/git@sha256:ac6c828f15d82775d204d3d75112e1d34af8945da80ccf922713c9891ba68e7a
        command: [sh, -c]
        # Reproduces the old dagweave "Check out a git repository" blueprint: clone a repo at a
        # revision. Credentials (username/password, e.g. a PAT as the password)
        # come from a Secret via env; a blank username means an anonymous clone.
        args:
          - |
            set -eu
            if [ -n "${GIT_USERNAME:-}" ]; then
              git config --global credential.helper \
                '!f() { echo "username=${GIT_USERNAME}"; echo "password=${GIT_PASSWORD}"; }; f'
            fi
            mkdir -p "${DEST}"
            git clone "${REPO_URL}" "${DEST}"
            git -C "${DEST}" checkout "${REVISION}"
            git -C "${DEST}" log --oneline -1
        env:
          - name: REPO_URL
            value: "{{inputs.parameters.repo-url}}"
          - name: REVISION
            value: "{{inputs.parameters.revision}}"
          - name: DEST
            value: "{{inputs.parameters.path}}"
          - name: GIT_USERNAME
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.username-key}}"
                optional: true
          - name: GIT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.password-key}}"
                optional: true
