# Workflow to update docker image for pwa.kiwix.org (since docker autobuild has been disabled)

name: Docker

# Controls when the action will run. 
on:
  # Triggers the workflow when a release is publsihed or updated
  release:
    types: [ published ]

  # Call this for testing from the REST API with '{"version":"{tag}"}'
  # 'https://api.github.com/repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches'
  workflow_dispatch:
    inputs:
      version:
        description: Specific version to build (overrides on-master and tag-pattern), e.g. 'v2.0.0'
        required: false
        default: ''

jobs:
  build-and-push:
    name: Deploy Docker Image
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Update app version number
        env:
          INPUT_VERSION: ${{ github.event.inputs.version }}
        run: |
          chmod +x ./scripts/rewrite_app_version_number.sh
          ./scripts/rewrite_app_version_number.sh
      - name: Build and push
        uses: openzim/docker-publish-action@v8
        with:
          image-name: kiwix/kiwix-pwa
          credentials: |
            GHCRIO_USERNAME=${{ secrets.GHCR_USERNAME }}
            GHCRIO_TOKEN=${{ secrets.GHCR_TOKEN }}
          tag-pattern: /^v?([0-9.]+)$/
          latest-on-tag: true
          dockerfile: Dockerfile.pwa
          restrict-to: kiwix/kiwix-js-windows
          registries: ghcr.io
          manual-tag: ${{ github.event.inputs.version }}
      - name: Restart live webapp
        uses: actions-hub/kubectl@master
        env:
          KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
        with:
          args: rollout restart deployments pwa-deployment -n pwa
