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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to use SSH access via Bitbucket pipeline for deployment

Edited

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'


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"

1 answer

You may try to use Personal Access Token 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.

  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"

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,850 views 10 16
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you