sous-chefs / jenkins

Development repository for the jenkins cookbook
https://supermarket.chef.io/cookbooks/jenkins
Apache License 2.0
424 stars 635 forks source link

The jenkins_script resource fails when 'groovy_path' attribute set instead of inline 'command' block. #779

Open mromero-convoso opened 2 years ago

mromero-convoso commented 2 years ago

:ghost: Brief Description

When attempting to use the jenkins_script resource with the groovy_path attribute set, as opposed to the command attribute set to a block of inline Groovy code, jenkins-cli returns the following error:

ERROR: This command is requesting the -remoting mode which is no longer supported. See https://www.jenkins.io/redirect/cli-command-requires-channel

I have a workaround that allows me to use a groovy file written by a template resource, which looks like this:

template ::File.join(Chef::Config[:file_cache_path], 'add_authentication.groovy') do
  source 'add_authentication.groovy.erb'
  mode '0644'
  owner 'jenkins'
  group 'jenkins'
  variables(
    password: 'somepassword'
  )
  notifies :execute, 'jenkins_script[add_authentication]', :immediately
  sensitive true
end

jenkins_script 'add_authentication' do
  command(lazy { ::File.open(::File.join(Chef::Config[:file_cache_path], 'add_authentication.groovy')).read })
  not_if 'grep ldapserver /var/lib/jenkins/config.xml'
end

:pancakes: Cookbook version

9.5.0

:woman_cook: Chef-Infra Version

17.8.25

:tophat: Platform details

Ubuntu 20.04 LTS

Steps To Reproduce

Within some recipe, use include the following:

template ::File.join(Chef::Config[:file_cache_path], 'add_authentication.groovy') do
  source 'add_authentication.groovy.erb'
  mode '0644'
  owner 'jenkins'
  group 'jenkins'
  variables(
    password: 'somepassword'
  )
  notifies :execute, 'jenkins_script[add_authentication]', :immediately
  sensitive true
end

jenkins_script 'add_authentication' do
  groovy_path ::File.join(Chef::Config[:file_cache_path], 'add_authentication.groovy')
  not_if 'grep ldapserver /var/lib/jenkins/config.xml'
end

You should get the error mentioned above during convergence.

:police_car: Expected behavior

The executed jenkins-cli command should look something like this:

java -jar "/tmp/kitchen/cache/jenkins-cli.jar" -s http://localhost:8080 -webSocket -auth @<some cli file> groovy = < /tmp/kitchen/cache/add_authentication.groovy

It currently looks something like this:

java -jar "/tmp/kitchen/cache/jenkins-cli.jar" -s http://localhost:8080 -webSocket -auth @<some cli file> groovy < /tmp/kitchen/cache/add_authentication.groovy

The additional = sign in the first example above is what seems to allow it to behave the way it should, without throwing the error about the -remoting option no longer being supported.

mbaitelman commented 2 years ago

When I looked into this there are 2 different methods being called for groovy execution, likely needs to be unified in some way. https://github.com/sous-chefs/jenkins/blob/main/libraries/_executor.rb#L69 https://github.com/sous-chefs/jenkins/blob/main/libraries/_executor.rb#L152