How access variable declared earlier in same cloudbuild.yaml file

9/16/2018

I am new to devops and I am having the following problem:

I have cloudbuild file which configured to generate deployment for namespace named after tag.

For example: for tag v1.0.2/host/dev it should generate new deployment at "dev" namespace.

Here is part of cloudbuild.yaml code:

  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
    - '-c'
    - |
        export APP_VERSION
        export NAMESPACE
        export CLUSTER_NAME
        IFS=/ read -r APP_VERSION CLUSTER_NAME NAMESPACE <<< "$TAG_NAME"

        ... here is my problem ...
        export ENVIRONMENT
        export X
        export XX
        IFS=/ read -r X XX ENVIRONMENT <<< "$TAG_NAME"
        ...

As you can see - variable NAMESPACE is set with last part of tag (like "dev")

Later in code i need to set one more variable - ENVIRONMENT - with same value ("dev"). I did it by copy the way it done before and use some X and XX unused variables.

How can this be done in more accurate way? I tried:

export ENVIRONMENT=NAMESPACE

Or:

export ENVIRONMENT=${NAMESPACE}

Any ideas?

-- happyZZR1400
bash
kubernetes

1 Answer

9/16/2018

This should do it:

export ENVIRONMENT=${NAMESPACE}

Basically, you are doing an

export NAMESPACE

before the said assignment and in bash NAMESPACE should be available as a variable ${NAMESPACE}

-- Rico
Source: StackOverflow