Jenkins - Kubernetes Plugin inm OpenShift. Only jnlp containers work

3/8/2019

I am currently working on a build pipeline in OpenShift using the following Configuration

openshift: v3.6.173.0.140
Jenkins: 2.017 (using the redhat images from https://github.com/openshift/jenkins)
Jenkins-Kubernetes plugin 1.12.2

As Jenkins Agents I am using the nodejs agents supplied by the openshift jenkins template and images that build up on them (e.g. one image that I also fittet with a typescript compiler)

Now what I want to do is to run pods with multiple containers (not just the jnlp one but standard node, go etc. containers) Now according to the documentation this should not be a problem (https://github.com/jenkinsci/kubernetes-plugin) as I should just add containers to my podTemplate like

podTemplate(label: mylabel, cloud: 'openshift', 
  containers: [
    containerTemplate(
      name: "jnlp",
      resourceRequestMemory: "512Mi",
      resourceLimitMemory: "2048Mi",
      workingDir: "/home/default",
      tty: "false",
      imagePullPolicy: "Always",
      image: 'private-registry:5000/namespace/nodejs-tsc-jnlp-image:latest',
      args: '${computer.jnlpmac} ${computer.name}',
    ),
    containerTemplate(
      name: 'node',
      resourceRequestMemory: '512Mi',
      resourceLimitMemory: '2048Mi',
      workingDir: '/home/default',
      tty: 'true',
      imagePullPolicy: 'Always',
      image: 'node:alpine',
      command: 'cat'
    )
  ]
)

Now the problem is, that this is not working. Pulling the image for the node container just works fine, and if I use echo test for example instead of cat as command test will show in the containers log, but the container will just pass as completed and it will not execute anything that is described in the pipeline. Again this is completely written like stated in the documentation

node(mylabel){
stage('TEST NODE'){
  container("node"){
    sh("echo test node")
  }
  container("jnlp"){
    sh("echo test jnlp")
  }
}

Any idea what I am doing wrong?

-- relief.melone
jenkins
jenkins-pipeline
kubernetes
openshift

2 Answers

3/8/2019

I would try using as the command: /bin/sh -c and cat as an argument.

-- iocanel
Source: StackOverflow

3/12/2019

Ok,

it seems like the only mistake I made was the following. Use a stage inside the container and it works. In Openshift it is also better to use official jenkins slave image as the community image ran into problems with the git checkout for me. I also made some changes to use a yamlfile to configure my build pod. So my setup now looks like this (with just a basic stage as example)

jenkins/BuildPod.yaml

kind: Pod
apiVersion: v1
metadata:
  labels:
    app: my-build-pod
spec:
  containers:
  - name: jnlp
    image: openshift/jenkins-slave-base-centos7:latest
  - name: nodejs
    image: node:8-alpine
    command:
    - cat

Jenkinsfile

node {
  checkout scm
  def label = "mypod-${UUID.randomUUID().toString()}"

  podTemplate(
    label: label
    cloud: 'openshift'
    yaml: readFile(file: "jenkins/BuildPod.yaml")
  ){
    node(label){
      container('nodejs'){
        stage('Check versions'){
          sh 'node -v'
          sh 'npm -v'
        }
      }
    }
  }
}
-- relief.melone
Source: StackOverflow