Home > OS >  Jenkins job executed on kubernetes cluster runs on jnlp image instead of specified one
Jenkins job executed on kubernetes cluster runs on jnlp image instead of specified one

Time:01-15

I have a situation I don't quite understand. It seems like my code is executed in the wrong image - even though the log states that it has loaded my image.

Expected output vs actual

When I run docker run -it jstevnsvig/jenkins-build-slave-php:v8.2 /bin/sh -c "dpkg-query -l | cat" I get the expected output.
When I run a Jenkins freestyle job with the appropriate tag, with a single script step executing dpkg-query -l | cat I get what I assume is the output from running in the jnlp image (jenkins/inbound-agent:3077.vd69cf116da_6f-3-jdk11) How do I get it to run on the correct image?

docker vs k8s

Jenkins Kubernetes cloud Pod template Configuration

Jenkins configuration

Jenkins job console

Started by user JoSSte
Running as SYSTEM
Agent php8-2dfd8 is provisioned from template php8
---
apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    jenkins: "slave"
    jenkins/label-digest: "cb0616020c016c081bd72366740b4151da57f6a3"
    jenkins/label: "php8"
  name: "php8-2dfd8"
  namespace: "devops-tools"
spec:
  containers:
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "jstevnsvig/jenkins-build-slave-php:v8.2"
    imagePullPolicy: "IfNotPresent"
    name: "php8t"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - env:
    - name: "JENKINS_PROTOCOLS"
      value: "JNLP4-connect"
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "php8-2dfd8"
    - name: "JENKINS_DIRECT_CONNECTION"
      value: "jenkins-service:50000"
    - name: "JENKINS_INSTANCE_IDENTITY"
      value: "..."
    - name: "JENKINS_NAME"
      value: "php8-2dfd8"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    image: "jenkins/inbound-agent:3077.vd69cf116da_6f-3-jdk11"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - emptyDir:
      medium: ""
    name: "workspace-volume"

I have tried changing the label of the container template to jnlp but then it doesn't connect properly (my image is built on the jenkins/agent image)

CodePudding user response:

As documented here, by default, commands in Jenkins agents will run in the jnlp container. If you want to have them run in another container, that you have defined in the podTemplate, you use the container {..} block (section here)

podTemplate(containers: [
    containerTemplate(name: 'maven', image: 'maven:3.8.1-jdk-8', command: 'sleep', args: '99d'),
    containerTemplate(name: 'golang', image: 'golang:1.16.5', command: 'sleep', args: '99d')
  ]) {

    node(POD_LABEL) {
        stage('Get a Maven project') {
            git 'https://github.com/jenkinsci/kubernetes-plugin.git'
            container('maven') {
                stage('Build a Maven project') {
                    sh 'mvn -B -ntp clean install'
                }
            }
        }
...
  • Related