Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-60547

KubernetesDeclarativeAgent workingDir is ignored in templating Jenkins agent pod

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • kubernetes-plugin
    • None

      While working around the changes in JENKINS-58705, I discovered the KubernetesDeclarativeAgent.workingDir attribute method of configuring the workspace-volume of the Jenkins agent is ignored as it results in the recent /home/jenkins/agent/ change.

      Example Jenkinsfile demonstrating issue:

      pipeline {
          agent {
              kubernetes {
                  cloud 'openshift'
                  label UUID.randomUUID().toString()
                  workingDir '/home/jenkins'
              }
          }
          options {
              timestamps()
          }
          stages {
              stage('Greeting') {
                  steps {
                      script {
                          container('jnlp') {
                              sh 'echo "Hello world"'
                          }
                      }
                  }
              }
          }
      }
      

      Resulting console output:

      [Pipeline] Start of Pipeline
      [Pipeline] podTemplate
      [Pipeline] {
      [Pipeline] node
      Still waiting to schedule task
      ‘eb0a374c-d844-4327-a140-891e892cfd76-793rd-5rhhr’ is offline
      Agent eb0a374c-d844-4327-a140-891e892cfd76-793rd-5rhhr is provisioned from template Kubernetes Pod Template

      apiVersion: "v1"
      kind: "Pod"
      metadata:
      annotations:
      buildUrl: "http://172.24.30.67:80/job/os-demo-afeller/job/os-demo-afeller-pull-request/18/"
      labels:
      jenkins: "slave"
      jenkins/eb0a374c-d844-4327-a140-891e892cfd76: "true"
      name: "eb0a374c-d844-4327-a140-891e892cfd76-793rd-5rhhr"
      spec:
      containers:

      • env:
      • name: "JENKINS_SECRET"
        value: "********"
      • name: "JENKINS_TUNNEL"
        value: "172.24.221.21:50000"
      • name: "JENKINS_AGENT_NAME"
        value: "eb0a374c-d844-4327-a140-891e892cfd76-793rd-5rhhr"
      • name: "JENKINS_NAME"
        value: "eb0a374c-d844-4327-a140-891e892cfd76-793rd-5rhhr"
      • name: "JENKINS_AGENT_WORKDIR"
        value: "/home/jenkins/agent"
      • name: "JENKINS_URL"
        value: "http://172.24.30.67:80/"
      • name: "HOME"
        value: "/home/jenkins"
        image: "jenkins/jnlp-slave:alpine"
        name: "jnlp"
        volumeMounts:
      • mountPath: "/home/jenkins/agent"
        name: "workspace-volume"
        readOnly: false
        nodeSelector: {}
        restartPolicy: "Never"
        volumes:
      • emptyDir: {}
        name: "workspace-volume"

      As the KubernetesDeclarativeAgent passes configuration to PodTemplate, I think the issue is in PodTemplate resolves the working dir.

          [JENKINS-60547] KubernetesDeclarativeAgent workingDir is ignored in templating Jenkins agent pod

          Andrew Feller added a comment -

          Also worth noting that hard coding the volume mount in yaml fails spectacularly

          ---
          apiVersion: "v1"
          kind: "Pod"
          metadata:
            labels:
              jenkins: "slave"
              jenkins/os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f: "true"
            name: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp"
          spec:
            containers:
            - env:
              - name: "JENKINS_SECRET"
                value: "********"
              - name: "JENKINS_TUNNEL"
                value: "172.24.221.21:50000"
              - name: "JENKINS_AGENT_NAME"
                value: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp"
              - name: "JENKINS_NAME"
                value: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp"
              - name: "JENKINS_AGENT_WORKDIR"
                value: "/home/jenkins/agent"
              - name: "JENKINS_URL"
                value: "http://172.24.30.67:80/"
              - name: "HOME"
                value: "/home/jenkins"
              image: "openshift/jenkins-slave-base-centos7:v3.11"
              name: "jnlp"
              resources:
                limits:
                  cpu: "1.0"
                  memory: "1Gi"
                requests:
                  cpu: "0.1"
                  memory: "0Mi"
              volumeMounts:
              - mountPath: "/home/jenkins"
                name: "workspace-volume"
                readOnly: false
              - mountPath: "/home/jenkins/agent"
                name: "workspace-volume"
                readOnly: false
            imagePullSecrets:
            - name: ...
            nodeSelector: {}
            restartPolicy: "Never"
            serviceAccountName: "jenkins"
            volumes:
            - emptyDir: {}
              name: "workspace-volume"
          

          Andrew Feller added a comment - Also worth noting that hard coding the volume mount in yaml fails spectacularly --- apiVersion: "v1" kind: "Pod" metadata: labels: jenkins: "slave" jenkins/os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f: " true " name: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp" spec: containers: - env: - name: "JENKINS_SECRET" value: "********" - name: "JENKINS_TUNNEL" value: "172.24.221.21:50000" - name: "JENKINS_AGENT_NAME" value: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp" - name: "JENKINS_NAME" value: "os-demo-6a49dcb7-3b17-4415-aa59-ea9a7fe2783f-80z3r-ncjrp" - name: "JENKINS_AGENT_WORKDIR" value: "/home/jenkins/agent" - name: "JENKINS_URL" value: "http: //172.24.30.67:80/" - name: "HOME" value: "/home/jenkins" image: "openshift/jenkins-slave-base-centos7:v3.11" name: "jnlp" resources: limits: cpu: "1.0" memory: "1Gi" requests: cpu: "0.1" memory: "0Mi" volumeMounts: - mountPath: "/home/jenkins" name: "workspace-volume" readOnly: false - mountPath: "/home/jenkins/agent" name: "workspace-volume" readOnly: false imagePullSecrets: - name: ... nodeSelector: {} restartPolicy: "Never" serviceAccountName: "jenkins" volumes: - emptyDir: {} name: "workspace-volume"

            vlatombe Vincent Latombe
            andyfeller Andrew Feller
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: