jenkinsfile
Some checks failed
eCommerce-backend/pipeline/head Something is wrong with the build of this commit
Some checks failed
eCommerce-backend/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
58
Jenkinsfile
vendored
58
Jenkinsfile
vendored
@@ -2,9 +2,7 @@ pipeline {
|
|||||||
agent any
|
agent any
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
// 1. ADD :80 HERE. This forces nerdctl to stop looking for Port 443
|
|
||||||
REGISTRY = "192.168.108.200:80"
|
REGISTRY = "192.168.108.200:80"
|
||||||
|
|
||||||
APP_NAME = "ecommerce-app"
|
APP_NAME = "ecommerce-app"
|
||||||
IMAGE_NAME = "ecommerce-backend"
|
IMAGE_NAME = "ecommerce-backend"
|
||||||
IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
IMAGE_TAG = "${REGISTRY}/library/${IMAGE_NAME}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
||||||
@@ -13,50 +11,47 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Build Image') {
|
stage('Initialize Agent Tools') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// We run everything in ONE sh block to maintain the session
|
|
||||||
sh """
|
sh """
|
||||||
# 1. Install binaries if they are missing
|
# 1. Install BuildKit if missing
|
||||||
if ! command -v buildkitd >/dev/null; then
|
if ! command -v buildkitd >/dev/null; then
|
||||||
curl -L https://github.com/moby/buildkit/releases/download/v0.12.5/buildkit-v0.12.5.linux-amd64.tar.gz | tar -xz -C /usr/local/bin/ --strip-components=1
|
curl -L https://github.com/moby/buildkit/releases/download/v0.12.5/buildkit-v0.12.5.linux-amd64.tar.gz | tar -xz -C /usr/local/bin/ --strip-components=1
|
||||||
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 2. Setup Sockets
|
# 2. Install kubectl if missing (The cause of error 127)
|
||||||
mkdir -p /run/buildkit /run/buildkit-default
|
if ! command -v kubectl >/dev/null; then
|
||||||
|
echo "Installing kubectl..."
|
||||||
# 3. Start BuildKit in the background specifically for this session
|
curl -LO "https://dl.k8s.io/release/\$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
|
||||||
export JENKINS_NODE_COOKIE=dontKillMe
|
chmod +x kubectl
|
||||||
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
mv kubectl /usr/local/bin/
|
||||||
|
|
||||||
# 4. Wait for socket with a simple check
|
|
||||||
echo "Waiting for BuildKit socket..."
|
|
||||||
for i in \$(seq 1 20); do
|
|
||||||
if [ -S /run/buildkit/buildkitd.sock ]; then
|
|
||||||
echo "Socket is READY."
|
|
||||||
break
|
|
||||||
fi
|
fi
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
# 5. Execute the build immediately in the same step
|
# 3. Start BuildKit
|
||||||
|
mkdir -p /run/buildkit /run/buildkit-default
|
||||||
|
export JENKINS_NODE_COOKIE=dontKillMe
|
||||||
|
if ! pgrep buildkitd > /dev/null; then
|
||||||
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
||||||
|
sleep 10
|
||||||
|
fi
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Build & Push') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
sh """
|
||||||
export BUILDKIT_HOST=unix:///run/buildkit/buildkitd.sock
|
export BUILDKIT_HOST=unix:///run/buildkit/buildkitd.sock
|
||||||
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} .
|
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} .
|
||||||
"""
|
"""
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Push to Harbor') {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
sh """
|
sh "echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry"
|
||||||
echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry
|
sh "nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry"
|
||||||
nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +61,7 @@ pipeline {
|
|||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
||||||
|
// Now kubectl will exist and be found
|
||||||
sh """
|
sh """
|
||||||
kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \
|
kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \
|
||||||
'{"spec": {"template": {"spec": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}'
|
'{"spec": {"template": {"spec": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}'
|
||||||
|
|||||||
Reference in New Issue
Block a user