<%= "# This file is generated. Please edit publish-images.yml.erb instead!" %>

<% if release %>
name: Release docker images to GHCR
#
#  This workflow assumes the maintainer has chosen the appropriate tag in the workflow dispatch UI.
#
on:
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag name to release"
        required: true

<% else %>
name: Weekly publish docker images to GHCR
on:
  workflow_dispatch:
  schedule:
    - cron: "0 3 * * 3" # At 03:00 on Wednesday # https://crontab.guru/#0_3_*_*_3
<% end %>

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

jobs:
<%  parallel_docker_build.all_deps.each do |tree_dep|
      dockerfile = parallel_docker_build.final_deps.invert[tree_dep]
      job = dockerfile ? dockerfile[/[-\w]+$/] : tree_dep
%>
  <%= job %>:
    name: "build <%= job %> ${{ matrix.os }}"
    <%= need = parallel_docker_build.tree_deps[tree_dep]; "needs: #{need}" if need %>
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - ubuntu-24.04-arm
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
<% if release %>
        with:
          ref: ${{ inputs.tag }}
<% end %>
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.4"
          bundler-cache: true
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{github.actor}}
          password: ${{secrets.GITHUB_TOKEN}}

      - name: Fetch docker buildx layer cache
        uses: actions/cache@v4
        with:
          path: tmp/build-cache-${{ runner.arch }}
          key: ${{ runner.os }}-on-${{ runner.arch }}-<%= job %>-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-on-${{ runner.arch }}-<%= job %>
            ${{ runner.os }}-on-${{ runner.arch }}-<%= need %>
          enableCrossOsArchive: true

<%
# push the final per-platform images in order to merge them in the job below
if dockerfile && (plat = dockerfile[/[-\w]+$/])
  version = {version: "snapshot"} unless release
  tag = RakeCompilerDock::Starter.container_image_name(platform: plat, **version)
  push_tag = "-t #{tag}-${{ runner.arch }} --push"
end
%>
      - name: Build the image layers <%= job %> on ${{ runner.arch }}
        env:
          RCD_TASK_DEPENDENCIES: "false"
        run: |
          # Change docker to a cache-able driver
          docker buildx create --driver docker-container --use
          bundle exec rake build:<%= tree_dep %> RCD_DOCKER_BUILD="docker buildx build --cache-from=type=local,compression=zstd,src=tmp/build-cache-${{ runner.arch }} --cache-to=type=local,compression=zstd,dest=tmp/build-cache-new <%= push_tag %>"
      - name: Update and prune docker buildx layer cache
        run: |
          rm -rf tmp/build-cache-${{ runner.arch }}
          mv tmp/build-cache-new tmp/build-cache-${{ runner.arch }}
<% end %>

  merge:
    name: merge
    needs: [<%= parallel_docker_build.final_deps.keys.map{|a| a[/[-\w]+$/] }.join(",") %>]
    strategy:
      fail-fast: false
    runs-on: ubuntu-24.04-arm
    steps:
      - uses: actions/checkout@v4

      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.4"
          bundler-cache: true
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{github.actor}}
          password: ${{secrets.GITHUB_TOKEN}}

      - name: Merge pushed images and build a manifest for both host platform
<% unless release %>
        env:
          RCD_IMAGE_VERSION: snapshot
<% end %>
        run: |
          bundle exec rake release:manifests
