jenkinsfile
Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
Some checks failed
eCommerce-backend/pipeline/head There was a failure building this commit
This commit is contained in:
63
Jenkinsfile
vendored
63
Jenkinsfile
vendored
@@ -11,60 +11,63 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Initialize Agent') {
|
stage('Build Image') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// 1. Install binaries if missing
|
// We run everything in ONE sh block to maintain the session
|
||||||
sh """
|
sh """
|
||||||
|
# 1. Install binaries if they are 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
|
||||||
fi
|
fi
|
||||||
ln -sf /usr/local/bin/buildctl /usr/bin/buildctl
|
|
||||||
"""
|
|
||||||
|
|
||||||
// 2. Start BuildKit and prevent Jenkins from killing it
|
# 2. Setup Sockets
|
||||||
sh """
|
|
||||||
mkdir -p /run/buildkit /run/buildkit-default
|
mkdir -p /run/buildkit /run/buildkit-default
|
||||||
if ! pgrep buildkitd > /dev/null; then
|
|
||||||
echo "Starting buildkitd..."
|
# 3. Start BuildKit in the background specifically for this session
|
||||||
# The 'dontKillMe' cookie is the secret sauce here
|
export JENKINS_NODE_COOKIE=dontKillMe
|
||||||
export JENKINS_NODE_COOKIE=dontKillMe
|
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
||||||
nohup buildkitd --addr unix:///run/buildkit/buildkitd.sock --addr unix:///run/buildkit-default/buildkitd.sock > /tmp/buildkitd.log 2>&1 &
|
|
||||||
|
# 4. Wait for socket with a simple check
|
||||||
# Wait for the socket to actually appear
|
echo "Waiting for BuildKit socket..."
|
||||||
for i in \$(seq 1 15); do
|
for i in \$(seq 1 20); do
|
||||||
if [ -S /run/buildkit/buildkitd.sock ]; then
|
if [ -S /run/buildkit/buildkitd.sock ]; then
|
||||||
echo "Socket found!"
|
echo "Socket is READY."
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
sleep 2
|
sleep 1
|
||||||
done
|
done
|
||||||
fi
|
|
||||||
|
# 5. Execute the build immediately in the same step
|
||||||
|
export BUILDKIT_HOST=unix:///run/buildkit/buildkitd.sock
|
||||||
|
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} .
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Build & Push') {
|
stage('Push to Harbor') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Use the socket path verified in your logs
|
|
||||||
sh "export BUILDKIT_HOST=unix:///run/buildkit/buildkitd.sock && \
|
|
||||||
nerdctl --address ${CONTAINERD_ADDR} build --insecure-registry -t ${IMAGE_TAG} ."
|
|
||||||
|
|
||||||
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
withCredentials([usernamePassword(credentialsId: 'harbor-creds', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
sh "echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry"
|
sh """
|
||||||
sh "nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry"
|
echo '${PASS}' | nerdctl --address ${CONTAINERD_ADDR} login ${REGISTRY} -u ${USER} --password-stdin --insecure-registry
|
||||||
|
nerdctl --address ${CONTAINERD_ADDR} push ${IMAGE_TAG} --insecure-registry
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Deploy') {
|
stage('Deploy to K8s') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
withCredentials([file(credentialsId: 'k8s-config', variable: 'KUBECONFIG')]) {
|
||||||
sh "kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch '{\"spec\": {\"template\": {\"spec\": {\"containers\": [{\"name\": \"${APP_NAME}\", \"image\": \"${IMAGE_TAG}\", \"imagePullPolicy\": \"Never\"}]}}}}'"
|
sh """
|
||||||
|
kubectl --kubeconfig=${KUBECONFIG} patch deployment ${APP_NAME} -n ${NAMESPACE} --patch \
|
||||||
|
'{"spec": {"template": {"spec": {"containers": [{"name": "${APP_NAME}", "image": "${IMAGE_TAG}", "imagePullPolicy": "Never"}]}}}}'
|
||||||
|
"""
|
||||||
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
sh "kubectl --kubeconfig=${KUBECONFIG} rollout status deployment/${APP_NAME} -n ${NAMESPACE}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user