on:
  push:
    branches:
      - main
  pull_request:

env:
  TZ: Europe/Berlin

jobs:
  build:
    runs-on: docker
    container:
      image: docker.io/hugomods/hugo:latest
    steps:
      - name: Pipeline info
        run: |
          echo "Run triggered by ${{ github.event_name }} (${{ github.event.action }}) on ref ${{ github.ref_name }}"

      - name: Install packages
        run: |
          apk update
          # For CI actions.
          apk add nodejs-current
          # For website build.
          apk add tzdata coreutils curl jq git
          # For uploading.
          apk add rsync openssh

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # pull full history for page lastmod by git commit date
          submodules: recursive

      - name: Patch baseURL (staging only)
        if: github.ref_name != 'main'
        run: |
          sed -i "s#baseURL: 'https://hackertours.hamburg.ccc.de/'#baseURL: 'https://staging.hackertours.hamburg.ccc.de/pr${{ github.event.pull_request.number }}/'#" hugo.yaml

      - name: Build website (prod)
        if: (github.event_name == 'push' || github.event_name == 'schedule') && github.ref_name == 'main'
        run: |
          hugo
      - name: Build website (staging)
        if: github.ref_name != 'main'
        run: |
          hugo --buildFuture --buildDrafts

      - name: Deploy - Prepare keys
        if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event_name == 'schedule'
        run: |
          echo "${{ secrets.SSH_DEPLOY_KEY }}" > deploykey.priv
          chmod 400 deploykey.priv
          echo "${{ secrets.SSH_KNOWN_HOSTS_FILE }}" > ./known_hosts

      - name: Deploy - Upload PR to staging
        if: github.event_name == 'pull_request'
        run: |
          echo "Deploying to staging.hamburg.ccc.de/pr${{ github.event.pull_request.number }}"
          rsync -v -r --delete -e "ssh -i deploykey.priv -o 'UserKnownHostsFile ./known_hosts'" public/ ht-ccchh-website-deploy@public-web-static-intern.hamburg.ccc.de:/var/www/staging.hackertours.hamburg.ccc.de/pr${{ github.event.pull_request.number }}/
      - name: Deploy - Add comment to PR with staging URL
        if: github.event_name == 'pull_request' && github.event.action == 'opened'
        run: |
          curl \
            -X POST \
            ${{ github.event.pull_request.base.repo.url }}/issues/${{ github.event.pull_request.number }}/comments \
            -H "Content-Type: application/json" \
            -H "Authorization: token $GITHUB_TOKEN" \
            --data '{ "body": "You can view your changes at https://staging.hackertours.hamburg.ccc.de/pr${{ github.event.pull_request.number }}/" }'

      - name: Deploy - Upload to prod
        if: (github.event_name == 'push' || github.event_name == 'schedule') && github.ref_name == 'main'
        run: |
          rsync -v -r --delete -e "ssh -i deploykey.priv -o 'UserKnownHostsFile ./known_hosts'" public/ ht-ccchh-website-deploy@public-web-static-intern.hamburg.ccc.de:/var/www/hackertours.hamburg.ccc.de/

      - uses: actions/upload-artifact@v3
        if: github.event_name == 'pull_request'
        with:
          name: website-build
          path: public/