
name: Build CPSign prediction service

on:
  workflow_dispatch:
  push:
    branches:
      - 'main'
      - 'master'

permissions:
  contents: read
  packages: write

jobs:

  build-images:
    runs-on: ubuntu-latest

    env:
      REGISTRY: ghcr.io
      DOCKER_FILE: [TODO: path to your Dockerfile from repo root]
      working-directory: [TODO: YOUR BUILD DIRECTORY]

    steps:
    - uses: actions/checkout@v4

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3

    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@v3

    - name: Login to GHCR
      #if: github.event_name != 'pull_request'
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Docker meta
      id: meta_service
      uses: docker/metadata-action@v3
      with:
        # list of Docker images to use as base name for tags
        images: |
          ghcr.io/[TODO: owner or organization]/[TODO: repo name or intended image name]
        flavor: |
          latest=auto
        # generate Docker tags based on the following events/attributes
        tags: |
          type=raw,value=latest,enable={{is_default_branch}}
          type=schedule
          type=semver,pattern={{version}}
          type=semver,pattern={{major}}.{{minor}}
          type=semver,pattern={{major}}
          type=sha

    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        context: ${{ env.working-directory }}
        platforms: linux/amd64,linux/arm64
        file: ${{ env.DOCKER_FILE }}
        push: ${{ github.event_name != 'pull_request' }}
        tags: ${{ steps.meta_service.outputs.tags }}
        labels: ${{ steps.meta_service.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max