name: "update-iso-image-versions"
on:
  workflow_dispatch:
env:
  GOPROXY: https://proxy.golang.org
  GO_VERSION: '1.22.5'
permissions:
  contents: read
jobs:
  update-all:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
      - uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
        with:
          go-version: ${{env.GO_VERSION}}
      - name: Bump versions
        id: bumpVersions
        run: |
          OLD_BUILDKIT=$(DEP=buildkit make get-dependency-version)
          OLD_CNI_PLUGINS=$(DEP=cni-plugins make get-dependency-version)
          OLD_CONTAINERD=$(DEP=containerd make get-dependency-version)
          OLD_CRICTL=$(DEP=crictl make get-dependency-version)
          OLD_CRIO=$(DEP=cri-o make get-dependency-version)
          OLD_CRUN=$(DEP=crun make get-dependency-version)
          OLD_DOCKER=$(DEP=docker make get-dependency-version)
          OLD_DOCKER_BUILDX=$(DEP=docker-buildx make get-dependency-version)
          OLD_GO=$(DEP=go make get-dependency-version)
          OLD_NERDCTL=$(DEP=nerdctl make get-dependency-version)
          OLD_NERDCTLD=$(DEP=nerdctld make get-dependency-version)
          OLD_RUNC=$(DEP=runc make get-dependency-version)
          OLD_UBUNTU=$(DEP=ubuntu make get-dependency-version)
          make update-buildkit-version
          make update-cni-plugins-version
          make update-containerd-version
          # Uncomment after crictl/cri-o issue resolved: https://github.com/kubernetes/minikube/issues/18359
          # make update-cri-o-version
          # make update-crictl-version
          make update-crun-version
          make update-docker-version
          make update-docker-buildx-version
          make update-golang-version
          make update-nerdctl-version
          make update-nerdctld-version
          make update-runc-version
          make update-ubuntu-version
          NEW_BUILDKIT=$(DEP=buildkit make get-dependency-version)
          NEW_CNI_PLUGINS=$(DEP=cni-plugins make get-dependency-version)
          NEW_CONTAINERD=$(DEP=containerd make get-dependency-version)
          NEW_CRICTL=$(DEP=crictl make get-dependency-version)
          NEW_CRIO=$(DEP=cri-o make get-dependency-version)
          NEW_CRUN=$(DEP=crun make get-dependency-version)
          NEW_DOCKER=$(DEP=docker make get-dependency-version)
          NEW_DOCKER_BUILDX=$(DEP=docker-buildx make get-dependency-version)
          NEW_GO=$(DEP=go make get-dependency-version)
          NEW_NERDCTL=$(DEP=nerdctl make get-dependency-version)
          NEW_NERDCTLD=$(DEP=nerdctld make get-dependency-version)
          NEW_RUNC=$(DEP=runc make get-dependency-version)
          NEW_UBUNTU=$(DEP=ubuntu make get-dependency-version)
          echo "changelog<<EOF" >> "$GITHUB_OUTPUT"
          if [ "$OLD_BUILDKIT" != "$NEW_BUILDKIT" ]; then
            echo "### Update BuildKit from $OLD_BUILDKIT to $NEW_BUILDKIT" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/moby/buildkit/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_CNI_PLUGINS" != "$NEW_CNI_PLUGINS" ]; then
            echo "### Update CNI Plugins from $OLD_CNI_PLUGINS to $NEW_CNI_PLUGINS" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/containernetworking/plugins/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_CONTAINERD" != "$NEW_CONTAINERD" ]; then
            echo "### Update containerd from $OLD_CONTAINERD to $NEW_CONTAINERD" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/containerd/containerd/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_CRICTL" != "$NEW_CRICTL" ]; then
            echo "### Update crictl from $OLD_CRICTL to $NEW_CRICTL" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/kubernetes-sigs/cri-tools/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_CRIO" != "$NEW_CRIO" ]; then
            echo "### Update CRI-O from $OLD_CRIO to $NEW_CRIO" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/cri-o/cri-o/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_CRUN" != "$NEW_CRUN" ]; then
            echo "### Update crun from $OLD_CRUN to $NEW_CRUN" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/containers/crun/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_DOCKER" != "$NEW_DOCKER" ]; then
            echo "### Update Docker from $OLD_DOCKER to $NEW_DOCKER" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/moby/moby/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_DOCKER_BUILDX" != "$NEW_DOCKER_BUILDX" ]; then
            echo "### Update buildx from $OLD_DOCKER_BUILDX to $NEW_DOCKER_BUILDX" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/docker/buildx/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_GO" != "$NEW_GO" ]; then
            echo "### Update Go from $OLD_GO to $NEW_GO" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_NERDCTL" != "$NEW_NERDCTL" ]; then
            echo "### Update nerdctl from $OLD_NERDCTL to $NEW_NERDCTL" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/containerd/nerdctl/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_NERDCTLD" != "$NEW_NERDCTLD" ]; then
            echo "### Update nerdctld from $OLD_NERDCTLD to $NEW_NERDCTLD" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/afbjorklund/nerdctld/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_RUNC" != "$NEW_RUNC" ]; then
            echo "### Update runc from $OLD_RUNC to $NEW_RUNC" >> "$GITHUB_OUTPUT"
            echo "[Release notes](https://github.com/opencontainers/runc/releases)" >> "$GITHUB_OUTPUT"
          fi
          if [ "$OLD_UBUNTU" != "$NEW_UBUNTU" ]; then
            echo "### Update Ubuntu from $OLD_UBUNTU to $NEW_UBUNTU" >> "$GITHUB_OUTPUT"
          fi
          echo "EOF" >> "$GITHUB_OUTPUT"
          # The following is to support multiline with GITHUB_OUTPUT, see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
          echo "changes<<EOF" >> "$GITHUB_OUTPUT"
          echo "$(git status --porcelain)" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"
      - name: Create PR
        id: createPR
        if: ${{ steps.bumpVersions.outputs.changes != '' }}
        uses: peter-evans/create-pull-request@4320041ed380b20e97d388d56a7fb4f9b8c20e79
        with:
          token: ${{ secrets.MINIKUBE_BOT_PAT }}
          commit-message: 'Kicbase/ISO: Update dependency versions'
          committer: minikube-bot <minikube-bot@google.com>
          author: minikube-bot <minikube-bot@google.com>
          branch: bump_iso_image_versions
          branch-suffix: short-commit-hash
          push-to-fork: minikube-bot/minikube
          base: master
          delete-branch: true
          title: 'Kicbase/ISO: Update dependency versions'
          body: |
            Changelog:
            ${{ steps.bumpVersions.outputs.changelog }}
      - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
        if: ${{ steps.bumpVersions.outputs.changes != '' }}
        with:
          github-token: ${{ secrets.MINIKUBE_BOT_PAT }}
          script: |
            github.rest.issues.createComment({
              issue_number: ${{ steps.createPR.outputs.pull-request-number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'ok-to-build-image'
            })
      - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
        if: ${{ steps.bumpVersions.outputs.changes != '' }}
        with:
          github-token: ${{ secrets.MINIKUBE_BOT_PAT }}
          script: |
            github.rest.issues.createComment({
              issue_number: ${{ steps.createPR.outputs.pull-request-number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'ok-to-build-iso'
            })
