One of my previous article, I have explained how to publish Helm chart to GitHub pages. But in that tutorial Helm chart packaging and publishing to GitHub pages are done manually. But it's a time consuming process. So in this article I will explain how to automate Helm chart packaging and publishing to GitHub pages using GitHub Actions. This will save your time and effort. Let's get started.

Prequsites

  • GitHub repo with Helm chart
  • GitHub Pages enabled
  • Create Helm chart

    Now you need to create a Helm chart. You can create a new Helm chart or you can use an existing Helm chart. In this example, I am going to use an existing Helm chart. I am going to use the "tomcat-chart" Helm chart. You can find the Helm chart in the following GitHub repository.

    Configuring GitHub Pages

    Cretae a new branch called gh-pages in your GitHub repository. Now you need to configure GitHub pages. You can use the following steps to configure GitHub pages.
  • Navigate to the "Settings" tab in your GitHub repository
  • Scroll down to the "GitHub Pages" section from the left sidebar
  • Click the "Source" dropdown and select the "Deploy from a branch" option
  • Click the "Source" dropdown and select the "gh-pages" branch from the dropdown
  • Click the "Save" button
  • Initalize the GitHub Action Workflow

    Naviagte to your local project directory and create a new directory called .github/workflows . Inside that directory create a new file called action.yml. You can use any name according to your desire. This file will contain the workflow configuration. First we need to provide name for our workflow and need to define the event that will trigger the workflow. In this case we are going to use push event. So our workflow configuration will look like this.

    1
    2
    3
    4
    5
    6
    7
    8
    name: Deploy Helm Chart

    on:
    push:
    branches:
    - master

    permissions: write-all

    Now we need to define the jobs that will run when the workflow is triggered. In this case we are going to use job called deploy but you can use any other name you wish to use and it’s running on ubuntu-latest virtual machine. So our workflow configuration will look like this.

    1
    2
    3
    4
    ...
    jobs:
    deploy: // job that will run when the workflow is triggered
    runs-on: ubuntu-latest // virtual machine that will run the job

    In next step we’ll moving to steps section. In this section we are going to define the steps that we need to package and publish Helm chart.

    Checkout code

    First we need to checkout the code from the repository. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    5
    6
    ...
    jobs:
    deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    Install Helm

    Now we need to install Helm in our virtual machine. To do that we need to add the following code to our workflow configuration.

    1
    2
    - name: Helm tool installer
    uses: Azure/setup-helm@v3

    In here, I am using Azure/setup-helm action to install Helm. You can use any other action to install Helm. You can find more information about Azure/setup-helm action from here.

    Check Helm version(optional)

    In here I have add extra step to check the Helm version. This step is optional. You can skip this step if you want. To do that we need to add the following code to our workflow configuration.

    1
    2
    - name: Check Helm version
    run: helm version

    Generate Random Semantic Version

    For versioning our Helm chart, here I am going to use random semantic version. To do that we need to add the following code to our workflow configuration. You can use any other method to version your Helm chart.

    1
    2
    3
    - name: Generate Random Semantic Version
    id: random-version
    run: echo "::set-output name=version::$(echo v0.$((1 + RANDOM % 9)).$((1 + RANDOM % 9)))-${{ github.run_number }}"

    So, when this step is executed in the GitHub Actions workflow, it will generate a random semantic version (e.g., v0.1.3) and set it as the output variable version. Other steps in the workflow can then access this output value if needed.

    Package Helm chart

    Now we need to package our Helm chart. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    - name: Package Helm Chart
    run: |
    helm package --version ${{ steps.random-version.outputs.version }} ./<helm-chart-directory>

    So, when this step is executed in the GitHub Actions workflow, it will use the Helm CLI to package the Helm chart from the ./ and set the version for the package based on the value generated in the previous step using the Generate Random Semantic Version step.

    Copy packaged Helm chart

    Now we need to copy the packaged Helm chart file (.tgz file) to a directory. Because we need to use this file in the next step. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    - name: Copy chart file
    run: |
    mkdir -p ./<your-directory>
    cp ${{ github.workspace }}/tomcat-chart-${{ steps.random-version.outputs.version }}.tgz ./<your-directory>/

    So, when this step is executed in the GitHub Actions workflow, it will create the directory (if it doesn’t exist) and then copy the packaged Helm chart file (with the version in its name) to that directory for further use or deployment.

    Upload directory as an artifact

    Now we need to upload the directory as an artifact. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    5
    - name: Upload chart file
    uses: actions/upload-artifact@v2
    with:
    name: <artifact-name> // name of the artifact
    path: <your-directory> // directory that we used in the previous step

    So, when this step is executed in the GitHub Actions workflow, it will create an artifact named from the contents of the directory. The artifact will be stored and can be accessed later within GitHub Actions or downloaded manually through the GitHub web interface. This is useful for passing the packaged Helm chart to subsequent workflows or jobs, making it available for deployment or further processing.

    Checkout to gh-pages branch

    As I previously mentioned, we are going to publish our Helm chart to GitHub pages. So we need to checkout to gh-pages branch. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    - name: Checkout gh-pages branch
    uses: actions/checkout@v2
    with:
    ref: gh-pages

    So, when this step is executed in the GitHub Actions workflow, it will checkout to gh-pages branch.

    Download chart file

    Now we need to download the uploaded artifact. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    - name: Download artifact
    uses: actions/download-artifact@v2
    with:
    name: <artifact-name>

    Publish Helm chart

    Now we have reach the final step. In this step we are going to publish our Helm chart to GitHub pages. To do that we need to add the following code to our workflow configuration.

    1
    2
    3
    4
    5
    6
    7
    8
    - name: Update chart repo index in gh-pages branch
    run: |
    helm repo index --merge index.yaml .
    git config --local user.email "[email protected]"
    git config --local user.name "GitHub Actions"
    git add .
    git commit -m "feat: Update chart index with version ${{ steps.random-version.outputs.version }}"
    git push https://<github-username>:${{ secrets.PAT_TOKEN }}@github.com/<github-username>/<github-repo>.git HEAD:gh-pages -

    This helm repo index –merge index.yaml . command updates the Helm chart repository index by merging the index.yaml file with the new chart information. The helm repo index command is used to generate or update an index file for a Helm chart repository. The –merge option is used to merge the existing index.yaml file with the newly packaged chart information. The . specifies the current directory where the charts and the index.yaml file are located. Other commands use to configure git settings and push the changes to the gh-pages branch. The secrets.PAT_TOKEN is a GitHub secret that contains a personal access token (PAT) with the repo scope. This PAT is used to authenticate the git push command to the GitHub repository.

    Conclusion

    In this article, we learned how to automate Helm chart packaging and publish them GitHub pages using GitHub Actions. You can find the all the related scripts and code for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.

    Happy Coding