CD onboarding with Harness using Azure.
Tags: azure, devops, kubernetes, docker
Published: 2022-11-07 09:23:36

## **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 Cloud Delivery Pipeline (CD Pipeline)?**⚓
A CD Pipeline is a series of Stages where each Stage deploys a Service to an Environment.A CD Pipeline can perform many additional CD operations, including, but not limited to:
-   Propagating Services and their definitions across Stages
-   Approvals using integrations like Jira and ServiceNow.
-   Synchronizing stage deployments using barriers.
-   Notifications.

##Aim: Setting up and running the pipeline which can run an Nginx image on your cluster with **Harness** using Microsoft **Azure**!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzu4dmpqee9x1egzmw2u.png)

##**PreFlight Checklist!**⚓
1. [Forking the Repository](https://github.com/harness-community/cd-pipeline-sample)✔️
2. Microsoft Azure Account✔️
3. A Docker Hub account✔️
4. Kubernetes cluster✔️
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89tgtohfx7locwxbghpk.png)

##**Steps to set up Azure-kubernetes cluster**⚓
1. Log in to [Microsoft Azure Portal](https://portal.azure.com/),using student id gives you free credit.
2. Search for Kubernetes Services.
3. Click on **Create**.Then click on **Create Kubernetes cluster**.
4. Fill up all the necessary details like Kubernetes cluster name-demo(example).
5. Finally Click **Review+Create** button then **Create** and give it some time to Initialize deployment.You will be notified once it's deployed.
6.Click **connect** and get ready for using Harness.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqaf3osg1e4z8t5ouv48.png)

##**Steps to set up the Harness Delegate**⚓
1. Log in to your [Harness Account](https://harness.io/).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqcq5y7mus93hu06wsn6.png)
2. In The Project Setup tab choose **Delegates** and hit **+ 
   Create a Delegate** after **setting up project:firstproject**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2zkygutmxwq87m5yfzq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncd04zs3fjhdcyhooih0.png)
3. Choose **Kubernetes** and continue.
     Enter the following details: 
   - Delegate Name:newdelegate
   - Delegate Size:Small
   - What installer do you want to use?:Kubernetes
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s971k32lk5vwr4lsppf3.png)
4. Press **Continue**.
5. **Download YAML file** and then Continue
6. Open Kubernetes Cluster tab and Press **Connect** then **Open 
   Cloud Shell**
7. Upload the YAML file in the cli or copy the content of YAML file to cli of Cloud Shell 
8. Enter the following code
```
kubectl apply -f harness-delegate.yml
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mztl4f748lwa5f1lcpvl.png)
   Press **Done** once you see green check mark in Checking for 
   Heartbeat section in Harness tab.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7xful0yauq9jns4sc34.png)

##**Secrets and steps to set them up**⚓

  **<u>GitHub Personal Access Token</u>**
Harness needs a Personal Access Token to access GitHub repositories.

1. Login to **GitHub** and click on **Settings** from the top-right menu
2. Select **Developer Settings** from the list of **Account Settings**
3. Select **Personal access tokens->Token(Classic)->generate new Token**
4. Click on Generate new token (**grant ALL permissions**)
5. Copy the generated token and save it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iogcg8dmao8d9sjf3614.png)
6. Now go to Harness Secret Tab
7. **Click New Secret->Text**
8. Fill up the details and copy the secret key from github token generated and paste it in harness secret just created.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4wor8ys69bisq4la22n.png)

 **<u>DockerHub Access Token</u>**
Sign-in to Docker Hub
 Go to **Account Setting** then **Security** and generate **New 
 Access Token** and generate.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a29sv5z4szn3bpxdrmuz.png)
1. Now go to Harness Secret Tab and click New Secret->Text
2. Fill up details and paste the key generated from docker hub and hit save.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evxkaq6gu2jdzuxu2iae.png)

## **Steps the to set up GitHub Connector**⚓
   In Harness Dashboard you will notice Connector tab under your 
   project setup section.
1. Click **+ New Connector** and choose Github from **Code 
   Repositories**
   - Name:githubconnector
   - GitHub Account URL: YOUR_GITHUB_URL
   - Test Repository:cd-pipeline-sample and **Continue**.
   - Username:YOUR_GITHUB_USERNAME
   - Personal Access Token:choose your github secret created in 
     harness
   - Check the Enable API access (recommended)☑️
   - Personal Access Token:choose your github secret created in 
     harness and **Continue**
2. Select how you would like to connect to the provider:Connect 
   through a Harness Delegate
3. Choose the Delegate you created-newdelegate then **Save and Continue** and **Finish**.

## **Steps the to set up Docker Connector**⚓
   In Harness Dashboard you will notice Connector tab under your 
   project setup section.
1. Click **+ New Connector** and choose **Docker Repositories** 
   from **Artifact Repositories**.
   - Name:dockerconnector
   - Docker Registry URL:https://registry.hub.docker.com/v2/
   - Username:DOCKER_USERNAME
   - Password:Docker secret you created in Harness Secret Tab and 
   **Continue**.
2. Select how you would like to connect to the provider:Connect 
     through a Harness Delegate
3. Select the Delegates this Connector will use:Use Delegate you 
     created-newdelegate then **Save and Continue** and 
   **Finish**.

##**Setting up Project**⚓
We have already **forked** the [cd-pipeline-sample](https://github.com/harness-community/cd-pipeline-sample).
Its time to create Pipeline and see what we have created.

##**Setting up the Pipeline**⚓
   Go to **cd-pipeline-sample repo** which you forked browse 
   through**.harness folder** and edit **Pipeline.yaml file**
   Name the projectIdentifier:PROJECT_NAME_CREATED_IN_HARNESS
   (for eg:my project name is firstproject)
   So **Pipeline.yaml** will be:
```
pipeline:
  name: testk8s
  identifier: testk8s
  projectIdentifier: firstproject
  orgIdentifier: default
  tags: {}
  stages:
    - stage:
        name: k8deploy
        identifier: k8deploy
        description: ""
        type: Deployment
        spec:
          serviceConfig:
            serviceRef: k8service
            serviceDefinition:
              spec:
                variables: []
                manifests:
                  - manifest:
                      identifier: manifest
                      type: K8sManifest
                      spec:
                        store:
                          type: Github
                          spec:
                            connectorRef: testhelp
                            gitFetchType: Branch
                            paths:
                              - default-k8s-manifests/Manifests/Files/templates
                            repoName: harness-docs
                            branch: main
                        skipResourceVersioning: false
                  - manifest:
                      identifier: values
                      type: Values
                      spec:
                        store:
                          type: Github
                          spec:
                            connectorRef: testhelp
                            gitFetchType: Branch
                            paths:
                              - default-k8s-manifests/Manifests/Files/ng_values_dockercfg.yaml
                            repoName: harness-docs
                            branch: main
                artifacts:
                  primary:
                    spec:
                      connectorRef: harnessdocker
                      imagePath: <+input>
                      tag: <+input>
                    type: DockerRegistry
              type: Kubernetes
          infrastructure:
            environmentRef: K8Env
            infrastructureDefinition:
              type: KubernetesDirect
              spec:
                connectorRef: test
                namespace: default
                releaseName: release-<+INFRA_KEY>
            allowSimultaneousDeployments: false
          execution:
            steps:
              - step:
                  name: Rollout Deployment
                  identifier: rolloutDeployment
                  type: K8sRollingDeploy
                  timeout: 10m
                  spec:
                    skipDryRun: false
            rollbackSteps:
              - step:
                  name: Rollback Rollout Deployment
                  identifier: rollbackRolloutDeployment
                  type: K8sRollingRollback
                  timeout: 10m
                  spec: {}
        tags: {}
        failureStrategies:
          - onFailure:
              errors:
                - AllErrors
              action:
                type: StageRollback
```

1. Choose the Pipeline tab from your project in Harness Dashboard 
   and click on **+ Import From Git** 
2. Fill the details according to image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uk1pjmuj2is8ewwa7cf.png)
3. Now open the pipeline you created and click on **k8deploy**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlc2uve2vb8bn3o3fymo.png)
4. Under **Services** -> Select Service click **New Service** and 
   name it as **nginx** then choose it from dropdown
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w21z8km4yfg1a0qnibhj.png)
5.  Lets setup the manifest now
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2birwgs7cwo7y3l7gb9f.png)
6.  Click on **manifest** edit button ->Continue
   GitHub Connector:CONNECTOR_CREATED_FOR_GITHUB_IN_HARNESS i.e 
   githubconnector.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws4wh6w2ipafs5zbi5tl.png)
7.  Edit **values** by clicking its edit button then **Continue**
   and choose the **Connector** created for Github in Harness 
   Connector and **Submit**.
8.  Finally lets edit the Docker Artifact->**Continue**
  Choose the Docker connecter you created in Harness Connector 
   section.
9.  Click on the button shown in the image and give the path as 
  **library/nginx** and **Submit**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/160cx0z2zkodtccg4bqk.png)
10. **Continue** it will take you to Infrastructure section
     Specify Environment:+ New Environment .
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c24c0t97wd3d221lq36t.png)

- Connector: + New Connector
- Name:kubernetesinfraconnector
- Details:Use the credentials of a specific Harness Delegate (IAM role, service account, etc) and **Continue**
- Delegate:Delegate you created in harness-newdelegate
- Finally **Save and Continue** and **Finish** then **Continue**.
- Now hit **Save** and **Save** again then **Run**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur387titiovsee7ekuwr.png)

 Give tag:**latest** and click **Run Pipeline** 
🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈🦈

**Hoooray we have completed our cd pipeline.**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3em1uxs2gg2l3q8r2ovs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jqklwikd8gyo4zotrhl.png)
 
**👌Happy hacking,see you until next time!अलविदा**
  About me:[freaking_wish](https://wishtree.vercel.app/) 😊
  ref:[Github repo](https://github.com/harness-community/cd- 
  pipeline-sample)
  vid ref:[Harness community](https://youtu.be/yz-HNgHCLHE)