<p>I have created Kubernetes job and job got created, but it is failing on deployment to the Kubernetes cluster. When I try to redeploy it using Helm the job is not redeploying (deleting old job and recreating new one, unlike a microservice deployment).</p>
<p>How can I achieve this redeploying job without manually deleting it in Kubernetes? Could I tell it to recreate the container?</p>
<p><code>job.yaml</code> contains:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: &quot;{{ .Release.Name }}-init-job&quot;
  namespace: {{ .Release.Namespace }}
spec:
  template:
    metadata:
      annotations:
        linkerd.io/inject: disabled
        &quot;helm.sh/hook-delete-policy&quot;: before-hook-creation
        &quot;helm.sh/hook&quot;: pre-install,pre-upgrade,pre-delete
        &quot;helm.sh/hook-weight&quot;: &quot;-5&quot;
    spec:
      serviceAccountName: {{ .Release.Name }}-init-service-account
      containers:
        - name: app-installer
          image: some image
          command:
            - /bin/bash
            - -c
            - echo Hello executing k8s init-container
          securityContext:
            readOnlyRootFilesystem: true
      restartPolicy: OnFailure
</code></pre>
<p>Job is not getting redeployed</p>
<pre><code>kubectl get jobs -n namespace

NAME                    COMPLETIONS   DURATION   AGE
test-init-job   0/1           13h        13h
</code></pre>
<p>kubectl describe job test-init-job -n test</p>
<pre><code>Name:           test-init-job
Namespace:      test
Selector:       controller-uid=86370470-0964-42d5-a9c1-00c8a462239f
Labels:         app.kubernetes.io/managed-by=Helm
Annotations:    meta.helm.sh/release-name: test
                meta.helm.sh/release-namespace: test
Parallelism:    1
Completions:    1
Start Time:     Fri, 14 Oct 2022 18:03:31 +0530
Pods Statuses:  0 Running / 0 Succeeded / 1 Failed
Pod Template:
  Labels:           controller-uid=86370470-0964-42d5-a9c1-00c8a462239f
                    job-name=test-init-job
  Annotations:      helm.sh/hook: pre-install,pre-upgrade
                    helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
                    helm.sh/hook-weight: -5
                    linkerd.io/inject: disabled
  Service Account:  test-init-service-account
  Containers:
   test-app-installer:
    Image:      artifactory/test-init-container:1.0.0
    Port:       &lt;none&gt;
    Host Port:  &lt;none&gt;
    Environment:
      test.baseUrl:         baser
      test.authType:        basic
      test.basic.username:  jack
      test.basic.password:  password
    Mounts:
      /etc/test/config from test-installer-config-vol (ro)
  Volumes:
   test-installer-config-vol:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      test-installer-config-files
    Optional:  false
Events:        &lt;none&gt;
</code></pre>

santosh jk
<p>Jobs cannot be redeployed, because their template is immutable.</p>
<p>If you get a message <code>cannot patch &quot;...&quot; with kind Job: Job.batch &quot;db-sync-download-snapshot&quot; is invalid: spec.template: Invalid value: ... field is immutable</code>, this is the reason.</p>
<p>Just delete a job, or create it with a <code>$VERSION</code> appended to a name.</p>
<p>With GitOps, <code>$VERSION</code> should be <code>$CI_COMMIT_HASH</code>.</p>
<p>Replace the <code>name</code> within the Helm template:</p>
<pre><code>  metadata:
    name: &quot;{{ .Release.Name }}-init-job-{{ .Release.revision }}&quot;
</code></pre>
