Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Use Deployment variables inside sh script

vabard
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 10, 2019 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
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 12, 2019

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.

vabard
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 4, 2020 edited

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

Like Daniel Santos likes this
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 7, 2020

You are welcome! =]

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events