Search code examples
terraformgoogle-cloud-build

Cloudbuild - how to set a variable, looping multiples paths (folders)


i am trying to configure a variable in this case _PATH using cloudbuild. I've multiple paths (folders) on my github repo with tf files, and want to the terraform recognize any change that have been made on any folder at the moment to push and trigger.

I was wondering if there is any way to looping values ​​separated by "comma" on trigger options and then use "for" in bash script.., or perhaps exists another better way that i dont really know yet,

Thanks for the help!

code cloudbuild

cloudbuild sample


Solution

  • Sadly, I haven't found a way yet to set variables at the cloudbuild.yaml level.

    Note CloudBuild was originally called Cloud Container Builder, which is why it acts differently than other CI/CD tools.

    I think there may be another way to get the behavior you want though:

    1. Implement the bash looping logic in a script (ie. sh/run_terraform_applys.sh), and create a Dockerfile for it in your repo:
    FROM hashicorp/terraform:1.0.0
    
    WORKDIR /workspace
    
    COPY sh/                           /workspace/sh/
    COPY requirements.txt              /workspace/
    RUN pip install -r requirements.txt
    RUN . sh/run_terraform_applys.sh
    
    COPY .                            /workspace/
    RUN . sh/other_stuff_to_do.sh
    
    1. Use the cloud-builders image to build your image and as a consequence the docker build will run sh/run_terraform_applys.sh within the Docker image (You can push your image to GCR to allow for layer-caching):
      - name: 'gcr.io/cloud-builders/docker'
        entrypoint: 'bash'
        args: 
          - '-c'
          - |-
            # Build Dockerfile
            docker build -t ${MY_GCR_CACHE}  --cache-from ${MY_GCR_CACHE} -f cicd/Dockerfile .
            docker push ${MY_GCR_CACHE}
        id: 'Run Terraform Apply'
        waitFor: ['-']