name: "Containers: Update"

on:
  workflow_dispatch:
  push:
    branches:
    - main
    paths:
    - ".github/workflows/containers.update.yaml"
  schedule:
  - cron: "0 3 * * *"

jobs:
  get-versions:
    name: Get Apps Versions
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v2
      with:
        token: ${{ secrets.BOT_TOKEN }}
        fetch-depth: 1
    - name: Fetch new application versions
      run: |
        find ./containers/apps -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | while read app; do
          if test -f "./containers/apps/${app}/latest-version.sh"; then
            version=$(bash "./containers/apps/${app}/latest-version.sh")
            if [[ ! -z "${version}" || "${version}" != "null" ]]; then
              echo "${version}" | tee "./containers/apps/${app}/VERSION" > /dev/null
              echo "App: ${app} using version: ${version}"
            fi
          fi
          if test -f "./containers/apps/${app}/BASE"; then
            if test -f "./containers/apps/${app}/latest-base.sh"; then
              base=$(bash "./containers/apps/${app}/latest-base.sh")
              if [[ ! -z "${base}" || "${base}" != "null" ]]; then
                echo "${base}" | tee "containers/apps/${app}/BASE" > /dev/null
                echo "App: ${app} using Base: ${base}"
              fi
            fi
          fi
        done
    - name: Fetch new base versions
      run: |
        find ./containers/base -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | while read app; do
          if test -f "./containers/base/${app}/latest-version.sh"; then
            version=$(bash "./containers/base/${app}/latest-version.sh")
            if [[ ! -z "${version}" || "${version}" != "null" ]]; then
              echo "${version}" | tee "./containers/base/${app}/VERSION" > /dev/null
              echo "${app} ${version}"
            fi
          fi
        done
    - name: Add, commit and push to applications version file
      run: |
        if [[ -n $(git status --porcelain) ]]; then
          git config user.name "TrueCharts Bot"
          git config user.email "bot@truecharts.org"
          git add -A
          git commit -sam "chore: added new container release versions" || exit 0
          git push
        fi
