Deployer + Bitbucket Pipelines: how to automate the deploy system of your project
Versione Italiana

Leggi questo articolo in Italiano: clicca qui


This guide can help you to create an automatic system to deploy you project without any other external software and customizable directly on you repository project (so you can versioning it).

REQUIREMENTS

  • bitbucket account
  • repository project on bitbucket and pipeline enabled
  • ssh access on your server

pipeline

DIFFICULT LEVEL

  • medium

1 - Create an ssh key and add it to bitbucket pipelines

From shell, just run:

ssh-keygen -t rsa -b 4096 -N '' -f my_ssh_key

Once you have ssh keys, execute these commands:

cat my_ssh_key | grep base64
cat my_ssh_key.pub | grep base64

Each output of the commands will be copied in the 'Environment Variable Section' of bitbucket repository (pipeline tab).

deploy-keys

So we have set our key as variable of bitbucket environment and we do not need to copy it in any other place. This is usefully to access on our server directly for the pipeline.

2 - Add public key to your server

To add public key to your server, just execute:

ssh-copy-id -i my_ssh_key.pub [email protected]

3 - Create 'known_hosts' file for pipeline

This file is usefully with pipeline because when we run the scripts we can't confirm questions via shell.

ssh-keyscan -t rsa server.example.com > my_known_hosts

Take it a safe place, because it will be used on repository project.

For more infos, just follow the official bitbucket guide: https://confluence.atlassian.com/bitbucket/access-remote-hosts-via-ssh-847452940.html

4 - Add bitbucket-pipelines.yml file to repository

On project root add bitbucket-pipelines.yml file with these steps:

image: debian:8.6

pipelines:
    branches:
        master:
            - step:
                script:
                    - mkdir -p ~/.ssh
                    - cat ./deploy/my_known_hosts >> ~/.ssh/known_hosts
                    - touch ~/.ssh/id_rsa
                    - touch ~/.ssh/id_rsa.pub
                    - (umask  077 ; echo $DEPLOY_SSH_KEY | base64 --decode -i > ~/.ssh/id_rsa)
                    - (echo $DEPLOY_SSH_KEY_PUB | base64 --decode -i > ~/.ssh/id_rsa.pub)
                    - apt-get update
                    - apt-get install curl git -y
                    - apt-get install php5 php5-cli -y
                    - curl -LO https://deployer.org/deployer.phar
                    - mv deployer.phar /usr/local/bin/dep
                    - chmod +x /usr/local/bin/dep
                    - curl -LO https://getcomposer.org/composer.phar
                    - chmod +x ./composer.phar
                    - mv ./composer.phar /usr/local/bin/composer
                    - composer install -d ./deploy
                    - dep -f=./deploy/deploy.php gmdotnet:deploy production

With this example, the pipeline will be execute every time we made a 'push' against branch master. Is it possibile to set many triggers with manual or automatic mode and different branch.

Official Guide: https://confluence.atlassian.com/bitbucket/branch-workflows-in-bitbucket-pipelines-856697482.html

This service is similar of more famous Travis CI, Codeship ecc.. In fact, we have a yaml file to config a docker image and execute any command.

Here the offical guide to make some test with docker: https://confluence.atlassian.com/bitbucket/debug-your-pipelines-locally-with-docker-838273569.html

5 - Add deploy folder

To make a complete deploy with pipelines, we need a deploy software. Deployer PHP is what we need to execute all command directly on the server. It's very simple, so you can learn how use it directly on its official website: https://deployer.org/

What the folder has inside:

  • composer.json -> additional 'receipe' for deployer php
  • deploy.php -> all the task that will be execute on the server
  • my_known_hosts -> file created on step 3 of this guide
  • server.yml -> server configuration (ip, user ecc...)

Here you are a complete example that you can use for your test: https://bitbucket.org/gmdotnet/pipelines-deployer

6 - Make a deploy

To run a deploy just make a new commit and push it on branch master. Pipelines will be triggered without any manual action. On the right side of commit list, you can see the job status of the pipeline (red or green).

commit-pipeline


Conclusion

This guide is not complete for a production system, just use as test. Also, every project as different requirements, so this method can't be a perfect tool. Be careful, and test it in on staging environment before switch on production environment!

Questions? Doubts?


Do you need help for your deploy system?

Contact Me
Blog Comments powered by Disqus.