Pipelines: Error using echo to create file
Asked by mdiastecnologia:

I'm having a problem when executing automatic pipelines (from a commit in a branch) with this code block below.

```
echo $GCLOUD_PROD > auth_base.json | base64 -d auth_base.json > auth.json | gcloud auth activate-service-account --key-file auth.json
```

It always fails. When a manually start it from GUI it works.

Can anyone help me?

Tks.


Answers:
author: Eddie Webbinaro
body: Hmm.

```
echo $GCLOUD_PROD > auth_base.json | base64 .....
```

I'm not understanding how this works anywhere...  

The first io redirect \`\>\` would redirect all stdout to that file, so only stderr would be passed through the pipe to base64 and subsequent commands.  

Also, to get more accurate help, what is the purpose of the 2 auth files?   

Typically I see base64 decoding of environment variables handled like this, with any subsequent commands broken to their own line to use the decoded file.

```
 script: 
   - echo $GCLOUD_PROD | base64 -d > auth.json
   - gcloud auth activate-service-account --key-file auth.json

```

<br />

<br />

comments: author: mdiastecnologia
body: Eddie,

It?s working (when started manually) in a bunch of projects. See the attached image.

That's because de subsequent commands do not need the stdout and/or stderr.

They are working with the file created.

I divided it into two files only to make more clear the process.

First, get the base64 env and create a file, after convert that base64 file to a plain text file.

And, so then, log into gcp.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/133704i6D52FCC6688363E7/image-size/large?v=v2&px=999 "image.png")


author: Eddie Webbinaro
body: OK, I see, so you're just using pipes to chain the commands? I've not seen that pattern.  

Anyway, can you send a failure example? I see your working example is a **deployment**, is it possible the Variable is not available to your automatic runs? (which may not be specifying an environment to access environment specific variables?)


author: mdiastecnologia
body: I did not specify env for a specific environment. That is useful, can you point me a doc to do that?

Below is my script:

```
release/ecare-master:
- step:
name: Build image
script: # compila a imagem e sobe ela para o reposit?rio de imagens
- echo $GCLOUD_PROD > auth_base.json | base64 -d auth_base.json > auth.json | gcloud auth activate-service-account --key-file auth.json
- export IMAGE_NAME=gcr.io/hcms-prod/hcms-salessurvey-frontend:$BITBUCKET_COMMIT
- docker build -t $IMAGE_NAME --build-arg REACT_APP_BASE_URL='' --build-arg REACT_APP_TOKEN_SECRET=hcmsjsonwebtokensecretapiPPRDO --build-arg REACT_APP_BASE_URL_SURVEY=/survey --build-arg REACT_APP_KIBANA_URL=https://1a97ebd98d4047208c895b2c2b2f3828.southamerica-east1.gcp.elastic-cloud.com:9243 .
- gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://gcr.io
- docker push $IMAGE_NAME
```

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/133720iD26E02E5F6D02BDF/image-size/large?v=v2&px=999 "image.png")


author: Eddie Webbinaro
body: Huh.  

For the environment variables:
> Deployment variables override both team and repository variables. Also variables with the same name can have different *values* for each deployment environment. For example, you could set a different $DEPLOYMENT_SECRET_KEY for each environment

<https://support.atlassian.com/bitbucket-cloud/docs/set-up-and-monitor-deployments/>  

For the error... I'm stumped!

<br />

base64 is throwing the error, not finding file. So my mind goes to the previous command, echo. Echo's simplicity has me wondering about the variable.  

<br />

I personally would either [debug locally o](https://support.atlassian.com/bitbucket-cloud/docs/debug-pipelines-locally-with-docker/)r try to isolate cause with lots of debug statements to isolate cause.

* break each command to their own line, getting their own error
* download the full log, sometime snot all message show in UI
* add debug commands like "echo $GCLOUD_PROD" without redirect (ensure variable is set to secure in repo settings to mask it.
* add ls command after echo to make sure it was written.


Tags: bitbucket-pipelines