132 lines
6.0 KiB
Jsonnet
132 lines
6.0 KiB
Jsonnet
// 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 '<EFBFBD> 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"]
|
||
}
|
||
}
|
||
} |