Getting started with Harness CI
Tags: devops, docker, kubernetes, harness
Published: 2022-11-17 15:36:30

## **What is Harness?**🐤
Harness is the industry's first Software Delivery Platform to use AI to simplify your DevOps processes - CI, CD, Feature Flags, Cloud Costs, and much more.

##  **Why Harness?**🐤
Harness is a modern software delivery platform that allows engineers and DevOps to build, test, deploy, and verify software, on-demand.

##**What is Harness CI?**🐤
1. Harness CI simplifies the development and testing of code.
2. Each stage includes steps for building,testing, and pushing your code.
3. CI executes steps as containers , packaging code, and dependencies in isolation from other steps. 

##**PreFlight Checklist!**🐤
1. [Forking the Repository](https://github.com/harness-community/python-pipeline-samples)✔️
2. Microsoft Azure Account✔️(Optional,You can use Harness prebuild)
3. A Docker Hub account✔️
4. Kubernetes cluster✔️(Optional,You can use Harness prebuild)

![CI pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibt19yu4w1481cfg01ta.png)

##**WorkFlow**🐤
- Build the code and run unit tests.
- Package the app as a Docker image and upload it to Docker Hub.
- Pull the uploaded image to the build farm as a Background 
  Task.Check out more about background tasks here
- Run an integration test against the sample app.

##**Step 1: Fork the repository**🐤
- Go to [Python-Pipeline-Samples Repo](https://github.com/harness-community/python-pipeline-samples)
- Now fork this repository in your Github [Forking a repository in Github](https://docs.github.com/en/get-started/quickstart/fork-a-repo)

##**Step 2: Create your Harness Project**🐤
- Move to the Harness Platform & click on Project -> New Project
- Configure the project settings as below
  `Name: (Default Project Name)`
  `Organization: Default`
 
 <img width="1792" alt="Screenshot 2022-09-09 at 10 24 17 AM" src="https://user-images.githubusercontent.com/109092049/192598400-7254737b-ff8b-4e1c-bdc8-6efd2895f011.png">

- Select CI Module in the modules sections

##**Step 3: Pipeline Creation & Configure Stages**🐤
- Click on `Pipelines` -> Create a Pipeline 
- Configure the pipeline as below
  `Name: CI-Python-Quickstart`
  `Set up pipeline as: Inline`
<img width="1792" alt="Screenshot 2022-09-09 at 10 32 57 AM" src="https://user-images.githubusercontent.com/109092049/192598502-92912fe7-3102-4b77-845c-ba914b684754.png">

To know more about Pipelines [check out the docs here](https://github.com/wishsagarks/python-pipeline-samples/blob/main/docs/overview.md)
Next, we are going to add Stages and steps to our Pipeline and compile our Python code.

Click on **Start**

##**Step 4: Build Step**🐤
- Hover over to Build tab.
- Click on `Add Stage` to get started with the pipeline creation
- Select the type of stage as `Build`
<img width="1792" alt="Screenshot 2022-09-09 at 10 53 12 AM" src="https://user-images.githubusercontent.com/109092049/192598696-a311f0b8-5c8e-46be-9a48-779d1c358bbb.png">
- Configure the Stage Settings as below
  - Name: `build test and run`
  - Make sure to turn on `clone codebase`.

**Setup the Connector as follows**
Select `Connectors` -> Click on `+ New Connector`
Connector Type: `Github`

<img width="1792" alt="Screenshot 2022-09-09 at 10 55 35 AM" src="https://user-images.githubusercontent.com/109092049/192598730-107417b7-4aa3-410b-9379-346b66d3e58d.png">
Name: `python-sample-connector`
URL Type : `Repository`
Connection Type: `HTTP`
GitHub Repository URL: Paste the link of your forked repository
Username: (Your Github Username)
Personal Access Token: [Check out how to create personal access token](https://dev.to/freaking_wish/cd-onboarding-with-harness-using-azure-2k04)
Secret Name: `Git-Token`
Secret  Value: PAT value generated in Github
Click on **Save**.

This will allow the repository to be fetched click on it and click `Apply Selected`
Make Enable API access (ON) with the secret token created
Click on **Connect through Harness Platform**.
To develop more understanding on Connectors [check out the docs here](overview.md)
Go the Infrastructure settings of stage and click on `Hosted by Harness`.
Then go to Execution (In this step we are going to compile the code)
#### Code Compilation
- Click on `Add step`
 - Go to `Build` and click on `Run`
 - Change the settings as following 
 - Name: `Code compile`
 - Container Registry -> Choose `New connector`
   - Click on `Docker Registry`
   - Change the settings as following 
       - Overview 
         - Name- `docker quickstart`
       - Details 
         - Docker registry url -  `https://index.docker.io/v1/`
         - Provider type - `Docker Hub`
         - Authentication - `Username and Password`
         - Username - Docker hub username 
         - Secret Token - [Check out how to create docker PAT](https://dev.to/freaking_wish/getting-started-with-harness-ci-16dn-temp-slug-3271428)
       - Connect to Provider 
         - Choose to connect through the Harness Platform
         - It will take sometime to verify your credentials.
  - Image: `python:3.10.6-alpine`
  - Shell: `Sh`
  - Command:
    `python -m compileall ./`
  - Then click `Apply changes`
Next we are going to create Image and Push the image to docker registry 
Click on **Create Image and Push to Docker Registry**

##**Step 5: DockerFile Creation**🐤
 - Click on `Add step`
   - Go to builds and click on run 
   - Change the settings as following:
      - Name: `create Docker file`
      - Container registry: Click on the Docker connecter created in the previous step 
      - Image: `alpine`
      - Commands: Copy the following command and click on apply changes.
         ```
         touch pythondockerfile
         cat > pythondockerfile <<- EOM
         FROM python:3.10.6-alpine
         WORKDIR /python-pipeline-samples
         ADD . /python-pipeline-samples
         RUN pip install -r requirements.txt
         CMD ["python" , "app.py"]
         EOM
         cat pythondockerfile
         ```
###Build and Push Image to Docker Registry
 - Click on `Add step`
 - Go to `builds` and click on `build and push an image to docker registry`
 -  Change the settings as following:
    - Name: `Build and push image to docker hub`
    - Docker connector: select the Docker connector you created previously 
    - Docker repository: `<docker-hub-username>/<docker-repository name>`
    - Tags: `latest`
    - In the optional Configuration
      - Dockerfile - ```pythondockerfile```
Now we move to Integration Testing and running our Pipeline.
Click on **Integration Test and Run Pipeline**

##**Step 6: Integration Testing**🐤
### Python server
Now you have a Stage to clone, build, containerize, and then push your image to Docker Hub. In this step you'll add a Stage to pull that image, run it in a container, and run integration tests on it.
- Click Add stage and select `build`
- Name it as `integration test`
- Turn off `clone from codebase` 
- Click `setup stage`
- Go to the `Stage overview` turn on `Clone Codebase`
- Now we will setup Stage Infrastructure
- Go to Infrastructure and select propagate from an existing 
  stage.
- Select the previous stage
- Click `Next`
In the Build Test and Push stage, you built your code and pushed your built image to Docker Hub.
Harness will pull the image onto the container in your infrastructure. Next, it will start the Hello World Server in the image.

Note:According to new flow and UI** Propagate from an existing stage** is already enabled.


   - Go to execution tab in run integration stage 
   - Select `add step`
   - Go to builds and select background 
   - Change the settings as following 
   - Name: `python server` 
   - Description(optional): `server connection`
   - Container registry: select the Docker connector you created 
     previously
   - Image : docker-hub-username/docker-hub-repo-name
          - Shell: `Sh`
          - Command: `python3 /python-pipeline-samples/app.py`
          - Select `Apply changes`

### Connecting to server
Next, we can run an integration test. We'll simply test the connection to the server.
- Select add step in the execution tab of run integration stage 
  - Go to Builds and select Run 
    - Change the settings as following 
       - Name: `test connection to server`
       - Container registry: select Docker connector you created .
       - Image: `curlimages/curl:7.73.0`
       - Commands:
        ```
        sleep 10
        curl localhost:5000
       ```
     - Select `Apply Changes`.

##**Step 7: Run the Pipeline**🐤
 - Click ↑Save.
 - Click `Run`. 
 - The Pipeline Inputs settings appear.
   - Under CI Codebase, select `Git branch`.
   - In Git Branch, enter the name of the branch where the codebase is, here ```main```
     - Click Run Pipeline.

**Hoooray we have completed our CI pipeline.**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57lgkpod099ynmr9rzmm.png)

**👌Happy hacking,see you until next time!अलविदा**
  About me:[freaking_wish](https://wishtree.vercel.app/) 😊
  doc ref:[Github repo](https://github.com/harness-community/python-pipeline-samples)