0

I'm trying to create multiple deployments using Cloud Build and Cloud Deploy in GKE cluster. Below is my cloudbuild.yaml

steps:
- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: 'bash'
  args: 
  - '-c'
  - |
    while IFS= read -r deplname; do
      sed -i "s/service/$$deplname/g" ./test/deployment.yaml
      release_name=$$(echo "build-$$RANDOM")
      gcloud deploy releases create $$release_name --project=PROJECT_ID --region=us-central1 --delivery-pipeline=test-cloud-deploy --source=. --images=test-cloud-deploy=us-central1-docker.pkg.dev/PROJECT_ID/cicd-test/test-image:v1
    done < ./consumer_names.txt
options:
 logging: CLOUD_LOGGING_ONLY

my consumer_names.txt looks like,

deployment-name1
deployment-name2

deployment.yaml looks like,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service
spec:
  selector:
    matchLabels:
      app: service
  template:
    metadata:
      labels:
        app: service
    spec:
      containers:
      - name: service
        image: nginx
        ports:
        - containerPort: 80

But it's creating only one deployment that is deployment-name1 not second. Is there any way to create multiple deployments using cloud build and cloud deploy ?

Rahul
  • 402
  • 2
  • 9
  • 23
  • You are missing a ; at the end of the gcloud deploy line – boredabdel Sep 22 '22 at 14:54
  • @boredabdel adding `;` at the end of line `gcloud deploy ...` doesn't make any difference. Only one deployment got created. Any suggestion ? – Rahul Sep 22 '22 at 14:59
  • The semi-colon should make no difference because you're using YAML pipe (`|`) so everything that follows is treated as multi-line. None of the semi-colons is needed. It's curious that you create `deplname` but don't use it so you make it more difficult to debug that it's being read correctly. It's also curious that you `release_name=$$(echo "build-$$RANDOM")`, `release_name="build-$$RANDOM"` should suffice. If you `printf "%s: %s" ${deplname} ${release_name}` you'll ensure that you're iterating and generating unique `release_name`s – DazWilkin Sep 22 '22 at 15:46
  • And... you should reference `release_name` "locally" (i.e. `${release_name}` **not** `$$release_name`) within the script because it's **not** being substituted by Cloud Build but by the shell. – DazWilkin Sep 22 '22 at 15:53
  • Sorry typo `release_name="build-${RANDOM}"` too. Not being substituted by Cloud Shell but the local shell. – DazWilkin Sep 22 '22 at 15:56
  • @DazWilkin I have used `deplname` in cloudbuild.yaml it was missed here. I have updated question again. – Rahul Sep 22 '22 at 17:25
  • @DazWilkin my ultimate goal is to deploy multiple deployment in GKE cluster using single template. Is there any better way to do that ? – Rahul Sep 22 '22 at 17:45
  • Ah, I see. Did you apply the other changes I proposed? That should get you to a working config. I think your script isn't working correctly and you're not seeing that it doesn't work because you don't log (e.g. `echo`) state. The changes should get you to a working script. – DazWilkin Sep 22 '22 at 17:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248270/discussion-between-rahul-and-dazwilkin). – Rahul Sep 22 '22 at 18:11
  • Please update the scripts with the recommendations and try that. Your `sed` for the Deployment is incorrect too. You don't want to use `$$VARIABLE` for variables that are defined **within** the script. `$$VARIABLE` is used to tell Cloud Build to substitute variables that you provide when you invoke Cloud Build. – DazWilkin Sep 22 '22 at 18:18

0 Answers0