<%= "# 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.tree_deps.to_a.flatten.uniq.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

      - 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
      - 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"
      - 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 %>

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

      - name: Use X64 cache from tree pipeline of ${{ matrix.platform }}
        uses: actions/cache/restore@v4
        with:
          path: tmp/build-cache-X64
          key: ${{ runner.os }}-on-X64-${{ matrix.platform }}-${{ github.sha }}
          restore-keys: ${{ runner.os }}-on-X64-${{ matrix.platform }}
          enableCrossOsArchive: true
      - name: Use ARM64 cache from tree pipeline of ${{ matrix.platform }}
        uses: actions/cache/restore@v4
        with:
          path: tmp/build-cache-ARM64
          key: ${{ runner.os }}-on-ARM64-${{ matrix.platform }}-${{ github.sha }}
          restore-keys: ${{ runner.os }}-on-ARM64-${{ matrix.platform }}
          enableCrossOsArchive: true
          fail-on-cache-miss: true

      - 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: Use cache and push docker image
        env:
          RCD_IMAGE_VERSION: snapshot
          RCD_DOCKER_BUILD: docker buildx build --cache-from=type=local,compression=zstd,src=tmp/build-cache-X64 --cache-from=type=local,compression=zstd,src=tmp/build-cache-ARM64
          RCD_TASK_DEPENDENCIES: "false"
        run: |
          docker buildx create --driver docker-container --use
          bundle exec rake release:${{ matrix.platform }}
