name: Build Docker Linux ARM64

on:
  push:
    branches:
      - main

jobs:
  build-docker-linux-arm64:
    runs-on: [self-hosted, linux, arm64, docker]
    steps:
      - name: Install packages
        run: apk add --no-cache git curl wget unzip bash nodejs npm docker

      - name: Checkout
        uses: actions/checkout@v4
        
      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Get Repository Name
        id: get_repo
        run: |
          REPO_LC="${GITHUB_REPOSITORY,,}" 
          echo "REPO_LC=$REPO_LC" >> $GITHUB_ENV
          echo "Repo: $REPO_LC"
          
      - name: Build version
        shell: bash
        run: |
            set -euo pipefail
        
            git fetch --force --tags origin
            git fetch --force origin main:refs/remotes/origin/main
        
            LAST_TAG=$(git log --first-parent --decorate=short --simplify-by-decoration --oneline origin/main \
              | grep -oE 'tag: v[^,)]+' \
              | head -n 1 \
              | sed 's/tag: //')
        
            if [[ -z "${LAST_TAG:-}" ]]; then
              echo "No release tag found"
              exit 1
            fi
        
            LAST_COMMIT=$(git rev-list -n 1 "$LAST_TAG")
            DISTANCE=$(git rev-list --count --first-parent "${LAST_COMMIT}..origin/main")
        
            BASE_VERSION="${LAST_TAG#v}"
        
            if [[ "$BASE_VERSION" == *-* ]]; then
              VERSION="${BASE_VERSION}.${DISTANCE}"
            else
              VERSION="${BASE_VERSION}-nightly.${DISTANCE}"
            fi
        
            echo "LAST_TAG=$LAST_TAG" >> "$GITHUB_ENV"
            echo "LAST_COMMIT=$LAST_COMMIT" >> "$GITHUB_ENV"
            echo "DISTANCE=$DISTANCE" >> "$GITHUB_ENV"
            echo "VERSION=$VERSION" >> "$GITHUB_ENV"
        
            echo "Last tag: $LAST_TAG"
            echo "Last tag commit: $LAST_COMMIT"
            echo "Distance to origin/main (first-parent): $DISTANCE"
            echo "Final version: $VERSION"
          
      - name: Set .NET version variables
        shell: bash
        run: |
          set -euo pipefail

          SEMVER="${VERSION}"
          BASE_NUMERIC=$(echo "$SEMVER" | sed -E 's/^([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
          BUILD_NO=$(echo "$SEMVER" | grep -oE '[0-9]+$' || echo "0")

          echo "PACKAGE_VERSION=$SEMVER" >> "$GITHUB_ENV"
          echo "ASSEMBLY_VERSION=${BASE_NUMERIC}.0" >> "$GITHUB_ENV"
          echo "FILE_VERSION=${BASE_NUMERIC}.${BUILD_NO}" >> "$GITHUB_ENV"
          echo "INFORMATIONAL_VERSION=$SEMVER" >> "$GITHUB_ENV"
          
      - name: Build Linux ARM64 Docker Image
        run: |
          docker build \
            -t gitea.penry.de/${{ env.REPO_LC }}:nightly \
            --build-arg VERSION=${{ env.PACKAGE_VERSION }} \
            .
      
      - name: Login to own image registry
        uses: docker/login-action@v3
        with:
          registry: gitea.penry.de
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      
      - name: Push Images
        run: |
          docker push gitea.penry.de/${{ env.REPO_LC }}:nightly

      - name: Save Docker Image to Tar
        run: docker save -o CouchLog-Linux-ARM64-Docker-Image.tar gitea.penry.de/${{ env.REPO_LC }}:nightly

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: CouchLog-Linux-ARM64-Docker-Image
          path: CouchLog-Linux-ARM64-Docker-Image.tar
          if-no-files-found: error
          retention-days: 5