jenkinsci / configuration-as-code-plugin

Jenkins Configuration as Code Plugin
https://plugins.jenkins.io/configuration-as-code
MIT License
2.69k stars 718 forks source link

casc.jenkins.config Java propert ignored #1945

Closed jzielke84 closed 2 years ago

jzielke84 commented 2 years ago

Jenkins and plugins versions report

Environment ```text Paste the output here ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Debian Buster

Reproduction steps

  1. Install Jenkins using jcasc
  2. Jcasc Plugin doesn't recognize the java property "-Dcasc.jenkins.config=..."

Expected Results

Respect the directory defined in the property.

Actual Results

Property is being ignored

Anything else?

At first the environment variable was being ignored. Hence I switched to the java property. Now this one after a few months seems also to be broken.

jpavlav commented 2 years ago

I just recently had issues with this myself. I'll preface the following explanation with, this is a workaround, it is not the intended flow. I make no guarantees that this will work indefinitely and it probably isn't recommended.

To work around it, you can drop a file in $JENKINS_HOME that specifies where the configuration path. The file name is io.jenkins.plugins.casc.CasCGlobalConfig.xml. The contents of the file should be as follows:

<?xml version='1.1' encoding='UTF-8'?>
<io.jenkins.plugins.casc.CasCGlobalConfig plugin="$(configuration-as-code@version)">
  <configurationPath>$(PATH_TO_CASC_CONFIGS)</configurationPath>
</io.jenkins.plugins.casc.CasCGlobalConfig>

Where $(PATH_TO_CASC_CONFIGS) is the path to the directory containing your casc configs and $(configuration-as-code@version) is literally the string configuration-as-code@<the current version of config as code>. To obtain the configuration-as-code@version string, if you have access to the jenkins cli, you should be able to do something like this (this is just an example, you'll have to negotiate whether or not you need auth):

/usr/bin/java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 list-plugins | grep configuration-as-code | awk '{print $1"@"$NF}'
configuration-as-code@1414.v878271fc496f

As an alternative, if you have access to the Jenkins script console https://$YOUR_JENKINS_URL/script, this snippet should produce it:

Jenkins.instance.pluginManager.plugins.each{
  plugin ->
  if (plugin.getShortName().contains("configuration-as-code")) {
    conf_as_code = "${plugin.getShortName()}@${plugin.getVersion()}"
  }
}

println(conf_as_code)
jzielke84 commented 2 years ago

I have figured out why: It seems like the jenkins people changed the way environment variables are taken into account on startup. Now everything resides in the systemd file itself. However, they didn't cleanup / remove the deprecated file in /etc/default/jenkins from their apt package. So both files are installed and there is no hint in the old file to use the other one (duh!). So in case anyone stumbles over this issue: there is your solution.