Blog-logo

How to setup deployment Pipeline with Bit bucket

May 27, 2020

Slider

In past while developing , Developers has to commit the code and then login to the deployment server and pull the changes , this has been the way of pull the changes on the remote server , But with the tool like Bit-bucket now you can set up a pipe-line to deploy and test your code before going to the server ,

So to setup first we need to add bit-bucket ssh key to the our server authorized keys

PS : if the below screen is not visible under pipelines , go to o repository settings and under pipeline heading click settings and enable pipeline 

Go to Your Repository -> Repository Settings -> SSH keys ( Under Pipe lines )

Bit-bucket SSH keys

In here you have to do two things

first enter your server host name /domain name ( without https or www ) here and click fetch to get a host’s fingerprint.

Secondly

Click on Generate New Keys and copy the public key

Then go into your server

Enter the following command and add the public key to your authorized keys

nano ~/.ssh/authorized_keys

now bit bucket can access your server with out a password .

Now lets setup pipeline in bit bucket

For that go to your repo and click on pipe-lines

How to create a pipeline in bit-bucket

How to create a pipeline in bit-bucket

and add the following lines to the file and commit to your branch

image: php:7.3
pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - apt-get update && apt-get install -y unzip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - vendor/bin/phpunit
          - apt-get update && apt-get install -y unzip
          - apt-get install -y openssh-client
          - ssh root@yourdomain.com 'bash -s' < deploy.sh
          - echo "Deploy step finished"

This bitbucket-pipelines.yml is setup for a PHP based Laravel Project

Important thing to change in this file is

ssh root@yourdomain.com add your domain or ip address where you use your normal ssh to connect with .

and also need to create a deploy.sh bash file in your local repository where you mention the steps you want to execute when the bit bucket connects to your server when there is a new git

add the following to your deploy.sh file

echo "Deploy script started"
cd /var/www/projectfolder
echo "directory Changed"

# Turn on maintenance mode
php artisan down

# Pull the latest changes from the git repository
git pull origin master

# Install/update composer dependecies
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev

# Run database migrations
php artisan migrate --force

# Clear caches
php artisan cache:clear

# Clear expired password reset tokens
php artisan auth:clear-resets

# Clear and cache routes
php artisan route:clear
php artisan route:cache

# Clear and cache config
php artisan config:clear
php artisan config:cache

# Turn off maintenance mode
php artisan up
echo "Deploy script finished execution"
exit


PS: I have personally faced this issues when bitbucket tries to execute git pull command in the server through deploy.sh

the error was

fatal: could not read Password for ‘https://username@bitbucket.org’: No such device or address

So temporary solution found for this is

was to cache the pass word for a  month

git config --global credential.helper 'cache --timeout=2628000'

this is not a very realiable solution so once we find a answer to this we will update the post but this works fine for now .

Once this step is done , your pipeline integration is completed , now when ever you push new changes to bit-bucket the pipeline you setup will get triggered and you will be able to see what has happened with the new build .