Omar Elwakeel
Posted on March 16, 2022
This article to demonstrate how to build your own CI/CD pipeline.
The next steps and procedures can be used on other code repo management systems and other deployment platforms, So Bitbucket and Heroku are just examples.
First of all you should have the following or similar:
- A code repo that is attached to Bitbucket or any similar code management system
- A platform to deploy code on, in my case I'm going to use Heroku
First to access your Heroku App you should have an API Key like any other platform, In case of AWS you have to have Access keys, You have to put this or these keys in the repository settings.
Second if you have variables that are related to the environment itself like the Heroku app name, you can state them in the deployment section.
In your code base you should have a file with the following name bitbucket-pipelines.yml
, the name of the file will differ based on the code repo managamenet system you are using, however this file should contain code similar to this
image: node:10.15.3
pipelines:
default:
- step:
script:
- echo $BITBUCKET_BRANCH
branches:
dev:
- step:
name: Create artifact
script:
- git archive --format=tar.gz $BITBUCKET_BRANCH -o application.tar.gz
artifacts:
- application.tar.gz
- step:
name: Deploy to heroku
deployment: dev
caches:
- node
script:
- pipe: atlassian/heroku-deploy:1.1.4
variables:
HEROKU_API_KEY: $HEROKU_API_KEY
HEROKU_APP_NAME: <App-Name>
ZIP_FILE: 'application.tar.gz'
WAIT: 'true'
stage:
- step:
name: Create artifact
script:
- git archive --format=tar.gz $BITBUCKET_BRANCH -o application.tar.gz
artifacts:
- application.tar.gz
- step:
name: Deploy to heroku
deployment: dev
caches:
- node
script:
- pipe: atlassian/heroku-deploy:1.1.4
variables:
HEROKU_API_KEY: $HEROKU_API_KEY
HEROKU_APP_NAME: <App-Name>
ZIP_FILE: 'application.tar.gz'
WAIT: 'true'
- All line of codes pre-fixed with $ are variables that will be determined in runtime.
- BITBUCKET_BRANCH is the current branch that had the currect change.
- The image is the image of the container responsible for deploying the code and doing the following steps, in here will be node version 10.15.3
- default pipeline is a pipeline that will execute no matter what branch is active.
- branches will define the code and steps to take place based on the active branch
- App-Name this will be your application name that will depend on the environment your are building on, testing, dev, staging, production.
Whenever your trigger this pipeline by pushing the code to branch or trigger it manually, this will happen.
And viola your code is deployed automatically, This can be better and more efficient if you have a testing phase before building the code, then you will add the script responsible for running the test cases before the build script, and the build script will only run if the test step is successful.
Posted on March 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.