|
| 1 | +name: Docker Build and Push (Slim) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + release: |
| 9 | + types: [published] |
| 10 | + |
| 11 | +env: |
| 12 | + REGISTRY_DOCKERHUB: docker.io |
| 13 | + REGISTRY_GHCR: ghcr.io |
| 14 | + IMAGE_NAME: ${{ github.repository }} |
| 15 | + DOCKERHUB_NAMESPACE: ${{ secrets.DOCKERHUB_USERNAME != '' && secrets.DOCKERHUB_USERNAME || github.repository_owner }} |
| 16 | + |
| 17 | +jobs: |
| 18 | + build-and-push-slim: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + packages: write |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Determine build settings |
| 28 | + id: build-config |
| 29 | + env: |
| 30 | + EVENT_NAME: ${{ github.event_name }} |
| 31 | + EVENT_ACTION: ${{ github.event.action }} |
| 32 | + run: | |
| 33 | + if [ "$EVENT_NAME" = "release" ] && [ "$EVENT_ACTION" = "published" ]; then |
| 34 | + echo "push=true" >> "$GITHUB_OUTPUT" |
| 35 | + echo "platforms=linux/amd64,linux/arm64" >> "$GITHUB_OUTPUT" |
| 36 | + echo "load=false" >> "$GITHUB_OUTPUT" |
| 37 | + else |
| 38 | + echo "push=false" >> "$GITHUB_OUTPUT" |
| 39 | + echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT" |
| 40 | + echo "load=true" >> "$GITHUB_OUTPUT" |
| 41 | + fi |
| 42 | +
|
| 43 | + - name: Derive image version |
| 44 | + id: version |
| 45 | + env: |
| 46 | + EVENT_NAME: ${{ github.event_name }} |
| 47 | + TAG_NAME: ${{ github.event.release.tag_name }} |
| 48 | + REF_NAME: ${{ github.ref_name }} |
| 49 | + GITHUB_SHA: ${{ github.sha }} |
| 50 | + run: | |
| 51 | + version="" |
| 52 | + if [ "$EVENT_NAME" = "release" ] && [ -n "$TAG_NAME" ]; then |
| 53 | + version="$TAG_NAME" |
| 54 | + elif [ -n "$REF_NAME" ]; then |
| 55 | + version="$REF_NAME" |
| 56 | + fi |
| 57 | + version="${version##*/}" |
| 58 | + if [ "${version#v}" != "$version" ]; then |
| 59 | + version="${version#v}" |
| 60 | + fi |
| 61 | + if [ -z "$version" ]; then |
| 62 | + version="${GITHUB_SHA:0:12}" |
| 63 | + fi |
| 64 | + if ! echo "$version" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+'; then |
| 65 | + safe_branch=$(printf %s "$version" | tr -c 'A-Za-z0-9' '-') |
| 66 | + safe_branch=${safe_branch%-} |
| 67 | + if [ -z "$safe_branch" ]; then |
| 68 | + safe_branch="sha-${GITHUB_SHA:0:12}" |
| 69 | + fi |
| 70 | + version="0.0.0+${safe_branch}" |
| 71 | + fi |
| 72 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 73 | +
|
| 74 | + - name: Set up QEMU |
| 75 | + if: steps.build-config.outputs.platforms == 'linux/amd64,linux/arm64' |
| 76 | + uses: docker/setup-qemu-action@v3 |
| 77 | + |
| 78 | + - name: Set up Docker Buildx |
| 79 | + uses: docker/setup-buildx-action@v3 |
| 80 | + with: |
| 81 | + driver: docker-container |
| 82 | + |
| 83 | + - name: Login to Docker Hub |
| 84 | + if: steps.build-config.outputs.push == 'true' |
| 85 | + uses: docker/login-action@v3 |
| 86 | + with: |
| 87 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 88 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 89 | + |
| 90 | + - name: Login to GitHub Container Registry |
| 91 | + if: steps.build-config.outputs.push == 'true' |
| 92 | + uses: docker/login-action@v3 |
| 93 | + with: |
| 94 | + registry: ${{ env.REGISTRY_GHCR }} |
| 95 | + username: ${{ github.actor }} |
| 96 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 97 | + |
| 98 | + - name: Extract metadata |
| 99 | + id: meta |
| 100 | + if: steps.build-config.outputs.push == 'true' |
| 101 | + uses: docker/metadata-action@v5 |
| 102 | + with: |
| 103 | + images: | |
| 104 | + ${{ env.DOCKERHUB_NAMESPACE }}/ttsfm |
| 105 | + ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME }} |
| 106 | + tags: | |
| 107 | + type=semver,pattern=v{{version}},suffix=-slim |
| 108 | + type=semver,pattern=v{{major}}.{{minor}},suffix=-slim,enable=${{ !contains(github.ref, 'alpha') && !contains(github.ref, 'beta') }} |
| 109 | + labels: | |
| 110 | + org.opencontainers.image.source=${{ github.repositoryUrl }} |
| 111 | + org.opencontainers.image.description=Free TTS API server compatible with OpenAI's TTS API format using openai.fm (slim variant without ffmpeg) |
| 112 | + org.opencontainers.image.licenses=MIT |
| 113 | + org.opencontainers.image.title=TTSFM - Free TTS API Server (Slim) |
| 114 | + org.opencontainers.image.vendor=dbcccc |
| 115 | +
|
| 116 | + - name: Set local image metadata |
| 117 | + id: meta-local |
| 118 | + if: steps.build-config.outputs.push != 'true' |
| 119 | + run: | |
| 120 | + echo "tags=${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME }}:ci-${GITHUB_RUN_ID}-slim" >> "$GITHUB_OUTPUT" |
| 121 | + echo "labels=org.opencontainers.image.source=${{ github.repositoryUrl }}" >> "$GITHUB_OUTPUT" |
| 122 | +
|
| 123 | + - name: Build and push image |
| 124 | + id: build-and-push |
| 125 | + uses: docker/build-push-action@v5 |
| 126 | + with: |
| 127 | + context: . |
| 128 | + platforms: ${{ steps.build-config.outputs.platforms }} |
| 129 | + push: ${{ steps.build-config.outputs.push == 'true' }} |
| 130 | + load: ${{ steps.build-config.outputs.load == 'true' }} |
| 131 | + tags: ${{ steps.meta.outputs.tags || steps.meta-local.outputs.tags }} |
| 132 | + labels: ${{ steps.meta.outputs.labels || steps.meta-local.outputs.labels }} |
| 133 | + cache-from: type=gha,scope=slim |
| 134 | + cache-to: type=gha,mode=max,scope=slim |
| 135 | + build-args: | |
| 136 | + VERSION=${{ steps.version.outputs.version }} |
| 137 | + VARIANT=slim |
| 138 | +
|
| 139 | + - name: Smoke test image |
| 140 | + if: steps.build-config.outputs.load == 'true' |
| 141 | + run: | |
| 142 | + set -euo pipefail |
| 143 | + IMAGE="${{ steps.meta-local.outputs.tags }}" |
| 144 | + echo "Running smoke test for slim image: $IMAGE" |
| 145 | + docker rm -f ttsfm-smoke-slim >/dev/null 2>&1 || true |
| 146 | + docker run -d --name ttsfm-smoke-slim -p 127.0.0.1:8001:8000 "$IMAGE" |
| 147 | + success="" |
| 148 | + for attempt in $(seq 1 10); do |
| 149 | + if curl --fail --silent --max-time 5 http://127.0.0.1:8001/api/health > /tmp/ttsfm-health-slim.json; then |
| 150 | + success="yes" |
| 151 | + cat /tmp/ttsfm-health-slim.json |
| 152 | + break |
| 153 | + fi |
| 154 | + sleep 3 |
| 155 | + done |
| 156 | + docker logs ttsfm-smoke-slim || true |
| 157 | + docker rm -f ttsfm-smoke-slim >/dev/null 2>&1 || true |
| 158 | + if [ -z "$success" ]; then |
| 159 | + echo "Container health check failed" >&2 |
| 160 | + exit 1 |
| 161 | + fi |
| 162 | +
|
| 163 | + - name: Show image info |
| 164 | + run: | |
| 165 | + echo "Variant: slim" |
| 166 | + echo "Push enabled: ${{ steps.build-config.outputs.push }}" |
| 167 | + echo "Image tags: ${{ steps.meta.outputs.tags || steps.meta-local.outputs.tags }}" |
| 168 | + echo "Image digest: ${{ steps.build-and-push.outputs.digest }}" |
| 169 | +
|
0 commit comments