Google Cloud Build deploy specific/current image tag

1/22/2020

I have the following Google Cloud Build pipeline:

# gcloud builds submit --config cloud-build/cloudbuild.yaml --substitutions=_GIT_USER="<your user>",_GIT_PASS="<your password here>,_GIT_TAG="<tag name>"
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 `<project_number>@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

deployment-backend.yaml

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/<my-project>/my-app-test:latest
...

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

step 3:

- 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', 
        '.']

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?

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.

Replacing the image tag with sed might work, but does not seem like a good solution.

-- Evgeni Dimitrov
google-cloud-build
google-cloud-platform
kubernetes

2 Answers

1/22/2020

Not sure in Kustomize, i think it will support environment variables finally.

But with Helm, it can now.

After you have the helm chart ready, when you need feed some values on fly, you can run below command

helm install --set GIT_TAG=${GIT_TAG} ...
-- BMW
Source: StackOverflow

1/29/2020

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)

kubectl set image deployment/my-deployment mycontainer=myimage

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

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

-- Lance Sandino
Source: StackOverflow