# How to create public helm chart repository with GitHub Pages

In this article, we host earlier created Helm Chart as helm repository on GitHub Pages. You can visit earlier article on how to create helm chart and add sub-chart dependency. 

%[https://tech.sachinbhosale.com/how-to-add-dependency-to-helm-chart]

Let's follow the below steps:
 ### Create 'gh_pages` branch to publish

To publish the helm repository to GitHub pages, create a `gh_pages` branch in your repo.

```bash
# Create new empty branch without parent
git checkout --orphan gh-pages
# remove local files 
git rm -rf . 
# Make empty commit to push
git commit --allow-empty  -m "Initial Commit"
# push branch to origin
git push
``` 
### Configure GitHub Repo to publish `github_pages`

To publish the `github_pages` branch , you need to setup `Settings > Pages` in your repository and it should show you the URL of hosted pages.

![Screen Shot 2021-11-30 at 10.07.18 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638290293132/sxXO-pcvj.png)

### Create a personal token

We need to create personal token to pass to Helm Chart Release workflow. Visit `Settings > Developer Settings > Personal Access Token`. Generate Personal token with Scopes `repo`, `workflow`, `write:packages`

>**Keep the token safely and never share with anyone**

![Screen Shot 2021-11-30 at 10.12.56 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638290589963/f4-gxHp5s.png)

### Create Repository Secret

To provide a token to workflow, you need to visit `Settings > Secrets` for your repository and add `REPO_TOKEN` with value of personal token created in previous step.

![Screen Shot 2021-11-30 at 10.14.58 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638290720967/C2CcZJW_2.png)

Also verify that default `GITHUB_TOKEN` has read and write permission by visiting repository `Settings > Actions`

![Screen Shot 2021-11-30 at 10.25.26 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638291446094/OIvK4gVHU.png)

### Create GitHub Action workflow for PullRequest

`helm-pr.yaml` workflow will trigger on pull request (PR) and perform below steps:
- Checkout the code on runner
- Set up Helm on runner i.e. `helm` tools
- Set up [Helm Chart Testing tools](https://github.com/helm/chart-testing#examples) `ct` 
- Add required helm chart dependencies
- List changes for Chart testing - it checks changes compared to base branch in this case `main`
- Chart Lint - it examines charts for possible issues and runs series of tests to verify that chart is well-formed
- Create Kubernetes [kind cluster](https://kind.sigs.k8s.io/docs/user/quick-start/#creating-a-cluster) to install the chart
- Install chart to Kubernetes cluster created in previous step and run the tests in `template/tests`

If all the checks are successful then PR is good to merge and reviewer can merge the changes.

`.github/workflows/helm-pr.yaml`

```yaml
name: Helm PR Workflow

on:
  pull_request:
  workflow_dispatch:

jobs:

  lint-test:
    runs-on: ubuntu-latest
    name: Helm Chart Lint
    env:
      CT_CHART_DIRS: charts
      CT_TARGET_BRANCH: $GITHUB_BASE_REF
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Helm
        uses: azure/setup-helm@v1

      - uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Set up chart-testing
        uses: helm/chart-testing-action@v2.1.0

      - name: Add Helm Repo dependencies
        run: helm repo add bitnami https://charts.bitnami.com/bitnami

      - name: Run chart-testing (list-changed)
        id: list-changed
        run: |
          changed=$(ct list-changed --target-branch $GITHUB_BASE_REF)
          if [[ -n "$changed" ]]; then
            echo "::set-output name=changed::true"
          fi

      - name: Run chart-testing (lint)
        run: ct lint --target-branch $GITHUB_BASE_REF

      - name: Create kind Kubernetes cluster
        uses: helm/kind-action@v1.2.0
        if: steps.list-changed.outputs.changed == 'true'

      - name: Run chart-testing (install)
        run: ct install --target-branch $GITHUB_BASE_REF
```

You can check [sample Helm PR workflow here](https://github.com/sachinb4u/helm-chart-demos/runs/4367579269?check_suite_focus=true) 


![Screen Shot 2021-11-30 at 7.10.01 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638279686635/IsJcG_gKn.png)

### Create Workflow for Helm Chart Release

`helm-release.yaml` workflow triggers on commit to `main` branch and performs below steps:
- Configures git for releasing and pushing changes to `gh_pages`
- Set up Helm on runner
- Add helm chart dependencies
- Setup [Helm Chart Releaser tools](https://github.com/helm/chart-releaser#usage) 
    - package chart and release with version
    - create `index.yaml` referring charts location
    - host the repository on [https://sachinb4u.github.io/helm-chart-demos/index.yaml](https://sachinb4u.github.io/helm-chart-demos/index.yaml)

`.github/workflows/helm-release.yaml`

```yaml
name: Helm Release Workflow

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  helm-release:
    runs-on: ubuntu-latest
    name: Release Helm Chart
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Configure Git
        run: |
          git config --global user.name "$GITHUB_ACTOR"
          git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"

      - name: Install Helm
        uses: azure/setup-helm@v1

      - name: Add Helm Repo dependencies
        run: helm repo add bitnami https://charts.bitnami.com/bitnami

      - name: Run User chart-releaser
        uses: helm/chart-releaser-action@v1.2.1
        env:
          CR_TOKEN: "${{ secrets.REPO_TOKEN }}"
          CR_SKIP_EXISTING: true
        with:
          charts_dir: charts
```

You can check [sample helm release workflow run here](https://github.com/sachinb4u/helm-chart-demos/runs/4367693267?check_suite_focus=true) 


![Screen Shot 2021-11-30 at 7.07.51 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638279522369/crtqz9a8f.png)

### Access Helm Repository

On successful workflow run, you would get helm repo available on [https://sachinb4u.github.io/helm-chart-demos/index.yaml](https://sachinb4u.github.io/helm-chart-demos/index.yaml)

You can try adding repository to your environment and installing `myapp` chart

```bash
# add helm repository
helm repo add sachin https://sachinb4u.github.io/helm-chart-demos

# search chart
helm search repo sachin

# install chart
helm install demo-app sachin/myapp
```

I hope you got an overview of Helm Repository using GitHub action to setup CI-CD pipeline. 

Thanks for reading! I would appreciate your feedback on the articles, it will motivate me to keep writing.
