// build-steps.libsonnet - Build-specific steps with replica-based scaling and locking { externalBuildahStep: { name: "build-via-external-buildah", image: "alpine:latest", pull: "if-not-exists", commands: [ "echo '๐Ÿ—๏ธ Building via external Buildah deployment with replica scaling...'", "echo 'Installing kubectl...'", "apk add --no-cache curl", "curl -LO \"https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl\"", "chmod +x kubectl", "mv kubectl /usr/local/bin/", "echo '๐Ÿ“ฆ Preparing build context...'", "BUILD_ID=\"auth-service-${DRONE_BUILD_NUMBER}-$(date +%s)\"", "echo \"Build ID: $BUILD_ID\"", "echo '๐Ÿ” Checking current Buildah deployment replicas...'", "CURRENT_REPLICAS=$(kubectl get deployment buildah-external -n apps--droneio--prd -o jsonpath='{.spec.replicas}')", "echo \"Current replicas: $CURRENT_REPLICAS\"", "echo '๐Ÿ”’ Attempting to scale up Buildah deployment (acts as build lock)...'", "if [ \"$CURRENT_REPLICAS\" = \"0\" ]; then", " echo \"โœ… No build running, scaling up deployment...\"", " kubectl scale deployment buildah-external --replicas=1 -n apps--droneio--prd", " echo \"โณ Waiting for pod to be ready...\"", " kubectl wait --for=condition=ready pod -l app=buildah-external -n apps--droneio--prd --timeout=120s", "else", " echo \"โŒ Build already running (replicas=$CURRENT_REPLICAS)! Aborting to prevent conflicts.\"", " exit 1", "fi", "echo '๏ฟฝ Finding ready Buildah pod...'", "BUILDAH_POD=$(kubectl get pods -n apps--droneio--prd -l app=buildah-external --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}')", "if [ -z \"$BUILDAH_POD\" ]; then", " echo \"โŒ No running Buildah pod found after scaling!\"", " kubectl get pods -n apps--droneio--prd -l app=buildah-external", " exit 1", "fi", "echo \"โœ… Using Buildah pod: $BUILDAH_POD\"", "echo '๐Ÿ“ Creating build directory in Buildah pod...'", "kubectl exec $BUILDAH_POD -n apps--droneio--prd -- mkdir -p \"/workspace/builds/$BUILD_ID\"", "echo '๐Ÿ“ค Copying source files to Buildah pod...'", "tar czf - . | kubectl exec -i $BUILDAH_POD -n apps--droneio--prd -- tar xzf - -C \"/workspace/builds/$BUILD_ID\"", "echo '๐Ÿ”จ Building container image with version from config...'", "echo 'Reading version configuration...'", ". ./version.conf", "DOCKER_TAG=\"$DOCKER_REPO:$BASE_VERSION.$DRONE_BUILD_NUMBER\"", "echo \"Building with tag: $DOCKER_TAG\"", "kubectl exec $BUILDAH_POD -n apps--droneio--prd -- sh -c \"cd /workspace/builds/$BUILD_ID && buildah build --isolation=chroot --storage-driver=vfs --format=docker --tag $DOCKER_TAG .\"", "echo '๐Ÿ“‹ Listing built images...'", "kubectl exec $BUILDAH_POD -n apps--droneio--prd -- buildah images | grep auth-service", "echo \"โœ… Image built with tag: $DOCKER_TAG\"", "echo '๐Ÿงน Cleaning up build directory...'", "kubectl exec $BUILDAH_POD -n apps--droneio--prd -- rm -rf \"/workspace/builds/$BUILD_ID\"", "echo 'โœ… External Buildah build completed successfully!'" ], when: { event: ["push"] } }, pushDockerStep: { name: "push-docker-image", image: "alpine:latest", environment: { DOCKER_USERNAME: { from_secret: "docker_username" }, DOCKER_PASSWORD: { from_secret: "docker_password" }, DOCKER_REGISTRY: { from_secret: "docker_registry" } }, commands: [ "echo '๐Ÿ“ค Pushing Docker image to registry...'", "echo 'Installing kubectl...'", "apk add --no-cache curl", "curl -LO \"https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl\"", "chmod +x kubectl && mv kubectl /usr/local/bin/", "echo 'Reading version configuration...'", ". ./version.conf", "DOCKER_TAG=\"$DOCKER_REPO:$BASE_VERSION.$DRONE_BUILD_NUMBER\"", "echo \"Pushing image: $DOCKER_TAG\"", "echo '๐Ÿ” Finding Buildah pod...'", "BUILDAH_POD=$(kubectl get pods -n apps--droneio--prd -l app=buildah-external --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}')", "echo \"Using Buildah pod: $BUILDAH_POD\"", "echo '๐Ÿ”‘ Authenticating with Docker registry...'", "if [ -n \"$DOCKER_USERNAME\" ] && [ -n \"$DOCKER_PASSWORD\" ]; then", " echo \"Logging into Docker registry...\"", " kubectl exec $BUILDAH_POD -n apps--droneio--prd -- buildah login -u \"$DOCKER_USERNAME\" -p \"$DOCKER_PASSWORD\" \"$DOCKER_REGISTRY\"", "else", " echo \"No Docker credentials provided - attempting unauthenticated push\"", "fi", "echo '๐Ÿš€ Pushing image to registry...'", "kubectl exec $BUILDAH_POD -n apps--droneio--prd -- buildah push \"$DOCKER_TAG\"", "echo \"โœ… Successfully pushed: $DOCKER_TAG\"" ], when: { event: ["push"], branch: ["main", "master"] } }, scaleDownStep: { name: "scale-down-buildah", image: "alpine:latest", commands: [ "echo '๐Ÿ”ฝ Scaling down Buildah deployment (release build lock)...'", "apk add --no-cache curl", "curl -LO \"https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl\"", "chmod +x kubectl && mv kubectl /usr/local/bin/", "echo '๐Ÿ“Š Current deployment status:'", "kubectl get deployment buildah-external -n apps--droneio--prd", "echo '๐Ÿ”ฝ Scaling down to 0 replicas...'", "kubectl scale deployment buildah-external --replicas=0 -n apps--droneio--prd", "echo 'โณ Waiting for pods to terminate...'", "kubectl wait --for=delete pod -l app=buildah-external -n apps--droneio--prd --timeout=60s || echo \"Pods may still be terminating\"", "echo 'โœ… Buildah deployment scaled down - build lock released!'" ], when: { status: ["success", "failure"] } } }