jenkinsjenkins-pipelinejenkins-pluginsjenkins-kubernetes

In the Jenkins Kubernetes plugin, the pod was terminated before it finished everything


I have installed jenkins on k8s with helm and want to run the desired image with agent. However, I have a problem that when I run the agent, it hangs immediately.

My Agent Image Dockerfile

FROM golang:1.21.1-alpine3.18 AS builder
RUN apk update
RUN apk upgrade
RUN apk add build-base

WORKDIR /app
COPY go_src .
RUN go mod download

WORKDIR /app/cmd/runner/
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o /bin/runner.

ENTRYPOINT ["/bin/runner"]

My podTemplate and Pipeline

pipeline {
    agent none
    stages {
        stage('Step 1') {
            agent {
                kubernetes {
                    yaml """
                    apiVersion: v1
                    kind: Pod
                    spec:
                      containers:
                      - name: runner
                        image: my_image:latest
                        volumeMounts:
                        - name: cache
                          mountPath: /cache
                      volumes:
                        - name: cache
                          persistentVolumeClaim:
                            claimName: cache-pvc
                      imagePullSecrets:
                      - name: regcred
                    """
                }
            }
            steps {
                container('runner') {
                    sh 'echo "This is runner."'
                }
            }
        }
    }
}

I don't want to do anything in the step part. That doesn't seem to be possible, so I just do an echo or something. Note that if I add sleep here, it will do as much as sleep and then exit. But I don't want to specify a time, I just want the agent to exit when the go in the image finishes all its work.

Maybe I'm misunderstanding the jenkins kubernetes plugin? Is this the right way to use it? If this is correct, why would it terminate if I do this? I would like to know how to make it not terminate. Thanks in advance.

I've also tried adding sleep in the script, and setting command: cat and tty:true. Googling around, my problem is not mentioned anywhere.


Solution

  • Jenkins k8s plugin takes care of starting and stopping containers. Containers are not expected to have an entrypoint or command parameters that affect their lifecycle. Most commonly these are replaced with sleep 99d. All the logic, like executing /bin/runner in your case, should be in the pipeline.