It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Replacing variables inbetween quotes in a linux command

I'm using this command:
- sed -i "s/api_token_placeholder/$PROD_API_KEY/g" backend/file.xml

Where $PROD_API_KEY is a variable configured in bitbucket env variables config...

The key is not being replaced... help!!!

2 answers

0 votes
Linette Atlassian Team Jul 04, 2018

You might need to escape the variable name with {} (see 

https://stackoverflow.com/questions/17477890/expand-variables-in-sed)

I can’t test that at the mo (replying on a phone) but figured a quicker answer might be of assistance rather till I have my setup in front of me ;) 

Linette Atlassian Team Jul 04, 2018

Ack, no, that's not it, I just tested in my own pipeline, and the variables replaced fine in my repo, that said it might depend on which docker image you are using.

So first, I'd probably recommend troubleshooting by testing your image and command locally (remember to use -e "PROD_API_KEY=123" to your docker run command if you do this).

 

Whether you test locally or in bitbucket debugging steps might include adding a few commands like:

cat backend/file.xml (check that it's looking in the right place for the file and can read it)

echo $PROD_API_KEY (make sure the value is readable)

 

hope that helps!

Hi Linette, thanks for your answer, but if I hardcode the API key it works. If I use the variable in other places it's replaced correctly.

Does having the key as an encrypted key has anything to do with whats happening?

Thanks,

Linette Atlassian Team Jul 05, 2018

Ooohh... mysterious!

It does sound like something odd is happening with the sed command itself.  Ooooor, it might also be something odd happening with quoting elsewhere in your script having a knock on effect.

Maybe try: *fingers crossed*

- sed -i 's/api_token_placeholder/'"$PROD_API_KEY"'/g' backend/file.xml

Note that I've closed the single quotes before double quoting the variable, and then re-opened them for the /g.

If this doesn't work, I have some questions:

When you say encrypted do you mean a secure variable (which should have no difference) or base64 encoded like we do for SSH here ? If the later, you'll have to decode the encryption.

And does it not work at all? or is your api_token_placeholder replaced with the literal string of '$PROD_API_KEY'?

ha! Testing it like this on a mac os or linux terminal doesn't work...

PROD_API_KEY=blabla sed "s/api_token_placeholder/$PROD_API_KEY/g" backend/file.xml

I've tried all the different combinations... 

And answering your question, it was encrypted as a secure variable.

 

Thanks!

Like Gurmukh1991 likes this
Linette Atlassian Team Jul 08, 2018

Yeah, a direct mac or linux terminal will not quite behave in the same way as a docker container, which is why we recommend when testing locally you use docker (https://confluence.atlassian.com/x/IQr3MQ)

and how about my second question:

And does it not work at all? or is your api_token_placeholder replaced with the literal string of '$PROD_API_KEY'?

That said, it might be time to open a case with support, they could work realtime with you. I imagine you must be quite frustrated with the delay by now!

This is how we do it for https://battlemaps.co .  Repository variables can be encrypted or unencrypted.  It just seems easier with good old m4.

 

# install deps
- apt-get update && apt-get install -y m4
- which m4
- echo 'I am a TEST_VAR' | m4 -DTEST_VAR="database name"

- mv src/main/resources/config/application-prod.yml src/main/resources/config/application-prod.m4
- m4 -DM4_TWITTER_CLIENT_ID=${TWITTER_CLIENT_ID} -DM4_TWITTER_CLIENT_SECRET=${TWITTER_CLIENT_SECRET} src/main/resources/config/application-prod.m4>src/main/resources/config/application-prod.yml

 

Hope it helps!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket Pipelines

What We Learned When We Researched Open Source Vulnerabilities in 7 Popular Coding Languages

...hey are a part of us, shaping how we interact with the world around us. The same holds true for programming languages when we think about how different kinds of vulnerabilities raise their heads in t...

155 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you