From 85d2b377f2c6bfe8200d57c86c70f5cde35a6f97 Mon Sep 17 00:00:00 2001
From: James Sansbury <james@lullabot.com>
Date: Wed, 11 Oct 2023 13:45:00 -0400
Subject: [PATCH] initial working

---
 .github/workflows/build.yml | 63 +++++++++++++++++++++++++++
 Dockerfile                  |  8 ++++
 README.md                   |  2 +
 Taskfile.yml                | 85 +++++++++++++++++++++++++++++++++++++
 4 files changed, 158 insertions(+)
 create mode 100644 .github/workflows/build.yml
 create mode 100644 Dockerfile
 create mode 100644 README.md
 create mode 100644 Taskfile.yml

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..c3dc09d
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,63 @@
+name: build
+
+on:
+  pull_request:
+    types: [opened, reopened, synchronize]
+  push:
+    branches:
+      - main
+  workflow_dispatch:
+    inputs:
+      push:
+        description: Push to Docker hub after build is complete.
+        default: true
+        type: boolean
+      services:
+        description: Space delimited string of services to build. If empty, all will be built.
+        type: string
+  schedule:
+    - cron: '0 0 * * 6'
+
+env:
+  TASK_VERSION: v3.29.1
+  TASK_CHECKSUM: e411770abf73d5e094100ab7a1c8278f35b591ecadbfd778200b6b2ad1ee340b
+  # If this changes, also update the if on the docs job. We unfortunately can't
+  # use env.PUSH in the if there.
+  PUSH: ${{ (inputs.push || github.event_name == 'schedule' || github.event_name == 'push') && 'true' || 'false' }}
+
+jobs:
+  build:
+    name: Build
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+      - name: Install dependencies
+        run: sudo apt-get update
+          && sudo apt-get install -y eatmydata
+          && sudo ln -snf /usr/bin/eatmydata /usr/local/bin/apt-get
+          && sudo apt-get install -y curl
+          && curl -L -o /tmp/task_linux_amd64.deb https://github.com/go-task/task/releases/download/${{ env.TASK_VERSION }}/task_linux_amd64.deb
+          && sha256sum /tmp/task_linux_amd64.deb | grep -q ${{ env.TASK_CHECKSUM }}
+          && sudo dpkg -i /tmp/task_linux_amd64.deb
+          && sudo apt-get clean
+          && sudo rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
+      -
+        # Add support for more platforms with QEMU (optional)
+        # https://github.com/docker/setup-qemu-action
+        name: Set up QEMU
+        uses: docker/setup-qemu-action@v2
+      -
+        name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v2
+      - name: Login to Docker Hub
+        uses: docker/login-action@v2
+        with:
+          username: ${{ secrets.DOCKERHUB_USERNAME }}
+          password: ${{ secrets.DOCKERHUB_TOKEN }}
+      -
+        # Expose the actions cache url and token.
+        # https://github.com/tonistiigi/go-actions-cache/blob/master/api.md#authentication
+        name: Expose GitHub Runtime
+        uses: crazy-max/ghaction-github-runtime@v2
+      - run: task
+        id: task
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..0c2361d
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,8 @@
+ARG FROM_TAG
+
+FROM debian:$FROM_TAG
+
+RUN \
+    sed -e 's/^Components: main$/& contrib/g' -i /etc/apt/sources.list.d/debian.sources && \
+    apt-get update && \
+    apt-get install --yes --no-install-recommends zfsutils-linux
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6a03352
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+This is a very simple docker image starting from Debian slim with just
+zfsutils-linux package installed.
\ No newline at end of file
diff --git a/Taskfile.yml b/Taskfile.yml
new file mode 100644
index 0000000..dc604f3
--- /dev/null
+++ b/Taskfile.yml
@@ -0,0 +1,85 @@
+# yaml-language-server: $schema=https://taskfile.dev/schema.json
+version: 3
+
+dotenv:
+  - .env.local
+  - .local.env
+  - .env
+
+env:
+  NAMESPACE: tugboat
+  IMAGE_NAME: zfs-utils
+  # Separate each platform with a comma.
+  PLATFORMS: linux/amd64,linux/arm64
+  # Separate each tag with a comma.
+  TAGS: latest,bookworm-slim
+  FROM_TAG: bookworm-slim
+
+vars:
+  BUILDX_BUILDER:
+    sh: echo "{{.NAMESPACE}}-{{.IMAGE_NAME}}"
+  IMAGE_TAGS:
+    sh: echo "{{.TAGS}}" | sed -E 's@(^|,)([^,]+)@ --tag {{.NAMESPACE}}/{{.IMAGE_NAME}}:\2@g'
+
+tasks:
+  default:
+    desc: 'Builds the image. Set PUSH=1 to push after building.'
+    cmds:
+      - task: create-builder
+      - task: build
+
+  create-builder:
+    desc: '[subtask] Create the Docker buildx builder.'
+    status:
+      # Exits zero (up to date) if there is already a builder.
+      - docker buildx ls | grep -q '^{{.BUILDX_BUILDER}}\s'
+    cmds:
+      - docker buildx create
+          --name "{{.BUILDX_BUILDER}}"
+          --platform "{{.PLATFORMS}}"
+          --bootstrap
+          --use
+
+  build:
+    desc: '[subtask] Build any docker image tarballs generated from bake for Tugboat usage.'
+    vars:
+      PUSH_OPT:
+        sh: (test "$PUSH" != "true" && test "$PUSH" != "1") || echo "--push"
+    cmds:
+      - docker buildx use "{{.BUILDX_BUILDER}}"
+      - echo 'PUSH_OPT {{.PUSH_OPT}}'
+      - docker buildx build
+          --build-arg FROM_TAG={{.FROM_TAG}}
+          --platform "{{.PLATFORMS}}"
+          {{.IMAGE_TAGS}} {{.PUSH_OPT}} .
+
+  clean:
+    desc: 'Removes all generated files and docker images.'
+    prompt: This will remove any generated files and delete all docker images in
+      the {{.NAMESPACE}}/* namespace. Do you want to continue?
+    deps:
+      - rm-images
+
+  clean-all:
+    desc: 'Removes the buildkit builder, all generated files, and all docker images.'
+    deps:
+      - rm-builder
+      - rm-images
+    prompt: This will remove the buildkit builder, all generated files, and
+      delete all docker images in the {{.NAMESPACE}}/* namespace. Do you
+      want to continue?
+
+  rm-builder:
+    internal: true
+    status:
+      - if docker buildx ls | grep -q '^{{.BUILDX_BUILDER}}\s'; then exit 1; fi
+    cmd: docker buildx rm -f '{{.BUILDX_BUILDER}}'
+  rm-images:
+    internal: true
+    status:
+      - test -z "{{.IMAGES}}"
+    vars:
+      IMAGES:
+        sh: docker images ls --filter=reference="{{.NAMESPACE}}/{{.IMAGE_NAME}}" -q | sort | uniq
+    cmd: docker rmi --force $(echo "{{.IMAGES}}") || true
+