Configuration with Kubernetes and Jenkins Part 2: ConfigMaps

In part 2 of configuring Kubernetes with Jenkins Pipeline, I will focus on the ConfigMap Resource. ConfigMaps are key/value configurations that are external to your image and can be passed at startup via environment variables or command line arguments. ConfigMaps differ from Secrets because they should not contain sensitive data (like passwords).

Jenkins has a concept of Config File Management. Jenkins will store the config file on disk and allow you to pull it in to in build that requests it. Will will use this config file to populate our ConfigMap at build time.

Jenkins -> Manage Jenkins -> Managed Files

Config File Management page:
1. Click Add a new Config.
2. Add a new Custom File Type
3. The id of the file should be your repo name so the file can be easily tied to the repo/build it belongs to.
4. Add the file in the proper ConfigMap yaml format (use a key without a . separator).

Example Config File contents:

properties: |+

You can add one or more of these files your Jenkinsfile, using the id from the previous step as fileId. The variable will be the variable name used to reference the config file in your helm script. The variable represents the file path of the configFile in your Jenkins workspace (not the content of the file). In this example, we use CONFIG_FILE as the reference variable name.

stage('Deploy') {
    steps {
        configFileProvider([configFile(fileId: 'example-config', variable: 'CONFIG_FILE')]) {
            sh '### run your helm command'

Assuming your file is a proper yaml doc, you can pass the entire file to helm and use it as a values file

helm upgrade --install
    --set service.image.nameTag=${DOCKER_NAMETAG}
    -f ${CONFIG_FILE}

In your ConfigMap helm template, use the Values file passed in from your helm command.

apiVersion: v1
kind: ConfigMap
  name: {{ .Chart.Name }}
data: |+
  {{- range split "\n" }}
    {{ . }}
  {{- end }}

In each Jenkins environment, load a config file with the same Id. When your pipeline runs, it will pickup the environment specific config file and deploy it in Kubernetes. From here, the ConfigMap can be mounted to your pod as a file or you can pass the configs as environment variables that will be used during image startup.

Leave a Reply

Your email address will not be published. Required fields are marked *