This template can be used for the setting up the simplest CI process using Bitbucket Pipelines.
This CI process is used for the creation separate environment on the server for the each branch. The links to the built environments are created using xip.io service.
The flow was created for the following stack on the server: Ubuntu, Apache, MySQL.
The idea of this template is creation separate environments on the server for each active branch of the project's repository. It may be useful for the testing features independently or for the setting up the deployment process.
- You need to create a user on the server or use existing one, which has enough permissions to create folders, add virtual hosts, and reload Apache.
- Save user's credentials into ./project/ci/config.sh file.
- Change SCRIPTS_DIR variable to the folder, which will contain the build scripts.
- Generate you local pair of the RSA keys:
ssh-keygen
- Copy the content of the private key into ./project/ci/id_rsa_ci:
cp ~/.ssh/id_rsa ./project/ci/id_rsa_ci
-
Add your public key to the server.
- Get content of your public key:
cat ~/.ssh/id_rsa.pub
- Login to the server, open/create file ~/.ssh/authorized_keys.
- Add the content of id_rsa.pub into authorized_keys.
More information about SSH keys can found into Set up an SSH key page.
You may need some specific DB user for the manipulations with the databases or use the existing one.
- Create new user:
CREATE USER 'ci'@'localhost' IDENTIFIED BY 'ci_pass';
GRANT ALL PRIVILEGES ON *.* TO 'ci'@'localhost';
- Save DB user's credentials into ./server/config.sh file.
More information about adding users account can be found on the official documentation page.
- Create RSA keys on the server.
- Add access key to the repository according to the documentation.
- Set up Slack Incoming Webhook according to the documenation.
- Save channel name and webhook URL into ./server/config.sh file.
Open ./server/config.sh file, and set up the variables:
- REPO - Bitbucket repository into SSH format: [email protected]:account/repo.git;
- SITES_ROOT - the directory where you host your sites;
- BUILDS_ROOT - the subdirectory used for the builds;
- DB_USER - CI DB user name;
- DB_PASS - CI DB user password;
- DB_NAME_LENGTH - the length of the generated databases' names;
- SLACK_CHANNEL - Slack notification channel;
- SLACK_WEBHOOK - Slack incoming webhook URL;
- SLACK_USERNAME - Slack notification user name;
- SLACK_ICON - Slack notification icon;
- DOMAIN_NAME - top-level domain name;
- SITE_TITLE - site title.
- Upload all files from the ./server/ folder to the directory defined in SCRIPTS_DIR variable of the ./project/ci/config.sh file.
- Set up the executive permissions on the files build.sh and cleanup.sh.
- Copy the content of ./project/ folder to the root of your project (master branch) and push in to the repository.
- Open Pipelines tab in Bitbucket and enable the pipelines.
- Set up cron job on the server, which executes cleanup.sh script.
$ crontab -e
0 */6 * * * /root/cleanup.sh
Crontab rules can be configures using Crontab.guru service.