name: Build and Publish

on:
  push:
    branches:
      - main
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  docker:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: true # Fetch hugo themes
          fetch-depth: 0 # Fetch all history for .GitInfo and .LastMod

      - name: Setup node (NPM)
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Install node dependencies
        run: npm install

      - name: Setup hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: "0.125.7"
          extended: true

      # Will use the build.sh script to build the page using hugo.
      # The resulting page will be in the ./public directory
      - name: Build the hugo page
        run: ./scripts/build.sh

      # The current version (v2) of Docker's build-push action uses
      # buildx, which comes with BuildKit features that help us speed
      # up our builds using additional cache features. Buildx also
      # has a lot of other features that are not as relevant to us.
      #
      # See https://github.com/docker/build-push-action
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to Github Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN  }}

      # Create a commit SHA-based tag for the container repositories
      - name: Create SHA Container Tag
        id: sha_tag
        run: |
          tag=$(cut -c 1-7 <<< $GITHUB_SHA)
          echo "::set-output name=tag::$tag"

      # Build and push the container to the GitHub Container
      # Repository. The container will be tagged as "latest"
      # and with the short SHA of the commit.
      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          cache-from: type=registry,ref=ghcr.io/itsdrike/web:latest
          cache-to: type=inline
          tags: |
            ghcr.io/itsdrike/web:latest
            ghcr.io/itsdrike/web:${{ steps.sha_tag.outputs.tag }}
          build-args: |
            git_sha=${{ github.sha }}

  deploy-portainer:
    runs-on: ubuntu-latest
    needs: [docker]
    env:
      WEBHOOK: ${{ secrets.PORTAINER_WEBHOOK }}
    if: (github.event_name == 'push' || github.event == 'workflow_dispatch') && github.ref == 'refs/heads/main'

    steps:
      - name: Trigger Portainer Webhook
        if: env.WEBHOOK != ''
        run: |
          response=$(curl -s -X POST -o /dev/null -w "%{http_code}" ${{ secrets.PORTAINER_WEBHOOK }})
          if [[ "$response" -lt 200 || "$response" -ge 300 ]]; then
            echo "Webhook trigger failed with response code $response"
            exit 1
          fi
