<p>I'm trying to understand what a good practice is for what I would assume is a pretty common scenario of building and deploying a kubernetes deployment using kubectl.</p>

<p>Scenario:</p>

<ul>
<li>I want to build a project and deploy it to my cluster.</li>
<li>I’ve got a yaml file with my deployment (and related things), and some src code with a Dockerfile.</li>
<li>I want to avoid using <strong>:latest</strong> in the image tag as per <a href="https://kubernetes.io/docs/concepts/configuration/overview/#container-images" rel="nofollow noreferrer">Best Practice recommendation</a> and instead use a unique tag for that image.</li>
<li>I have easy access to a unique identifier, either coming from CI or git sha that is available as an environment variable at time of building.</li>
</ul>

<p>So that seems pretty simple:</p>

<pre><code>docker build -t my-registry/my-app:${BUILD_NUMBER} .
docker push my-registry/my-app:${BUILD_NUMBER}
kubectl apply -f kube.yaml
</code></pre>

<p>Except that there is no native way to tell kubectl what image name to use.</p>

<p>I understand that <a href="https://github.com/kubernetes/kubernetes/issues/52787#issuecomment-369645645" rel="nofollow noreferrer">kubectl does not support environment variable template substitution</a>, but I've never seen just the usecase of image tags being mentioned explicitly.</p>

<p>I can use <a href="https://github.com/kubernetes/kubernetes/issues/52787#issuecomment-369397528" rel="nofollow noreferrer">sed replacement</a> but that seems to go against the spirit of 'no variable template support', and I barely understand what sed does when I write it, so I pitty the next developer who has to read my commands.</p>

<p>I feel like for such a basic scenario "I want to build and deploy to my cluster" there might be a simpler solution that doesn’t feel like it’s fighting kubectl.</p>

<p>Skaffold does a great job of assigning a tag and digest for each built image, but it would be nice to have a simple process to start with before bringing in more high level tooling.</p>

<ul>
<li>Besides "Use Skaffold" or "Use Helm" what should I do to have a simple build + deploy process?</li>
<li>Is templating yaml files okay, either with sed, mo, or other replacement tools?</li>
<li>Are there any example projects that have a simple build and deploy pipeline using just kubectl and maybe kustomize?</li>
</ul>

<p><strong>kube.yaml</strong> (Note the lack of version on the image name)</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: example
  name: my-app
spec:
  minReadySeconds: 2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      name: my-app
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: "my-registry/my-app"
          ports:
            - containerPort: 3000
          resources:
            limits:
              cpu: 300m
              memory: 600m
            requests:
              cpu: 50m
              memory: 100m
</code></pre>

MaybeARabbit
<p>Is <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment" rel="nofollow noreferrer"><code>kubectl set image &lt;deployment&gt; &lt;image&gt;</code></a> an option? It does what you are looking for, but it won't be reflected in your local copy of <code>kube.yaml</code>, so you would need to specify a dummy image for "my-registry/my-app" in <code>kube.yaml</code> to not confuse other developers that they have to change that value each time they want to re-deploy. </p>
