How to automate the deployment system of your project with Deployer and Bitbucket pipelines
This guide can help you to create an automatic system to deploy your project without any other external software and customizable directly on your repository project (so you can version it).
- bitbucket account
- repository project on bitbucket and pipeline enabled
- ssh access on your server
Step 1. Create an ssh key and add it to bitbucket pipelines.
From the 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 ‘Repository variables Section’ of the bitbucket repository (pipeline tab).
Where output from files:
- my_ssh_key will be used in variable $BB_SSH_KEY.
- my_ssh_key.pub will be used in variable $BB_SSH_KEY_PUB.
So we have set our key as the variable of the bitbucket environment, and we do not need to copy it in any other place. This is useful for accessing our server directly for the pipeline.
Step 2. Add a public key to your server.
To add the public key to your server, execute the following command:
ssh-copy-id -i my_ssh_key.pub [email protected]
Step 3. Create a ‘known_hosts’ file for the pipeline.
ssh-keyscan -t rsa server.example.com > my_known_hosts
Please take it to a safe place because it will be used on the repository project.
If you need to have more information, follow the official bitbucket guide.
Step 4. Add the bitbucket-pipelines.yml file to the repository.
On the project root, add the bitbucket-pipelines.yml file with these steps:
image: debian:11.6 pipelines: branches: master: - step: script: - mkdir -p ~/.ssh - cat ./deploy/known_hosts > ~/.ssh/known_hosts - touch ~/.ssh/id_rsa && touch ~/.ssh/id_rsa.pub - (umask 077 ; echo $BB_SSH_KEY | base64 --decode -i > ~/.ssh/id_rsa) - (echo $BB_SSH_KEY_PUB | base64 --decode -i > ~/.ssh/id_rsa.pub) - chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa.pub - apt-get update - apt-get install openssh-client curl php php-cli php-curl -y - curl -LO https://github.com/deployphp/deployer/releases/latest/download/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 - dep -f=./deploy/deploy.php deploy production
With this example, the pipeline will be executed every time we make a ‘push’ against the branch master. It is possible to set many triggers with manual or automatic modes and different branches. You can find more in the official bitbucket guide.
Step 5. Add deploy folder.
To make a complete deployment with pipelines, we need deployment software. Deployer is a tool for deploying PHP applications to a remote server. This tool allows us to execute all commands directly on the server without actual access to the server. It is very simple, so you can learn how to use it directly on its official website https://deployer.org/.
Step 6. Make a deployment.
To run a deployment, make a new commit and push it on the branch master. Pipelines will be triggered without any manual action. On the right side of the commit list, you can see the job status of the pipeline (red or green).
This guide is not complete for a production system. Use as a test. Also, every project has different requirements, so this method can’t be a perfect tool. Be careful, and test it in on staging environment before switching to the production environment!