How can I use SSH in Bitbucket Pipelines to update files in server?

Hi Guys!

I'm struggling to make pipeline to work. I just want Bitbucket simply update the changes to my server.

 

I already installed git in my server. I can ony access my server via ssh. I also already included my Host address in SSH Keys, and Fingerprint is already being displayed.

 

Here is my current yml configuration:

# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: samueldebruyn/debian-git

pipelines:
  default:
    - step:
        script:
          - apt-get update
          - apt-get -qq install git-ftp
          - git ftp init --user $FTP_USERNAME --passwd $FTP_PASSWORD ftp://123.45.678.901/site.com/

Are the informations correct? What I'm missing?

I just want to update my server once I commit and push the files to bitbucket.

Can you guys help me?

1 answer

I haven't tried it the way your are yet, but I have had success using scp:

pipelines: 
default:
-
step: 
  script: 
- mkdir -p ~/.ssh
-
umask 077 - echo $MY_SSH_KEY | base64 --decode > ~/.ssh/id_rsa

- scp -i ~/.ssh/id_rsa -P $SERVER_PORT -r $DIRECTORY_TRANSFER_LIST $USERNAME@$SERVER_IP_ADDRESS:~/path/to/project
 

Where $DIRECTORY_TRANSFER_LIST is the name of the project directory you wish to transfer

Hi @Shane Fast

Thanks for your response. When I access my server, I have the following folders: Maildir , sitename.com and logs.

So based on your example, I should use:

- scp -i ~/.ssh/id_rsa -P $SERVER_PORT -r sitename.com $USERNAME@$SERVER_IP_ADDRESS:~/sitename.com

Is this correct?

Also should I use id_rsa or this is something that I also have to change?

Yes, based on the info you provided that appears to be the correct command.

as for id_rsa - its just the standard name for the key file. You can name it whatever you want as long as its the same name as the command preceeding it:

- umask 077 - echo $MY_SSH_KEY | base64 --decode > ~/.ssh/<name of file holding my private key>

- scp -i ~/.ssh/<name of file holding my private key> -P .....

Saving it into the .ssh directory and using umask 077 are required for setting the correct permissions on the key file. Without the correct permissions on the key file any remote commands or transfers will be rejected for built in security reasons.

Hi @Shane Fast

Thanks for your response once again.

Sorry for my many questions, I'm really a beginner on this. 

Regarding this line: echo $MY_SSH_KEY 

Should I include the SSH public key (a really big number), or should I use this variable ($MY_SSH_KEY) as it is?

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

638 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot