0

I have a simple Jenkins pipeline which creates a pod with 3 containers - jnlp, dind, example-test

This looks as follows -

    agent {
        kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  name: example-pb
  annotations:
    container.apparmor.security.beta.kubernetes.io/dind: unconfined
    container.seccomp.security.alpha.kubernetes.io/dind: unconfined
  labels:
    some-label: label1
spec:
  serviceAccountName: example
  securityContext:
    runAsUser: 10000
    runAsGroup: 10000
  containers: 
    - name: jnlp
      image: 'jenkins/jnlp-slave:4.3-4-alpine'
      args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
    - name: dind
      image: docker:dind
      securityContext:
          runAsUser: 0
          runAsGroup: 0
          fsGroup: 0        
          privileged: true
      tty: true
      volumeMounts:
      - name: var-run
        mountPath: /var/run
    - name: example-test
      image: pranavbhatia/example-test:0.1
      securityContext:
          runAsUser: 0
          runAsGroup: 0
          fsGroup: 0
      volumeMounts:
      - name: var-run
        mountPath: /var/run  
  volumes:
  - emptyDir: {}
    name: var-run
"""
        }
    }

Also defined a few stages -

    stages {
        stage ('DIND') {
            steps {
                container('dind') {
                    sh 'pwd && echo "Pulling image" && docker pull ubuntu:18.04'
                }
            }
        }
        stage ('EXAMPLE') {
            steps {
                container('example-test') {
                    sh './example'
                }
            }
        }

So now I have this script "example" in my root folder and I want to run this but somehow it's not able to find it.

The Dockerfile looks something like this -

FROM ubuntu:18.04

COPY ./example ./example

#make it executable
RUN chmod +x ./example

#command to keep container running in detached mode
CMD tail -f /dev/null

pwd returns with "/home/jenkins/agent/workspace/test-pipeline" and not the docker container path.

The output is as follows -

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: test-pipeline-14-s7167-4zcg5-s68gw in namespace dc-pipeline
Still waiting to schedule task
‘test-pipeline-14-s7167-4zcg5-s68gw’ is offline
Agent test-pipeline-14-s7167-4zcg5-s68gw is provisioned from template test-pipeline_14-s7167-4zcg5
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    container.apparmor.security.beta.kubernetes.io/dind: "unconfined"
    container.seccomp.security.alpha.kubernetes.io/dind: "unconfined"
    buildUrl: "http://jenkins-164-229:8080/job/test-pipeline/14/"
    runUrl: "job/test-pipeline/14/"
  labels:
    some-label: "label1"
    jenkins: "slave"
    jenkins/label: "test-pipeline_14-s7167"
  name: "test-pipeline-14-s7167-4zcg5-s68gw"
spec:
  containers:
  - args:
    - "$(JENKINS_SECRET)"
    - "$(JENKINS_NAME)"
    env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_TUNNEL"
      value: "jenkins-164-229-agent:50000"
    - name: "JENKINS_AGENT_NAME"
      value: "test-pipeline-14-s7167-4zcg5-s68gw"
    - name: "JENKINS_NAME"
      value: "test-pipeline-14-s7167-4zcg5-s68gw"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://jenkins-164-229:8080/"
    - name: "HOME"
      value: "/home/jenkins"
    image: "jenkins/jnlp-slave:4.3-4-alpine"
    name: "jnlp"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  - image: "pranavbhatia/example-test:0.1"
    name: "example-test"
    securityContext:
      runAsGroup: 0
      runAsUser: 0
    volumeMounts:
    - mountPath: "/var/run"
      name: "var-run"
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  - image: "docker:dind"
    name: "dind"
    securityContext:
      privileged: true
      runAsGroup: 0
      runAsUser: 0
    tty: true
    volumeMounts:
    - mountPath: "/var/run"
      name: "var-run"
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  nodeSelector:
    beta.kubernetes.io/os: "linux"
  restartPolicy: "Never"
  securityContext:
    runAsGroup: 10000
    runAsUser: 10000
  serviceAccountName: "example"
  volumes:
  - emptyDir: {}
    name: "var-run"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on test-pipeline-14-s7167-4zcg5-s68gw in /home/jenkins/agent/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DIND)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ echo 'Pulling image'
Pulling image
+ docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7595c8c21622: Pulling fs layer
d13af8ca898f: Pulling fs layer
70799171ddba: Pulling fs layer
b6c12202c5ef: Pulling fs layer
b6c12202c5ef: Waiting
d13af8ca898f: Verifying Checksum
d13af8ca898f: Download complete
70799171ddba: Verifying Checksum
70799171ddba: Download complete
b6c12202c5ef: Verifying Checksum
b6c12202c5ef: Download complete
7595c8c21622: Verifying Checksum
7595c8c21622: Download complete
7595c8c21622: Pull complete
d13af8ca898f: Pull complete
70799171ddba: Pull complete
b6c12202c5ef: Pull complete
Digest: sha256:a61728f6128fb4a7a20efaa7597607ed6e69973ee9b9123e3b4fd28b7bba100b
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (EXAMPLE)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ ./example
/home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: 1: /home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: ./example: not found
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

Any idea on how to fix this?

Pranav Bhatia
  • 440
  • 2
  • 6
  • 10

1 Answers1

2

It may work to execute it with sh '/example' (without the dot). You installed it in the root filesystem, but when commands within the container are run from Jenkins the PWD will be the workspace.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • Running an "ls" command returns me with this - so seems like it's not running inside the container. Even "pwd" seems to be /home/jenkins/agent/workspace/test-pipeline Output of ls -> ls -la ./ total 8 drwxr-xr-x. 2 10000 10000 4096 Aug 4 13:07 . drwxr-xr-x. 4 10000 10000 4096 Aug 4 13:07 .. – Pranav Bhatia Aug 04 '20 at 14:58
  • 1
    what do you get with `ls -la /` ? Does it show the `example` file? – Henry Aug 04 '20 at 15:00
  • My bad it worked without the dot. But I still have trouble with the difference. "run from Jenkins the PWD will be the workspace" - what would this mean? – Pranav Bhatia Aug 04 '20 at 15:11
  • 1
    Jenkins executes the commands within the container but it sets the workspace as the current directory before doing so. In the Dockerfile the current directory depends on the base image (if you did not change it). The Ubuntu base image has / as the current directory. – Henry Aug 04 '20 at 15:14