name: Upgrade tag in appsets
description: "Set image tag in values of an application for appsets of an organization"
inputs:
  app:
    description: "The app directory name (support comma separated list or yaml list of objects), default to repository name"
    required: false
  env:
    description: "The env directory name (support comma separated list)"
    required: true
  tag:
    description: "The tag to set, default to extracted from meta-tags inputs"
    required: false
  meta-tags:
    description: "The meta-tags inputs, required if inputs.tag is not provided"
    required: false
  key:
    description: "The value key to upgrade"
    required: false
    default: "image.tag"
  commit:
    description: "Enable commit"
    required: false
  push:
    description: "Enable push"
    required: false

runs:
  using: "composite"
  steps:
  - name: 🚀 Upgrade image tag
    env:
      CD_APP: ${{ inputs.app || github.event.repository.name }}
      CD_ENV: ${{ inputs.env }}
      CD_COMMIT: ${{ inputs.commit || 'false' }}
      CD_PUSH: ${{ inputs.push || 'false' }}
      CD_KEY: ${{ inputs.key }}
    shell: bash
    run: |
      export TAG=${{ inputs.tag }}
      if [ -z "$TAG" ]; then
        IFS=',' read -ra TAGS_ARRAY <<< "${{ inputs.meta-tags }}"
        IMAGE=${TAGS_ARRAY[0]}
        export TAG=${IMAGE##*:}
      fi

      echo "Using tag: $TAG"

      export CD_WORKING_DIR="$PWD"
      cd $GITHUB_ACTION_PATH
      yarn node index.js
      cd "$CD_WORKING_DIR"
      
      git config user.name "forgejo-actions"
      git config user.email "bot@devthefuture.org"
      git add .
      
      if [ "$CD_COMMIT" != "false" ]; then
        git commit --allow-empty -m "chore: set $CD_ENV/$CD_APP image.tag=$TAG"
      fi

      if [ "$CD_PUSH" != "false" ]; then
        git push origin HEAD
      fi