|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Build Docker image for Tolgee CLI |
| 4 | +# Usage: |
| 5 | +# ./scripts/build-docker.sh [tag] [platform] [push] |
| 6 | +# |
| 7 | +# Examples: |
| 8 | +# ./scripts/build-docker.sh # Build for current platform with default tag |
| 9 | +# ./scripts/build-docker.sh latest # Build for current platform with specific tag |
| 10 | +# ./scripts/build-docker.sh latest linux/arm64 # Build for specific single platform (available locally) |
| 11 | +# ./scripts/build-docker.sh latest linux/amd64,linux/arm64 # Multi-platform build (NOT available locally) |
| 12 | +# ./scripts/build-docker.sh latest linux/amd64,linux/arm64 push # Multi-platform build and push |
| 13 | +# |
| 14 | +# Environment variables for push: |
| 15 | +# DOCKERHUB_USERNAME - Docker Hub username |
| 16 | +# DOCKERHUB_TOKEN - Docker Hub token/password |
| 17 | +# VERSION - Version for additional tagging (optional) |
| 18 | +# |
| 19 | +# Note: Multi-platform builds are stored in buildx cache and not available locally. |
| 20 | +# To run the image locally after a multi-platform build, either: |
| 21 | +# 1. Build for your current platform only (e.g., linux/arm64 on Apple Silicon) |
| 22 | +# 2. Push the multi-platform image to a registry and pull it |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +# Default values |
| 27 | +TAG=${1:-"dev"} |
| 28 | +PLATFORM=${2:-""} |
| 29 | +PUSH=${3:-""} |
| 30 | +IMAGE_NAME="tolgee/cli" |
| 31 | + |
| 32 | +# Colors for output |
| 33 | +RED='\033[0;31m' |
| 34 | +GREEN='\033[0;32m' |
| 35 | +YELLOW='\033[1;33m' |
| 36 | +NC='\033[0m' # No Color |
| 37 | + |
| 38 | +echo -e "${GREEN}Building Tolgee CLI Docker image...${NC}" |
| 39 | +echo "Tag: ${TAG}" |
| 40 | +echo "Image: ${IMAGE_NAME}:${TAG}" |
| 41 | + |
| 42 | +# Check if Docker is running |
| 43 | +if ! docker info > /dev/null 2>&1; then |
| 44 | + echo -e "${RED}Error: Docker is not running. Please start Docker and try again.${NC}" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +# Docker login if push is requested |
| 49 | +if [ "$PUSH" = "push" ]; then |
| 50 | + if [ -z "${DOCKERHUB_USERNAME:-}" ] || [ -z "${DOCKERHUB_TOKEN:-}" ]; then |
| 51 | + echo -e "${RED}Error: DOCKERHUB_USERNAME and DOCKERHUB_TOKEN environment variables are required for push.${NC}" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + echo -e "${GREEN}Logging in to Docker Hub...${NC}" |
| 55 | + echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin |
| 56 | +fi |
| 57 | + |
| 58 | +# Ensure we have the built files |
| 59 | +if [ ! -d "dist" ]; then |
| 60 | + echo -e "${YELLOW}Warning: dist directory not found. Building the CLI first...${NC}" |
| 61 | + npm run build |
| 62 | +fi |
| 63 | + |
| 64 | +# Prepare tags |
| 65 | +TAGS=( -t "${IMAGE_NAME}:${TAG}" ) |
| 66 | +if [ -n "${VERSION:-}" ] && [ "$TAG" = "latest" ]; then |
| 67 | + TAGS+=( -t "${IMAGE_NAME}:${VERSION}" ) |
| 68 | +fi |
| 69 | + |
| 70 | +# Prepare labels |
| 71 | +LABELS=() |
| 72 | +if [ "$PUSH" = "push" ]; then |
| 73 | + LABELS+=( --label "org.opencontainers.image.title=Tolgee CLI" ) |
| 74 | + LABELS+=( --label "org.opencontainers.image.description=A tool to interact with the Tolgee Platform through CLI" ) |
| 75 | + LABELS+=( --label "org.opencontainers.image.url=https://github.com/tolgee/tolgee-cli" ) |
| 76 | + LABELS+=( --label "org.opencontainers.image.source=https://github.com/tolgee/tolgee-cli" ) |
| 77 | + LABELS+=( --label "org.opencontainers.image.licenses=MIT" ) |
| 78 | + if [ -n "${VERSION:-}" ]; then |
| 79 | + LABELS+=( --label "org.opencontainers.image.version=${VERSION}" ) |
| 80 | + fi |
| 81 | + if [ -n "${GITHUB_SHA:-}" ]; then |
| 82 | + LABELS+=( --label "org.opencontainers.image.revision=${GITHUB_SHA}" ) |
| 83 | + fi |
| 84 | + if [ -n "${BUILD_DATE:-}" ]; then |
| 85 | + LABELS+=( --label "org.opencontainers.image.created=${BUILD_DATE}" ) |
| 86 | + fi |
| 87 | +fi |
| 88 | + |
| 89 | +# Build command |
| 90 | +BUILD_CMD=( docker build ) |
| 91 | +BUILD_CMD+=( "${TAGS[@]}" ) |
| 92 | +if [ ${#LABELS[@]} -gt 0 ]; then |
| 93 | + BUILD_CMD+=( "${LABELS[@]}" ) |
| 94 | +fi |
| 95 | + |
| 96 | +if [ -n "$PLATFORM" ]; then |
| 97 | + echo "Platform(s): ${PLATFORM}" |
| 98 | + # For multi-platform builds, we need buildx |
| 99 | + BUILD_CMD=( docker buildx build --platform "${PLATFORM}" ) |
| 100 | + BUILD_CMD+=( "${TAGS[@]}" ) |
| 101 | + if [ ${#LABELS[@]} -gt 0 ]; then |
| 102 | + BUILD_CMD+=( "${LABELS[@]}" ) |
| 103 | + fi |
| 104 | + |
| 105 | + # Check if buildx is available |
| 106 | + if ! docker buildx version > /dev/null 2>&1; then |
| 107 | + echo -e "${RED}Error: Docker buildx is required for multi-platform builds.${NC}" |
| 108 | + echo "Please install Docker buildx or build for single platform." |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + |
| 112 | + # Create builder if it doesn't exist |
| 113 | + if ! docker buildx inspect tolgee-builder > /dev/null 2>&1; then |
| 114 | + echo -e "${YELLOW}Creating multi-platform builder...${NC}" |
| 115 | + docker buildx create --name tolgee-builder --use |
| 116 | + else |
| 117 | + docker buildx use tolgee-builder |
| 118 | + fi |
| 119 | + |
| 120 | + # Check if this is a single platform build that can be loaded locally |
| 121 | + PLATFORM_COUNT=$(echo "$PLATFORM" | tr ',' '\n' | wc -l) |
| 122 | + if [ "$PLATFORM_COUNT" -eq 1 ] && [ "$PUSH" != "push" ]; then |
| 123 | + # Single platform - we can load it to local Docker daemon |
| 124 | + BUILD_CMD+=( --load ) |
| 125 | + echo -e "${GREEN}Single platform build - image will be available locally after build.${NC}" |
| 126 | + elif [ "$PUSH" = "push" ]; then |
| 127 | + # Push mode - add push flag |
| 128 | + BUILD_CMD+=( --push ) |
| 129 | + echo -e "${GREEN}Build and push mode enabled.${NC}" |
| 130 | + else |
| 131 | + # Multi-platform build - cannot load to local Docker daemon |
| 132 | + echo -e "${YELLOW}Multi-platform build - image will NOT be available locally.${NC}" |
| 133 | + echo -e "${YELLOW}To run locally, build for your current platform only or push to a registry.${NC}" |
| 134 | + fi |
| 135 | +elif [ "$PUSH" = "push" ]; then |
| 136 | + # Regular build with push - we need to build and then push |
| 137 | + echo -e "${GREEN}Build and push mode enabled for single architecture.${NC}" |
| 138 | +fi |
| 139 | + |
| 140 | +BUILD_CMD+=( . ) |
| 141 | + |
| 142 | +echo -e "${GREEN}Running:${NC} ${BUILD_CMD[*]}" |
| 143 | +"${BUILD_CMD[@]}" |
| 144 | + |
| 145 | +if [ "$PUSH" = "push" ] && [ -z "$PLATFORM" ]; then |
| 146 | + # For regular builds, we need to push separately |
| 147 | + echo -e "${GREEN}Pushing images to registry...${NC}" |
| 148 | + docker push ${IMAGE_NAME}:${TAG} |
| 149 | + if [ -n "${VERSION:-}" ] && [ "$TAG" = "latest" ]; then |
| 150 | + docker push ${IMAGE_NAME}:${VERSION} |
| 151 | + fi |
| 152 | +fi |
| 153 | + |
| 154 | +if [ "$PUSH" = "push" ]; then |
| 155 | + echo -e "${GREEN}✓ Docker image built and pushed successfully!${NC}" |
| 156 | +else |
| 157 | + echo -e "${GREEN}✓ Docker image built successfully!${NC}" |
| 158 | +fi |
| 159 | + |
| 160 | +# Show appropriate run instruction based on build type |
| 161 | +if [ -n "$PLATFORM" ]; then |
| 162 | + PLATFORM_COUNT=$(echo "$PLATFORM" | tr ',' '\n' | wc -l) |
| 163 | + if [ "$PLATFORM_COUNT" -eq 1 ]; then |
| 164 | + # Single platform - image is available locally |
| 165 | + echo -e "${GREEN}Run with: docker run --rm ${IMAGE_NAME}:${TAG}${NC}" |
| 166 | + else |
| 167 | + # Multi-platform - image is NOT available locally |
| 168 | + echo -e "${YELLOW}Multi-platform build completed. Image is not available locally.${NC}" |
| 169 | + echo -e "${YELLOW}To run locally: build for your platform only or pull from registry after push.${NC}" |
| 170 | + fi |
| 171 | +else |
| 172 | + # Default build for current platform - always available locally |
| 173 | + echo -e "${GREEN}Run with: docker run --rm ${IMAGE_NAME}:${TAG}${NC}" |
| 174 | +fi |
| 175 | + |
| 176 | +# Show image info |
| 177 | +echo -e "\n${YELLOW}Image information:${NC}" |
| 178 | +if [ -n "$PLATFORM" ]; then |
| 179 | + PLATFORM_COUNT=$(echo "$PLATFORM" | tr ',' '\n' | wc -l) |
| 180 | + if [ "$PLATFORM_COUNT" -eq 1 ]; then |
| 181 | + echo "Single-platform image built and loaded locally." |
| 182 | + docker images ${IMAGE_NAME}:${TAG} |
| 183 | + else |
| 184 | + echo "Multi-platform image built. Use 'docker buildx imagetools inspect ${IMAGE_NAME}:${TAG}' to see details." |
| 185 | + echo "Note: Multi-platform images are not available locally. Build for your current platform to run locally." |
| 186 | + fi |
| 187 | +else |
| 188 | + docker images ${IMAGE_NAME}:${TAG} |
| 189 | +fi |
0 commit comments