<p>I have the following Google Cloud Build pipeline:</p>

<pre><code># gcloud builds submit --config cloud-build/cloudbuild.yaml --substitutions=_GIT_USER="&lt;your user&gt;",_GIT_PASS="&lt;your password here&gt;,_GIT_TAG="&lt;tag name&gt;"
steps:
# Git
- name: 'gcr.io/cloud-builders/git'
  args: ['clone', 'https://${_GIT_USER}:${_GIT_PASS}@bitbucket.my-company.com/scm/my-project/my-app.git',
         '--branch', '${_GIT_TAG}', '--single-branch']
# Build
- name: 'gcr.io/cloud-builders/mvn'
  args: ['package', '-DskipTests=true']
  dir: my-app/backend
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', 
        '--no-cache', 
        '-t', 'gcr.io/$PROJECT_ID/my-app-test:latest', 
        '-f', './cloud-build/Dockerfile-backend', 
        '--build-arg', 'JAR_FILE=./my-app/backend/target/my-app-0.0.1-SNAPSHOT.jar', 
        '.']
- name: "gcr.io/cloud-builders/docker"
  args: ["push", "gcr.io/$PROJECT_ID/my-app-test:latest"]
# Deploy
# The Deploy step requires the role 'Kubernetes Engine Developer' for the service account `&lt;project_number&gt;@cloudbuild.gserviceaccount.com`
- name: 'gcr.io/cloud-builders/kubectl'
  id: Deploy
  args:
  - 'apply'
  - '-f'
  - 'cloud-build/deployment-backend.yaml'
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_K8S_COMPUTE_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_K8S_CLUSTER}'
substitutions:
    _K8S_COMPUTE_ZONE: us-central1-a
    _K8S_CLUSTER: my-cluster-1
    _GIT_USER: my-git-user
    _GIT_PASS: replace-me-in-cloudbuild-file # default value
</code></pre>

<p>deployment-backend.yaml</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-backend
  template:
    metadata:
      labels:
        app: my-backend
    spec:
      containers:
      - name: my-backend
        image: gcr.io/&lt;my-project&gt;/my-app-test:latest
...
</code></pre>

<p>The problem is that in step 3 I have to build the image as <code>my-app-test:latest</code> so I can use again the latest image in the <code>deployment.yaml</code> (<code>image: gcr.io/&lt;my-project&gt;/my-app-test:latest</code>)
I would like to be able to use the tag name for the image tag like this:</p>

<p>step 3:</p>

<pre><code>- name: 'gcr.io/cloud-builders/docker'
  args: ['build', 
        '--no-cache', 
        '-t', 'gcr.io/$PROJECT_ID/my-app-test:${_GIT_TAG}', 
        '-f', './cloud-build/Dockerfile-backend', 
        '--build-arg', 'JAR_FILE=./my-app/backend/target/my-app-0.0.1-SNAPSHOT.jar', 
        '.']
</code></pre>

<p>but in that case what is the best way to tell the Deployment step to use the image named after the tag that is used?</p>

<p>I've found that Kustomize is the idiomatic way to "parameterize" kubernetes, but I still have to know the image name upfront and store it in a file.</p>

<p>Replacing the image tag with <code>sed</code> might work, but does not seem like a good solution. </p>

<p>Is there a reason you’re reapplying they deployment? Or are you doing that just one time? you could just use the built in command to replace / update the image instead of reapplying the config (if that’s what you’re doing)</p>

<p><code>kubectl set image deployment/my-deployment mycontainer=myimage</code></p>

<p>Or the other way is like you said, just use sed. (Basically what kustomize does) 
Bash into kubectl and then </p>

<p><code>cat deploy-file | sed “/latest/${_TAG_NAME}/“ | kubectl.bash apply -f -</code></p>
