kokuwaio / helm-gradle-plugin

Gradle Plugin For HELM Chart Development
MIT License
5 stars 5 forks source link
gradle-plugin helm

Helm Plugin for Gradle

This is a simple plugin for interacting with Helm in gradle builds.

Java CI

Why?

This plugin is different from the alternatives listed at the bottom.

What?

Tested with Gradle 6

Changelog

See releases.

Usage

Activate

plugins {
    id 'com.kiwigrid.helm' version '1.5.1'
}

Configure

// everything is optional
helm {
    version '3.0.0' // defaults to 2.17.0
    architecture 'amd64' // auto-detected if not given
    operatingSystem 'linux' // auto-detected if not given
    helmDownloadUrl 'https://example.com/helm.tar.gz' // defaults to 'https://get.helm.sh/helm-v${version}-${operatingSystem}-${architecture}.tar.gz'

    // will be added via: helm repo add <name> <url>, non-existent will be removed (but 'local' and 'stable')
    repositories {
        myHelmRepoName {
            url "https://example.com"
            user "happy-dev" // username used for basic auth (supported since helm v2.9.0-rc3)
            password "1234" // password used for basic auth (supported since helm v2.9.0-rc3)
            deployVia 'PUT' to 'https://example.de/' // also supports 'POST', filename will be appended if url ends with '/' and method is 'PUT'
            // shortcut if url is the same:
            // deployVia 'PUT' toFetchUrl()
        }
    }

    // expansions are replaced in the chart files if found in the form ${path.to.value}
    expansions = [
            helm: [
                    chartName:'my-chart',
                    chartVersion: project.version
            ],
            anotherParam: project.version,
            path: [
                    to: [
                            value: 'foobar'
                    ]
            ]
    ]

    // this will upload the chart to the respective repo using the appropriate deploy spec
    deployTo repositories.myHelmRepoName
}

helmChartBuild() {
    // source folder for the charts manifest files. just an example here, provide your own as needed
    // defaults to file('/src/main/helm')
    source = file(project.projectDir.getAbsolutePath() + '/helm') 
}

helmChartTest() {
    // source folder for the tests files. just an example here, provide your own as needed
    // defaults to file('/src/test/helm')
    tests = file(project.projectDir.getAbsolutePath() + "/helm/test")
}
// Optional. You need to register new pair of tasks for each additional helm chart in your project
// outputDirectory must be same in both tasks and differs from helm.outputDirectory
tasks.register("helmSecondChartBuild", HelmBuildTask.class) {
  dependsOn tasks.named("helmRepoSync")
  expansions = helm.expansions
  copyFrom helm

  // custom configuration
  // source folder for the charts manifest files. just an example here, provide your own as needed
  // defaults to file('/src/main/helm')
  source = file('src/main/secondchart')
  // output folder for the compiled charts manifest files. just an example here, provide your own as needed
  outputDirectory = file("$buildDir/helm/repo2")
}

tasks.register("helmSecondChartTest", HelmBuildTask.class) {
  dependsOn tasks.named("helmSecondChartBuild")
  copyFrom helm

  // custom configuration
  // source folder for the tests files. just an example here, provide your own as needed
  tests = file('src/test/secondchart')
  // output folder for the compiled charts manifest files. just an example here, provide your own as needed
  // defaults to file('/src/test/helm')
  outputDirectory = file("$buildDir/helm/repo2")
}

Author chart

<project folder>
|-- src/main/helm
|   |-- Chart.yml
|   `-- templates
|       `-- ...
`-- src/test/helm
    |-- test-suite01
    |   |-- values_test1.yaml
    |   `-- values_test2.yaml
    |-- value-config-1.yaml
    |-- value-config-2.yaml
    `-- value-config-3.yaml

Noteworthy:

Author Tests

The structure of a test yaml file can take 2 different forms.

If on the top level the key title and values are found this is considered a structured test. Otherwise it's just a value test.

A tests name is determined by the relative path within src/test/helm.

Value Tests

Provides a sample value file. The test case is named like the file name. The value file is fed into helm template and helm lint. If both succeed with exit code 0 the test is considered successful.

Structured Tests

Create a yaml structure like this:

# a simple title for logging purposes
title: "Test title"

# everything under values is provided in a separate value file to helm
values: ~ 

# by default value files are expected to work, set succeed to false
# for negative testing (asserts are ignored in this case)
succeed: true  

# any assertions to be made on the rendered files
assert:
  - file: "<rendered file name relative to chart root>"
    test: "<name-of-test>"
    # ... test specific properties, see below
  - ...

If and only if title and values is present, a structured test is assumed.

Supported tests are:

Note:

Further work:

Alternatives

When the plugin has been created, there hasn't been an alternative. These days there are numerous alternatives: