Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to grep kubectl output to the Jenkins variable

I have kubernetes job and I would like to get his pod logs in the jenkins pipeline.

So I try to grep pod name to the jenkins variable and then get logs.

POD_NAME = sh script: "kubectl describe jobs.batch ${JOB_NAME} | grep 'Created pod' | cut -d':' -f2"

echo "${POD_NAME}"

sh "kubectl logs --follow ${POD_NAME}"

But I got null in the POD_NAME variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I assume that your jenkins controller or agent is able to query the kubernetes api with kubectl because it has a serviceaccount or some other form of credential to access kubernetes.

If that is true, I propose that you use a label to identify the pods created by the job and to query anything related to them.

You can do that by adding a label to the .spec.metadata.labels section as shown below and then query with kubectl and the --selector flag:

---
apiVersion: batch/v1
kind: Job
metadata:
  name: MYAPP
  ...
spec:
  template:
    metadata:
      ...
      labels:
        test: value
    spec:
      containers:
      - name: MYAPP
        image: python:3.7.6-alpine3.10
        ...

kubectl logs --follow --selector test=value

Use kubectl logs --help to get further information and examples.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading