# DSpace Docker image build for hub.docker.com
name: Docker images

# Run this Build for all pushes to 'main' or maintenance branches, or tagged releases.
# Also run for PRs to ensure PR doesn't break Docker build process
on:
  push:
    branches:
      - main
      - 'dspace-**'
    tags:
      - 'dspace-**'
  pull_request:

jobs:
  docker:
    # Ensure this job never runs on forked repos. It's only executed for 'dspace/dspace-angular'
    if: github.repository == 'dspace/dspace-angular'
    runs-on: ubuntu-latest
    env:
      # Define tags to use for Docker images based on Git tags/branches (for docker/metadata-action)
      # For a new commit on default branch (main), use the literal tag 'dspace-7_x' on Docker image.
      # For a new commit on other branches, use the branch name as the tag for Docker image.
      # For a new tag, copy that tag name as the tag for Docker image.
      IMAGE_TAGS: |
        type=raw,value=dspace-7_x,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }}
        type=ref,event=branch,enable=${{ !endsWith(github.ref, github.event.repository.default_branch) }}
        type=ref,event=tag
      # Define default tag "flavor" for docker/metadata-action per
      # https://github.com/docker/metadata-action#flavor-input
      # We turn off 'latest' tag by default.
      TAGS_FLAVOR: |
        latest=false

    steps:
      # https://github.com/actions/checkout
      - name: Checkout codebase
        uses: actions/checkout@v2

      # https://github.com/docker/setup-buildx-action
      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v1

      # https://github.com/docker/login-action
      - name: Login to DockerHub
        # Only login if not a PR, as PRs only trigger a Docker build and not a push
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

      ###############################################
      # Build/Push the 'dspace/dspace-angular' image
      ###############################################
      # https://github.com/docker/metadata-action
      # Get Metadata for docker_build step below
      - name: Sync metadata (tags, labels) from GitHub to Docker for 'dspace-angular' image
        id: meta_build
        uses: docker/metadata-action@v3
        with:
          images: dspace/dspace-angular
          tags: ${{ env.IMAGE_TAGS }}
          flavor: ${{ env.TAGS_FLAVOR }}

      # https://github.com/docker/build-push-action
      - name: Build and push 'dspace-angular' image
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          # For pull requests, we run the Docker build (to ensure no PR changes break the build),
          # but we ONLY do an image push to DockerHub if it's NOT a PR
          push: ${{ github.event_name != 'pull_request' }}
          # Use tags / labels provided by 'docker/metadata-action' above
          tags: ${{ steps.meta_build.outputs.tags }}
          labels: ${{ steps.meta_build.outputs.labels }}
