Missed Team ’24? Catch up on announcements here.

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

How to use Bitbucket Pipeline Variables within an SSH command

Rob Stemm January 29, 2019

I'm currently building a Bitbucket pipeline using pipeline variables so I can reuse the same pipeline across multiple projects. I have been unable to find how you use these variables in a SSH or SCP script within the pipeline. Below is an example.

Let's say I have the following three Deployment Variables in my Production Environment.

SERVER_IP => 192.168.10.21
SERVER_PORT => 1234
TEMP_DEPLOYMENT_LOCATION => my_temp_deployment_location

Now, let's say I would like to make a directory based on the above variables during the production deployment step in my bitbucket-pipelines.yml file. The SSH connection works, but the command to make the directory fails. It doesn't look like the variable is correctly evaluated.

ssh testuser@$SERVER_IP -p $SERVER_PORT 'mkdir -v ~/private/$TEMP_DEPLOYMENT_LOCATION/;'

Here is the log output by Bitbucket Pipeline when the above script is run.

+ ssh testuser@$SERVER_IP -p $SERVER_PORT 'mkdir -v ~/private/$TEMP_DEPLOYMENT_LOCATION/;'
mkdir: cannot create directory ‘~/private//’: File exists

What is the correct way to evaluate this pipeline variable in the above SSH command to create the following?

ssh testuser@192.168.10.21 -p 1234 'mkdir -v ~/private/my_temp_deployment_location/;'

 

1 answer

2 votes
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 3, 2019

Hi Rob,

At the moment, due to the way the quotes are set, the TEMP_DEPLOYMENT_ENVIRONMENT variable is trying to be retrieved inside of the remote machine, instead of from the pipelines environment. 

To instead reference the environment variable from Pipelines, the correct way appears to be using double quotes to wrap the command.

See: https://stackoverflow.com/questions/3314660/passing-variables-in-remote-ssh-command

Thanks,

Phil

Rob Stemm February 4, 2019

@Philip HodderThis solution worked for me. Thank you for your support.

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events