on:
  push:
    tags:
      - "*.*.*"

name: Publish Release Package

jobs:
  publish:
    env:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
    runs-on: ubuntu-latest
    steps:
      - name: 🛡️ 🔒 Add Siteworx CA Certificates
        run: |
          curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
          update-ca-certificates

      - name: Checkout code
        uses: actions/checkout@v2

      - name: Zip files
        id: zip_files
        run: |
          apt update && apt install -y zip
          
          echo "Creating zip file"
          zip -r release-${{ gitea.ref_name }}.zip . -x ".idea*" "*.git*" "*.github*" "*.gitea*" "node_modules/*" "dist/*" "coverage/*" "build/*" "logs/*" "tmp/*"
          echo "Zip file created: release.zip"

      - name: 📦 Publish Build Artifacts
        uses: christopherhx/gitea-upload-artifact@v4
        with:
          name: release-${{ gitea.ref_name }}.zip
          path: release-${{ gitea.ref_name }}.zip
          retention-days: 1

      - name: Upload release package
        run: |
          output=$(curl --user ${{ secrets.PACKAGE_PUBLISH_USER }}:${{ secrets.PACKAGE_PUBLISH_TOKEN }} \
            --upload-file release-${{ gitea.ref_name }}.zip \
            ${{ gitea.server_url }}/api/packages/php-packages/composer?version=${{ gitea.ref_name }})
          if [ $? -ne 0 ]; then
            echo "Error uploading release package"
            exit 1
          fi
          echo "Upload successful"