---
name: Build and Publish Most Docker Images

on:
  # run it on push to the default repository branch
  push:
  pull_request:
  schedule:
    - cron: '0 6 * * *'  # Daily 6AM UTC build

jobs:
  # define job to build and publish docker image
  build-and-push-docker-image:
    name: Build Docker image and push to repositories
    # run only when code is compiling and tests are passing
    runs-on: ubuntu-latest

    # steps to perform in job
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      # setup Docker buld action
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1

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

      - name: Build base image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_base"
          tags: |
            ghcr.io/jelmer/janitor/base:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build runner image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_runner"
          tags: |
            ghcr.io/jelmer/janitor/runner:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build archive image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_archive"
          tags: |
            ghcr.io/jelmer/janitor/archive:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build irc-notify image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_irc_notify"
          tags: |
            ghcr.io/jelmer/janitor/irc_notify:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build mastodon-notify image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_mastodon_notify"
          tags: |
            ghcr.io/jelmer/janitor/mastodon_notify:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build xmpp-notify image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_xmpp_notify"
          tags: |
            ghcr.io/jelmer/janitor/xmpp_notify:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build publish image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_publish"
          tags: |
            ghcr.io/jelmer/janitor/publish:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build vcs_store image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_vcs_store"
          tags: |
            ghcr.io/jelmer/janitor/vcs_store:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}

      - name: Build differ image
        uses: docker/build-push-action@v2
        with:
          file: "Dockerfile_differ"
          tags: |
            ghcr.io/jelmer/janitor/differ:latest
          # build on feature branches, push only on main branch
          push: ${{ github.ref == 'refs/heads/main' }}
