DEV Community

Omar Diaaeldine Elwakeel
Omar Diaaeldine Elwakeel

Posted on

CI/CD pipeline, easy steps.

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:

  1. A code repo that is attached to Bitbucket or any similar code management system
  2. 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.

Image description

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.

Image description

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'
Enter fullscreen mode Exit fullscreen mode
  • 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.

Image description

Image description

Image description

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.

Discussion (0)