apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: git-tag
  labels:
    dagweave.io/catalog-item: git-tag
  annotations:
    dagweave.io/catalog-version: "1.0.2"
    dagweave.io/catalog-category: "Git"
spec:
  entrypoint: tag
  templates:
    - name: tag
      inputs:
        parameters:
          - name: repo-url
          # Tag name to create and push.
          - name: tag
          # Annotation message for the tag.
          - name: message
            default: ""
          # Ref (branch/tag/SHA) the tag should point at.
          - name: ref
            default: main
          - name: author-name
            default: workflow-bot
          - name: author-email
            default: workflow-bot@dagweave.com
          - name: secret-name
            default: git-credentials
          - name: username-key
            default: username
          - name: password-key
            default: password
      container:
        # alpine/git:2.54.0
        image: alpine/git@sha256:ac6c828f15d82775d204d3d75112e1d34af8945da80ccf922713c9891ba68e7a
        command: [sh, -c]
        # Clone the repo, create an annotated tag at a ref, and push the tag.
        args:
          - |
            set -eu
            git config --global credential.helper \
              '!f() { echo "username=${GIT_USERNAME}"; echo "password=${GIT_PASSWORD}"; }; f'
            git clone "${REPO_URL}" /work
            cd /work
            git config user.name "${AUTHOR_NAME}"
            git config user.email "${AUTHOR_EMAIL}"
            git checkout "${REF}"
            git tag -a "${TAG}" -m "${MESSAGE:-${TAG}}"
            git push origin "${TAG}"
        env:
          - name: REPO_URL
            value: "{{inputs.parameters.repo-url}}"
          - name: TAG
            value: "{{inputs.parameters.tag}}"
          - name: MESSAGE
            value: "{{inputs.parameters.message}}"
          - name: REF
            value: "{{inputs.parameters.ref}}"
          - name: AUTHOR_NAME
            value: "{{inputs.parameters.author-name}}"
          - name: AUTHOR_EMAIL
            value: "{{inputs.parameters.author-email}}"
          - name: GIT_USERNAME
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.username-key}}"
          - name: GIT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.password-key}}"
