Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

1 vote
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

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

716 views 15 10
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