How to use SSH access via Bitbucket pipeline for deployment
Asked by Karim:

I am creating script for automatic testing and publishing. Everything works for me, except one thing. I can't do this command \`git fetch origin\` without using a password. I tried to set an access key in Bitbucket. I already set one in the server, but it doesn't work. Can you please help me? For clarity, I show you my script and my pipeline.

My Bitbucket pipeline:

```
# This is a sample build configuration for PHP.
# 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: php:7.2.24-fpm

pipelines:
default:
- step:
caches:
- composer
script:
- apt-get update
- apt-get install -y unzip libpng-dev libxml2-dev libmagickwand-dev libbz2-dev
- echo "memory_limit = 512M" > $PHP_INI_DIR/conf.d/php-memory-limits.ini
- pecl install imagick
- docker-php-ext-install bz2
- docker-php-ext-enable imagick
- docker-php-ext-install xml
- docker-php-ext-install mbstring
- docker-php-ext-install gd
- docker-php-ext-install zip
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install
- vendor/bin/phpunit
# custom:
# deploy-branch-develop-to-server:
# - step:
# name: Deploy to server branch develop
# script:
# - pipe: atlassian/ssh-run:0.2.6
# variables:
# SSH_USER: $USER
# SERVER: $SERVER
# SSH_KEY: $MY_SSH_KEY
# MODE: 'script'
# COMMAND: 'scripts/deploy-develop.sh'
# branches:
# develop:
# - step:
# name: Deploy to server branch develop
# script:
# - pipe: atlassian/ssh-run:0.2.6
# variables:
# SSH_USER: $USER
# SERVER: $SERVER
# SSH_KEY: $MY_SSH_KEY
# MODE: 'script'
# COMMAND: 'scripts/deploy-develop.sh'
# master:
# - step:
# name: Deploy to server branch master
# script:
# - pipe: atlassian/ssh-run:0.2.6
# variables:
# SSH_USER: $USER
# SERVER: $SERVER
# SSH_KEY: $MY_SSH_KEY
# MODE: 'script'
# COMMAND: 'scripts/deploy-master.sh'
```

<br />

My deployement script:

```
cd /var/www/vhosts/am-develop

sudo -u www-data cp .env.develop .env
sudo -u www-data git fetch origin # DOES NOT WORK!
sudo -u www-data composer install
sudo -u www-data npm run development
chown www-data:www-data -R .
service apache2 restart
service supervisor restart
php artisan elaborate:apps
wall "Deploy completed in production"
```


Answers:
author: Yevgen Lasman
body: You may try to use [Personal Access Token](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html#Personalaccesstokens-Usingpersonalaccesstokens) and pass it to git. The alternative is to generate a SSH key for the user which is running "git" command and [add it to your Bitbucket profile](https://confluence.atlassian.com/bitbucketserver/ssh-user-keys-for-personal-use-776639793.html).

comments: author: Karim
body: I read in both guides you proposed that you need to have Bitbucket Server, I am currently using an Ubuntu server to be able to host my application. Is there any other alternative? For clarity, I show you my script and my pipeline.

Pipeline:

```
# This is a sample build configuration for PHP.
# 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: php:7.2.24-fpm

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - apt-get update
          - apt-get install -y unzip libpng-dev libxml2-dev libmagickwand-dev libbz2-dev
          - echo "memory_limit = 512M" > $PHP_INI_DIR/conf.d/php-memory-limits.ini
          - pecl install imagick
          - docker-php-ext-install bz2
          - docker-php-ext-enable imagick
          - docker-php-ext-install xml
          - docker-php-ext-install mbstring
          - docker-php-ext-install gd
          - docker-php-ext-install zip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - vendor/bin/phpunit
#  custom:
#    deploy-branch-develop-to-server:
#      - step:
#          name: Deploy to server branch develop
#          script:
#          - pipe: atlassian/ssh-run:0.2.6
#            variables:
#              SSH_USER: $USER
#              SERVER: $SERVER
#              SSH_KEY: $MY_SSH_KEY
#              MODE: 'script'
#              COMMAND: 'scripts/deploy-develop.sh'
#  branches:
#    develop:
#      - step:
#          name: Deploy to server branch develop
#          script:
#            - pipe: atlassian/ssh-run:0.2.6
#              variables:
#                SSH_USER: $USER
#                SERVER: $SERVER
#                SSH_KEY: $MY_SSH_KEY
#                MODE: 'script'
#                COMMAND: 'scripts/deploy-develop.sh'
#    master:
#      - step:
#          name: Deploy to server branch master
#          script:
#            - pipe: atlassian/ssh-run:0.2.6
#              variables:
#                SSH_USER: $USER
#                SERVER: $SERVER
#                SSH_KEY: $MY_SSH_KEY
#                MODE: 'script'
#                COMMAND: 'scripts/deploy-master.sh'
```

Script:

```
cd /var/www/vhosts/am-develop

sudo -u www-data cp .env.develop .env
sudo -u www-data git fetch origin # DOES NOT WORK!
sudo -u www-data composer install
sudo -u www-data npm run development
chown www-data:www-data -R .
service apache2 restart
service supervisor restart
php artisan elaborate:apps
wall "Deploy completed in production"
```


Tags: bitbucket-server

git

server

ssh

ubuntu