Adjust site build workflow to preserve build artifact history

Adjusts the workflow building and publishing the website to the site
branch to preserve the history of the site branch.

This was done to avoid issues with the Apache backend caused by
force-pushing the new build results to an orphan branch every time.

This new behavior is a bit more brittle than the old one as it relies on
the current state of the site branch. The action will fails if the site
branch is not already present. Additionally, it might also fail if the
branch is in an unexpected state. But, as the action removes all
existing resources, this should be unlikely.

To resolve any issues caused by a missing or corrupted site branch, a
future commit will introduce a recovery workflow that can be used to
manually return the site branch to a usable base state.
diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml
index 4a1056a..eb6c817 100644
--- a/.github/workflows/deploy-site.yml
+++ b/.github/workflows/deploy-site.yml
@@ -1,4 +1,8 @@
 # Builds and deploys the site if the build was successful
+#
+# This job builds the website and deploys the build artifacts to the 'content' directory
+# Any additional files that are not part of the site build but should be published (e.g. website configuration files) must be handled explicitly
+# See step "Move additional resources to build directory" and sub-steps "Dropping old site resources" and "Adding new site configuration" of the 'publish' step
 name: Build & Deploy Site
 
 # Conditions necessary to trigger a build
@@ -17,6 +21,8 @@
       - 'src/main/asciidoc/**'
       - 'src/main/template/**'
       - '.asf.yaml.publish'
+      - '.htaccess'
+
 
 jobs:
   build:
@@ -28,9 +34,11 @@
         with:
           fetch-depth: 1
 
+
       - name: Build Site
         run: mvn clean compile
 
+
       # Determines the short sha of the commit that triggered the build
       - name: Determine Short SHA
         if: success()
@@ -40,6 +48,7 @@
           echo "::set-output name=short_sha::$short_sha"
         shell: 'bash'
 
+
       # Determines the author data of the HEAD commit
       # Sets up the author data to be used for the site deploy commit
       - name: Determine HEAD Commit Author Data
@@ -55,28 +64,62 @@
           git config user.email $author_email
         shell: 'bash'
 
-      # Publishes the site build results to a separate orphan branch
+
+      # Adds additional configuration files that are supposed to be included in the page deploy to the build directory
       #
-      # Creates and checks out a new orphan branch
-      # Creates a single commit containing only the site build artifacts and site configuration files located in the root directory
+      # This ensures that such files are preserved when checking out the publish branch
+      - name: Add additional resources to build directory
+        if: success()
+        run: |
+          cp -v .asf.yaml.publish target/site/.asf.yaml
+          cp -v .htaccess target/site/
+
+
+      # Publishes the site build results to a separate branch
+      #
+      # Checks out the site branch
+      # Replaces the site configuration files and site build artifact with the ones set up in the previous step
+      # Creates a new commit containing the new site build artifacts and site configuration files
       # The commit is created with the author data set up in the previous step
-      # Force-pushes the created branch, overwriting the previously published site build
+      # Pushes the site branch
       - name: Publish site branch
         if: success()
         run: |
           branch_name="publish"
 
-          echo "Creating orphan branch"
-          git checkout --orphan $branch_name
+          echo "Checking out site branch"
+          git fetch origin $branch_name
+          git checkout -b $branch_name origin/$branch_name
           echo
 
-          echo "Dropping content other than site data"
-          git reset
-          rm .gitignore
-          git add .asf.yaml.publish target/site
-          git clean -df
-          git mv target/site/* ./
-          git mv .asf.yaml.publish .asf.yaml
+          # Drops all existing files and folders except the base folder and the resources excluded by the regex
+          # This ensures that old configuration files that were removed on the master will be removed from the site branch as well
+          # Additional resources to exclude can be added by modifying the regex or adding new regex by using "-a -not -regex '...'"
+          echo "Dropping old site resources"
+          find . \
+            -mindepth 1 -regextype posix-extended \
+            -not -regex '^\./(target|.git)(/.*)?$' \
+            -delete -print
+          echo
+
+          echo "Adding new site configuration"
+          mv -v target/site/.asf.yaml ./
+          mv -v target/site/.htaccess ./
+          echo
+
+          echo "Adding new site build"
+          mkdir -v content
+          mv -v target/site/* content/
+          echo
+
+          # Explicitly removes build dir
+          # This checks whether there are any remaining resources that were not moved to the correct location
+          echo "Removing build dir"
+          rmdir -v -p target/site
+          echo
+
+          echo "Staging new content"
+          git add .
           echo
 
           echo "Committing changes"
@@ -84,5 +127,5 @@
           echo
 
           echo "Pushing site branch"
-          git push -f origin $branch_name
+          git push origin $branch_name
         shell: 'bash'