CI/CD pipeline, easy steps.
Tags: devops, webdev, beginners, automatio
Published: 2022-03-16 16:35:07

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w10ciw6vzk529v82hbpj.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87f4fmh75ox792xjwjea.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqjt1i05cuvkncei2lb8.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsfpst1srajfzkjvb5ff.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etg5fgmcmayvj5fomcvi.png)

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.