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

Use Deployment variables inside sh script Edited

My bitbucket-pipelines.yml file looks like this https://pastebin.com/MX5bXiuB

As one can see, in my bitbucket-pipelines.yml I use some Deployment variables, such as $SSH_USER, $SSH_HOST etc.

And my question is about that test-deploy.sh that I use to run some commands on my remote server during deploy process. I'd like to use that (or some other) pipelines Deployment variables (like $DEPLOY_DIR etc.) inside my test-deploy.sh so i could rule them all in one place. When I try 

cd $DEPLOY_DIR

inside my test-deploy.sh it does not work. Neither work 

- cat ./_ci/test-deploy.sh $DEPLOY_DIR | ssh -T $SSH_USER@$SSH_HOST

followed by

cd $1 

 

Any ideas of how this could be done are appreciated.

1 answer

1 accepted

0 votes
Answer accepted

Hi @vabard

This is not a problem with Pipelines environment, but a bash evaluation issue.
You are trying to send variables that are not evaluated to an SSH command.

The cat command will send the text inside the test-deploy.sh file. They are not variables until a terminal try to read them. The remote receiving them will not be able to interpret them because the variables are not declared there.

Also, the command below is not correct. 

- cat ./_ci/test-deploy.sh $DEPLOY_DIR | ssh -T $SSH_USER@$SSH_HOST

cat will just list the content of files, and in this case the $DEPLOY_DIR is not a file probably.


Suggestion

Can you try instead:

- eval ssh -T $SSH_USER@$SSH_HOST \"$(cat ./_ci/test-deploy.sh | tr '\n' ';')\"

Explanation:

  1. The cat command will list the content inside the test-deploy.sh file
  2. The tr command will remove the line ending and substitute them by ";" command separators.
  3. The section $(cat ./_ci/test-deploy.sh | tr '\n' ';') would result in:
    command1;command2 $VARIABLE;command3
  4. The scaped quotes will make it like:
    "command1;command2 $VARIABLE;command3"
    This will make the ssh command understand all of those commands as an argument.
  5. The eval command will evaluate all variables in the line. At the end you will have something like:
    ssh -T $SSH_USER@$SSH_HOST  "command1; command2 variable_value; command3"

 

You may find easier ways to solve this problem, this is just the solution I could implement now. I hope it helps.

Keep in mind that every time you mention a local variable in a script that will be sent to a remote host, you need to evaluate that variable before sending the content to the remote host.

Thank you @Daniel Santos , your snippet worked like a charm!

Like Daniel Santos likes this

You are welcome! =]

Suggest an answer

Log in or Sign up to answer
TAGS
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...

1,055 views 0 3
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