Files
ebook2audiobook/.github/workflows/Docker-Build.yml
2025-12-25 17:32:42 -05:00

174 lines
6.5 KiB
YAML

name: Docker Build
on:
workflow_dispatch:
inputs:
wipeAndReinstall:
type: boolean
description: 'Wipe & Re-Install E2A'
workflow_run:
workflows: ["E2A Test"]
types:
- completed
branches:
- main
jobs:
DockerBuild:
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
runs-on: [self-hosted, macos]
steps:
- name: Wipe & Re-Clone E2A E2ADockerBuild
if: ${{ inputs.wipeAndReinstall }}
run: rm -rf ~/E2ADockerBuild/ebook2audiobook
- name: Clone ebook2audiobook
shell: bash
run: |
set -e
REPO_DIR=~/E2ADockerBuild/ebook2audiobook
REPO_URL="https://github.com/${{ github.repository }}"
IS_PR="${{ github.event_name == 'pull_request' }}"
BASE_REF="${{ github.event.pull_request.base.ref }}"
HEAD_REF="${{ github.event.pull_request.head.ref }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
TRIGGER_SHA="${{ github.sha }}"
FRESH_CLONE=0
echo "==> Event: ${{ github.event_name }}"
echo "==> Repo: $REPO_URL"
# Clone or reuse
if [ -d "$REPO_DIR" ]; then
echo "==> Reusing existing repo"
cd "$REPO_DIR"
# Set correct remote and fix ambiguous refs
git remote set-url origin "$REPO_URL"
git remote set-head origin -a
git remote prune origin
git fetch --all --prune
echo "==> Cleaning working directory"
git reset --hard
else
echo "==> Cloning fresh"
git clone "$REPO_URL" "$REPO_DIR"
cd "$REPO_DIR"
git remote set-head origin -a
git remote prune origin
git fetch --all --prune
FRESH_CLONE=1
fi
if [ "$IS_PR" = "true" ]; then
echo "==> PR detected: simulating GitHub merge (base: $BASE_REF ← head: $HEAD_REF)"
# Fetch both branches
git fetch origin "$BASE_REF":"origin/$BASE_REF"
git fetch origin "$HEAD_REF":"origin/$HEAD_REF"
# Reset to base branch
git checkout -B "$BASE_REF" "remotes/origin/$BASE_REF"
git reset --hard "origin/$BASE_REF"
# Merge PR source
if ! git merge --no-ff --no-edit "origin/$HEAD_REF"; then
echo "❌ Merge conflict simulating PR merge"
exit 1
fi
else
echo "==> Not a PR: checking out triggered commit directly"
git fetch origin "$TRIGGER_SHA"
git checkout --detach "$TRIGGER_SHA"
git reset --hard "$TRIGGER_SHA"
fi
echo "==> Final repo state:"
git status
git log -1 --oneline
if [ "$FRESH_CLONE" -eq 1 ]; then
echo "==> Running ./ebook2audiobook.sh --help because this was a fresh clone"
if ! ./ebook2audiobook.sh --help; then
echo "==> Attempting fallback with conda deactivation"
source "$(conda info --base 2>/dev/null)/etc/profile.d/conda.sh" 2>/dev/null && conda deactivate || true
./ebook2audiobook.sh --help
fi
else
echo "==> Skipping script run because repo already existed"
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver: docker-container
buildkitd-flags: "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host"
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Get Git Commit Hash
run: echo "GIT_HASH=${GITHUB_SHA::9}" >> $GITHUB_ENV
- name: Get Latest Release Tag
run: |
TAG=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name)
if [ -z "$TAG" ] || [ "$TAG" == "null" ]; then TAG="latest"; fi
echo "RELEASE_TAG=$TAG" >> $GITHUB_ENV
- name: Debug Print Variables
run: |
echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}"
echo "GIT_HASH=${GIT_HASH}"
echo "RELEASE_TAG=${RELEASE_TAG}"
# ==================== CPU Build (linux/amd64 + linux/arm64) ====================
- name: Build and Push CPU Docker Image
run: |
cd ~/E2ADockerBuild/ebook2audiobook
CPU_TAG_1="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:${GIT_HASH}-cpu"
CPU_TAG_2="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:${RELEASE_TAG}-cpu"
CPU_TAG_3="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:cpu"
docker buildx build --pull --platform linux/amd64,linux/arm64 \
--build-arg DEVICE_TAG=cpu \
--build-arg DOCKER_DEVICE_STR='{"name": "cpu", "os": "manylinux_2_28", "arch": "x86_64", "pyvenv": [3, 12], "tag": "cpu", "note": "CPU only"}' \
-t "$CPU_TAG_1" \
-t "$CPU_TAG_2" \
-t "$CPU_TAG_3" \
--push .
# ==================== CUDA 12.8 Build (linux/amd64 only) ====================
- name: Build and Push CUDA 12.8 Docker Image
run: |
cd ~/E2ADockerBuild/ebook2audiobook
CUDA_TAG_1="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:${GIT_HASH}-cu128"
CUDA_TAG_2="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:${RELEASE_TAG}-cu128"
CUDA_TAG_3="${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:cu128"
docker buildx build --pull --platform linux/amd64 \
--build-arg DEVICE_TAG=cu128 \
--build-arg DOCKER_DEVICE_STR='{"name": "cuda", "os": "manylinux_2_28", "arch": "x86_64", "pyvenv": [3, 12], "tag": "cu128", "note": "CUDA 12.8"}' \
-t "$CUDA_TAG_1" \
-t "$CUDA_TAG_2" \
-t "$CUDA_TAG_3" \
--push .
- name: Build and Push Huggingface Docker Image
run: |
cd ~/E2ADockerBuild/ebook2audiobook
docker buildx build --platform linux/amd64 \
-f dockerfiles/HuggingfaceDockerfile \
-t ${{ secrets.DOCKER_USERNAME }}/ebook2audiobook:HuggingfaceSpace \
--push .
- name: Prune docker & buildx
run: |
docker system prune -af --volumes
docker buildx prune -af