# source  latest
# result  moby/buildkit:latest          > moby/buildkit:buildx-stable-1
#         moby/buildkit:rootless        > moby/buildkit:buildx-stable-1-rootless
#         moby/buildkit:ubuntu-latest   > moby/buildkit:buildx-stable-1-gpu
#
# source  v0.8.1
# result  moby/buildkit:v0.8.1          > moby/buildkit:buildx-stable-1
#         moby/buildkit:v0.8.1-rootless > moby/buildkit:buildx-stable-1-rootless
#         moby/buildkit:v0.8.1-ubuntu   > moby/buildkit:buildx-stable-1-gpu
name: buildx-image

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

# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions
permissions:
  contents: read

on:
  workflow_dispatch:
    inputs:
      source-tag:
        description: 'BuildKit source Docker tag'
        required: true
        default: 'latest'
        type: string
      dry-run:
        description: 'Dry run'
        required: false
        default: true
        type: boolean

env:
  SETUP_BUILDX_VERSION: "edge"
  REPO_SLUG_TARGET: "moby/buildkit"

jobs:
  create:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        tag:
          - 'buildx-stable-1'
        flavor:
          - ''
          - 'rootless'
          - 'ubuntu'
    steps:
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: ${{ env.SETUP_BUILDX_VERSION }}
          buildkitd-flags: --debug
      -
        name: Login to DockerHub
        if: github.event.inputs.dry-run != 'true'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Create
        uses: actions/github-script@v8
        env:
          INPUT_SOURCE-TAG: ${{ inputs.source-tag }}
          INPUT_DRY-RUN: ${{ inputs.dry-run }}
          INPUT_DEST-TAG: ${{ matrix.tag }}
          INPUT_FLAVOR: ${{ matrix.flavor }}
        with:
          script: |
            const inpSourceTag = core.getInput('source-tag');
            const inpDryRun = core.getBooleanInput('dry-run');
            const inpDestTag = core.getInput('dest-tag');
            const inpFlavor = core.getInput('flavor');
            const createArgs = ['buildx', 'imagetools', 'create'];
            if (inpDryRun) {
              createArgs.push('--dry-run');
            }
            let sourceTag = inpSourceTag;
            let destTag = inpDestTag;
            if (inpFlavor !== '') {
              if (inpFlavor === 'ubuntu') {
                sourceTag = `${inpSourceTag}-${inpFlavor}`;
                destTag = `${inpDestTag}-gpu`;
              } else {
                sourceTag = inpSourceTag === 'latest' ? inpFlavor : `${inpSourceTag}-${inpFlavor}`;
                destTag = `${inpDestTag}-${inpFlavor}`;
              }
            }
            createArgs.push('--tag', `moby/buildkit:${destTag}`, `moby/buildkit:${sourceTag}`);
            await exec.getExecOutput('docker', createArgs, {
              ignoreReturnCode: true
            }).then(res => {
              if (res.stderr.length > 0 && res.exitCode != 0) {
                throw new Error(res.stderr);
              }
            });
