Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Pipelines: Error using echo to create file

mdiastecnologia March 9, 2021

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.

1 answer

0 votes
Eddie Webbinaro
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 9, 2021

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



mdiastecnologia March 9, 2021

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

Eddie Webbinaro
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 9, 2021

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?)

mdiastecnologia March 9, 2021

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

Eddie Webbinaro
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 9, 2021

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!


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.


I personally would either debug locally or 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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events