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.
CodePudding user response:
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.