ShailyAggarwal
ShailyAggarwal

Reputation: 863

Provide external yaml file to JCasC configScripts

I am configuring my Jenkins instance using jenkins-helm chart (https://github.com/jenkinsci/helm-charts/blob/main/charts/jenkins/VALUES_SUMMARY.md#jenkins-configuration-as-code-jcasc)

Currently Jenkins config is provided in values.yaml as:

jenkins:
  controller:
    JCasC:
      configScripts:
        key1:|-
          <a-very-big-yaml-value>

Is there a way to import this 'big-yaml-value' from separate yaml file, as it will enhance maintainability of code for us.

Upvotes: 2

Views: 3315

Answers (5)

Artipixel
Artipixel

Reputation: 1244

Based on previous answers, Here is an example how I made it work with multiple configuration yaml files to load into JCasC, using terraform:

Jenkins module folder:
-- configs
   - kubernetes.yaml
   - default-config.yaml
   ...
-- main.tf
-- values.yaml

values.yaml:

   controller:
      JCasC:          
        defaultConfig: false
      persistence:
        enabled: true
        existingClaim: efs-claim-jenkins
      volumes:
        - name: jenkins-custom-casc-config
          configMap:
            name: jenkins-custom-casc-config
      mounts:
        - mountPath: /var/jenkins_home/casc_configs/custom
          name: jenkins-custom-casc-config
          readOnly: true

main.tf:

resource "kubernetes_config_map" "jenkins-custom-casc-config" {
  metadata {
    name      = "jenkins-custom-casc-config"
    namespace = "jenkins"
  }

  data = {
    for file in local.config_files :
    basename(file) => file("${path.module}/configs/${file}")
  }
}

Upvotes: 0

dedzera
dedzera

Reputation: 1

I did it following the idea of ​​HiroCereal, but it keeps showing in the UI

Configuration loaded from : 
  * /var/jenkins_home/casc_configs

and the folder casc_configs has nothing. I'm missing something? i'm using helm charts.

Upvotes: 0

Niko Moilanen
Niko Moilanen

Reputation: 1

I got it working in when doing subfolder to /var/jenkins_home/casc_configs where I inject all config files. Otherwise HiroCereal´s answer works.

Upvotes: 0

HiroCereal
HiroCereal

Reputation: 631

The setup below worked for me. Will put the relevant parts.

Directory layout for the helm chart:

jenkins/
├── conf/
│   ├── shared-library.yaml
│   └── big-yaml.yaml
├── templates/
│   └── jenkins-custom-casc-config.yaml
├── values.yaml
└── Chart.yaml

In the values.yaml, we override the CASC_JENKINS_CONFIG so it takes into account an additional path for config files on top of the default one.

controller:
  containerEnv:
    - name: CASC_JENKINS_CONFIG
      value: "/var/jenkins_home/casc_configs,/var/jenkins_home/custom-casc_configs"
   
persistence:
  volumes:
    - name: jenkins-custom-casc-config
      configMap:
      name: jenkins-custom-casc-config
  mounts:
    - mountPath: /var/jenkins_home/custom-casc_configs
      name: jenkins-custom-casc-config

ConfigMap jenkins-custom-casc-config.yaml that loads all files present in the conf/ folder

apiVersion: v1
kind: ConfigMap
metadata:
  name: jenkins-custom-casc-config
data:
  {{- (.Files.Glob "conf/*").AsConfig | nindent 2 }}

Upvotes: 1

Ian W
Ian W

Reputation: 4767

As I don't use the helm-charts,I can't answer authoritatively, but it is supported in the abstract. According to the JCasC Getting Started documentation:

First, start a Jenkins instance with the Configuration as Code plugin installed.

  • Those running Jenkins as a Docker container (and maybe also pre-installing plugins), do include Configuration as Code plugin.

Second, the plugin looks for the CASC_JENKINS_CONFIG environment variable. The variable points to a comma-separated list of any of the following:

  • Path to a folder containing a set of config files. For example, /var/jenkins_home/init.CasC.
  • A full path to a single file. For example, /var/jenkins_home/init.CasC/jenkins.yaml.
  • A URL pointing to a file served on the web. For example, https://acme.org/jenkins.yaml.

If an element of CASC_JENKINS_CONFIG points to a folder, the plugin will recursively traverse the folder to find file(s) with .yml,.yaml,.YAML,.YML suffix. It will exclude hidden files or files that contain a hidden folder in any part of the full path. It follows symbolic links for both files and directories.

So, yes, you can have multiple yml files. I have over 20 (for 120 plugins). They are broken down by capability (eg: global, agents, tools, credentials , including 2 for RBAC (1 for roles, for users, etc.)), plus some plugin specific yml files. Some are also reused across instances while others are specific.

You should be aware of Merge Strategy in the event of conflicts:

  • ErrorOnConflictMergeStrategy (default)
    • The strategy name is errorOnConflict.
    • Throws an exception if there's a conflict in multiple YAML files.
  • OverrideMergeStrategy
  • The strategy name is override
  • Override the config files according to the loading order.

Also be aware when updating an existing instance, certain plugin configurations may replace configurations, while others may augment an existing configuration, regardless of one yaml or many. And of course, not 100% of options are JCasC-able yet, so some init.groovy is also required. YMMV.

You may also wish to review: JCasC Handling Secrets.

Upvotes: 1

Related Questions