Unable to access environment variables in GCP artifact after using Bitbucket deploy pipeline
Asked by Lars Nedberg:

Hi,

Not sure if I have misunderstood how things work here, but I want to pass variables from my local .env-files to my artifact whenever I do automatic deploy with bitbucket pipelines.

When running or deploying manually, I have an \`.env\`-file present locally, which I obviously don't want to commit.

The build pipeline succeeds and it deploys a new version to GCP, but this version does not work, since it is unable to retrieve database configuration secret from my environment file. So it is practically useless.

I have set up bitbucket-pipelines.yml with the atlassian gcp-deploy-pipe. Relevant parts of file looks like this:

```
branches:
  develop:
    - step:
        name: Deploy
        deployment: test
        script:
          - echo "Deploying to GCP"
          - pipe: atlassian/google-app-engine-deploy:1.0.0
            variables:
                KEY_FILE: $GCP_SECRET
                PROJECT: 'test-123'
                DEPLOYABLES: 'test.yaml'
                PROMOTE: 'false'
                STOP_PREVIOUS_VERSION: 'false'

                MONGODB_CONNECTION_URI: $MONGODB_CONNECTION_URI
                <some more variables>
```

I have added both these variables, $GCP_SECRET and $MONGODB_CONNECTION_URI, to my deployment repository config inside bitbucket.

The GCP_SECRET works correctly, as I am allowed to push a new version to GCP. But clearly I have misunderstood how I should pass my actual environment variables when deploying like this.

---  

I have also tried to add the variables inside the \`test.yml\`, listed in det DEPLOYABLES, and also added DB-url to GCP secret manager, but still no luck obtaining db connection.

```
runtime: nodejs
env: flex
instance_class: B1
manual_scaling:
  instances: 1
env_variables:
  NODE_ENV: "test"
  MONGODB_CONNECTION_URI: $MONGODB_CONNECTION_URI
```

Any ideas on how to fix this will be greatly appreciated! Thank you!

EDIT: I try to access the variables in code like the following:

```
process.env.MONGODB_CONNECTION_URI
```

<br />

<br />

<br />

Bonus question on the side:

Is it possible to check health status of artifact pushed to GCP, and use result from healthcheck to determine if whole Bitbucket pipeline failed. This would be useful in this exact scenario, so I don't push gibberish to my test environment.


Answers:
author: Igor Stoyanov
body: [@Lars Nedberg](/t5/user/viewprofilepage/user-id/4497838) Hi. Thanks for your question. Please, refer to this repo example: [envsubst example repo](https://bitbucket.org/bitbucketpipelines/example-google-gke-kubectl-run/src/master/helloweb-deployment.tpl.yaml)  
Its a good case to use [envsubst](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) : you need to have [template](https://bitbucket.org/bitbucketpipelines/example-google-gke-kubectl-run/src/master/helloweb-deployment.tpl.yaml) to pass variables here and in your [bitbucket-pipelines.yml](https://bitbucket.org/bitbucketpipelines/example-google-gke-kubectl-run/src/master/bitbucket-pipelines.yml) you need to add code below before pipe executes:  

```
script:
 - envsubst < test.template.yaml > test.yaml
```

<br />

You can look at additional information here: [additional info](https://community.atlassian.com/t5/Bitbucket-Pipelines-questions/replace-the-image-value-in-kubernetes-yaml/qaq-p/1756883)   

Regards, Igor

comments: author: Lars Nedberg
body: Hi Igor,

Thank you so much for your reply. I didn't quite get the concept of the \`test.template.yaml\` file.

However, I found [this](https://medium.com/@jorn.verhaegen/environment-variables-with-google-app-engine-and-ci-cd-pipelines-e28b2c038a95) guide, which ultimately made everything work for me.

Basically I define a shell script in my repository, then runs the script inside the bitbucket pipeline, and populating my secret values with my bitbucket repository variables.

Thank you for your time and help! :)


Tags: bitbucket-pipelines

cloud

google

google-app-engine