<p>I have kubernetes job and I would like to get his pod logs in the jenkins pipeline.</p>
<p>So I try to grep pod name to the jenkins variable and then get logs.</p>
<pre><code>POD_NAME = sh script: &quot;kubectl describe jobs.batch ${JOB_NAME} | grep 'Created pod' | cut -d':' -f2&quot;

echo &quot;${POD_NAME}&quot;

sh &quot;kubectl logs --follow ${POD_NAME}&quot;
</code></pre>
<p>But I got <code>null</code> in the <code>POD_NAME</code> variable.</p>

user3433266
<p>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.</p>
<p>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.</p>
<p>You can do that by adding a label to the <code>.spec.metadata.labels</code> section as shown below and then query with kubectl and the <code>--selector</code> flag:</p>
<pre><code>---
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
        ...

</code></pre>
<p><code>kubectl logs --follow --selector test=value</code></p>
<p>Use <code>kubectl logs --help</code> to get further information and examples.</p>
