Forums

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

Connect to a Remote Server with SSH and run commands with Pipeline Step

ethanbeyer
Contributor
June 30, 2019

I've done a lot of documentation and help-question scouring on SSH Keys and Pipelines today, but I am still stuck.

I want to...

  1. Connect to a remote server within a pipeline step with SSH
  2. Run commands on the remote server to fix permissions after SSH connection

Currently, in order to test/troubleshoot this, I have a custom step in my pipelines file:

fix-permissions:
- step:
name: "Fix Permissions"
script:
- apt-get update && apt-get -qq install git-ftp ssh
- ssh -p 22 -i ~/.ssh/pipelines_id $SSH_USER@$SSH_HOST "touch test"
  1. Notice I've added the specific port (22)
  2. I have no idea whether or not `~/.ssh/pipelines_id` is the path to the Bitbucket SSH Key

ssh error.png

This is the feedback I get from pipelines on the command. Even thought SSH_USER and SSH_HOST are set in my repository variables, and the fingerprint for that IP address was added to my Repo's known hosts, it cannot resolve the IP. It also seems to try to use port 21 instead of 22, which I cannot fix, either.

3 answers

1 accepted

1 vote
Answer accepted
ethanbeyer
Contributor
July 22, 2019

Here's the finished `.yml` file I used to solve this problem.

One of the first issues I had was that I had added the `:21` to the `$SSH_HOST` Repository Variable, which I missed.

After that, I needed to fix the path to the SSH Private Key, and then everything worked fine.

Here's the code:

# Fixes the Permissions on the Live Server
fixPermissionsScripts: &fixPermissionsScripts
- apt-get update && apt-get -qq install ssh openssh-client
- ssh -i ~/.ssh/config $SSH_USER@$SSH_HOST "sudo chown -R www-data:www-enabled $WEB_DIR"
- ssh -i ~/.ssh/config $SSH_USER@$SSH_HOST "sudo find $WEB_DIR -type d -exec chmod 2775 {} +"
- ssh -i ~/.ssh/config $SSH_USER@$SSH_HOST "sudo find $WEB_DIR -type f -exec chmod 0664 {} +"

image: awhalen/docker-php-composer-node:latest

pipelines:
branches:
release/*:
- step:
name: "Staging: Deploy"
caches:
- composer
- node
deployment: staging
script:
- apt-get update && apt-get -qq install git-ftp
- composer install
- npm install
- npm run dev
- git ftp push --user $FTP_USER --passwd "$FTP_PASS" --remote-root "$FTP_RR_STAGING" $FTP_HOST
after-script: *fixPermissionsScripts
master:
- step:
name: "Production: Deploy"
caches:
- composer
- node
deployment: production
script:
- apt-get update && apt-get -qq install git-ftp
- composer install
- npm install
- npm run dev
- git ftp push --user $FTP_USER --passwd "$FTP_PASS" --remote-root "$FTP_RR_PRODUCTION" $FTP_HOST
after-script: *fixPermissionsScripts
custom:
fix-permissions:
- step:
script: *fixPermissionsScripts
staging-init:
- step:
name: "Staging: Init"
script:
- apt-get update && apt-get -qq install git-ftp
- composer install
- npm install
- npm run dev
- git ftp init --user $FTP_USER --passwd "$FTP_PASS" --remote-root "$FTP_RR_STAGING" $FTP_HOST
after-script: *fixPermissionsScripts
production-init:
- step:
name: "Production: Init"
script:
- apt-get update && apt-get -qq install git-ftp
- composer install
- npm install
- npm run dev
- git ftp init --user $FTP_USER --passwd "$FTP_PASS" --remote-root "$FTP_RR_PRODUCTION" $FTP_HOST
after-script: *fixPermissionsScripts
ethanbeyer
Contributor
July 22, 2019

I'm real active on this thread, but I'd like to say, to the Bitbucket Team...

 

PLEASE allow use of inline `<code>` syntax.

And let the "Code Block" accept Spaces as indentation.

Like Manish Patel likes this
1 vote
ethanbeyer
Contributor
July 3, 2019

paging @Philip Hodder -- seeing your answers to other questions, I am confident you can help!

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 22, 2019

Hey @ethanbeyer 

You should be able to use the SSH run Pipe to get this working quickly!

You can read full documentation on Pipes here: https://confluence.atlassian.com/bitbucket/pipes-958765631.html

Thanks,

Phil

Like piraz likes this
ethanbeyer
Contributor
July 22, 2019

Thanks! I hadn't seen that, so I appreciate it!

In the time since I posted this question, I brute-forced the solution - I will post my code in a top-level comment to accept the answer for posterity.

0 votes
ethanbeyer
Contributor
July 2, 2019

Does anyone have any input? I am still stuck on this.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events