name: Merge actions

on:
  push:
    branches:
    - master

jobs:
  build_docs:
    name: "Build documentation tarball"
    runs-on: ubuntu-24.04
    steps:
    - name: Disable AppArmor restriction for bubblewrap
      run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0

    - name: Checkout code
      uses: actions/checkout@v2
      # BuildStream requires tags to be able to find its version.
      with:
        fetch-depth: 0

    - name: Give `testuser` ownership of the source directory
      run: sudo chown -R 1000:1000 ${GITHUB_WORKSPACE}

    - name: Build documentation using Docker Compose
      run: |
        docker compose \
          --env-file ${GITHUB_WORKSPACE}/.github/common.env \
          --file ${GITHUB_WORKSPACE}/.github/compose/ci.docker-compose.yml \
          run \
          docs

        # Restore permissions to the current user
        sudo chown -R ${USER} ${GITHUB_WORKSPACE}

        # Include a tarball in the published docs, allowing for
        # easy re-publishing of master docs on docs.buildstream.build
        tar -C doc/build/html -zcf docs.tgz .

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: docs
        path: |
          doc/build/html
          docs.tgz

  publish_docs:
    needs: build_docs
    runs-on: ubuntu-24.04
    steps:

    - name: Download artifact
      uses: actions/download-artifact@v4
      with:
        name: docs
        path: docs

    - name: Checkout code
      uses: actions/checkout@v2
      with:
        ref: gh-pages
        path: pages
        fetch-depth: 0

    - name: Update repo
      run: |

        # First reset the branch state to the initial commit, this ensures that
        # we do not pollute the repository with the history of every docs package
        # we've ever published (history of docs packages for major releases is
        # also stored as GitHub release assets)
        #
        cd pages/
        git reset --hard GH_PAGES_FIRST_COMMIT

        # Copy the docs asset over and overwrite the orphan gh-pages branch, ensure
        # that we disable GitHub's jekyll by creating the .nojekyll file, otherwise
        # it will interfere with the rendering of the site.
        #
        cp -a ../docs/doc/build/html/* .
        cp -a ../docs/docs.tgz .
        touch .nojekyll

        git add .
        git config --local user.email "merge-ci@ponyland"
        git config --local user.name  "Github Actions Nightly Job"
        git commit -m "Update repo for docs build $GITHUB_RUN_NUMBER"
        git push --force "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.git" gh-pages
