Create #extension class in Groovy on Jenkins? - groovy

I'm using the Groovy script screen in jenkins, ie http://localhost:8080/jenkins/script , which is very awesome. I wrote a simple Extension:
jenkins = jenkins.model.Jenkins.getInstance()
#hudson.Extension
public class MyGroovyListener extends hudson.slaves.ComputerListener {
public void onOnline( computer, taskListener ) {
System.out.println("groovy onOnline " + computer + " " + taskListener );
}
}
Now I want to get the extension registered somehow. I tried:
jenkins.refreshExtensions()
... but no joy.
How can I go about registering an extension class from the groovy script webpage?

You need to do a little more to build a Jenkins plugin
You can build them in groovy but it needs to be in a HPI (specialised JAR file) - which you load via the plugin screen. You will need gradle, gent and the example as built by Kiyotaka.
https://github.com/kiy0taka/jenkins-plugin.gent
gent --name=<proj> kiy0taka/jenkins-plugin
cd <proj>
./gradlew server
The gradlew step will run up a local Jenkins server with the plugin ready loaded
There is more as you need to build form components for the configuration and build design step
and also handle serialising config data

This was answered better in the Google Groups post by the same poster, though it may be outdated now. The mentioned issue GROOVY-3361 still exists, and appears to limit how annotations can be processed and scanned for in Groovy classes.
It's a real shame you can't register things against extension points with Groovy code in Jenkins.
I still wonder if it may be possible with a custom hudson.ExtensionFinder, which itself must be written in Java. But I haven't the time to dig that far.

Related

Why is micronaut invoking the wrong controller method?

I have a micronaut API like this:
#Get("/")
List<Club> listClubs()
#Get("/{id}")
Club show(Long id)
In my unit test, when I invoke the show method, the listClubs() method is actually getting invoked, instead.
Why is this happening?
Details:
Thinking that my URL mappings must be wrong, I started debugging into Netty to try to understand how the framework constructs URLs.
In HttpClientIntroductionAdvice, the context shows the API method like this:
Club show(Long param0)
The interceptor is setting param0 in the parameter map, which doesn't match the actual parameter name of my method. When the URI template is expanded, this causes the ID to get dropped (thus the URI becomes / instead of /1).
I am trying to follow this example:
https://github.com/alvarosanchez/micronaut-workshop/tree/master/ex02/solution/clubs
There is one important different in my project, which is that the endpoint is set at "/club" instead of at "/":
#Controller("/club")
#Client("/club")
I am using a diff tool to compare my project to the sample, but I am struggling to find any other difference (besides package name changes).
Why is this happening? What should I be looking for?
Thanks
Update:
Tested the target endpoint with the browser - looks fine.
Gradle clean does not resolve the issue.
I switched from debugging the Application class with IntelliJ to using "gradlew run" and in the process, I made a change to build.gradle (adding JVM properties pass-through from the gradle CLI). I also played with enabling/disabling the annotation processor in the IDE.
(note: In the previous project, I enabled annotation processing as soon as I imported into the IDE. On this project, I didn't enable it until I started having issues.)
I think the build.gradle alteration caused the problem to go away. Since the issue shows up unreliably, it's hard to tell for certain if this is the change that caused it to be fixed.

jenkins: setting root url via Groovy API

I'm trying to update Jenkins' root URL via the Groovy API, so I can script the deployment of a Jenkins master without manual input (aside: why is a tool as popular with the build/devops/automation community as Jenkins so resistant to automation?)
Based on this documentation, I believe I should be able to update the URL using the following script in the Script Console.
import jenkins.model.JenkinsLocationConfiguration
jlc = new jenkins.model.JenkinsLocationConfiguration()
jlc.setUrl("http://jenkins.my-org.com:8080/")
println(jlc.getUrl())
Briefly, this instantiates a JenkinsLocationConfiguration object; calls the setter setUrl with the desired value, http://jenkins.my-org.com:8080/; and prints out the new URL to confirm that it has changed.
The println statement prints what I expect it to, but following this, the value visible through the web interface at "Manage Jenkins" -> "Configure System" -> "Jenkins URL" has not updated as I expected.
I'm concerned that the value hasn't been update properly by Jenkins, which might lead to problems when communicating with external APIs.
Is this a valid way to fix the Jenkins root URL? If not, what is? Otherwise, why isn't the change being reflected in the config page?
You are creating a new JenkinsLocationConfiguration object, and updating the new one, not the existing one being used
use
jlc = JenkinsLocationConfiguration.get()
// ...
jlc.save()
to get the one from the global jenkins configuration, update it and save the config descriptor back.
see : https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/jenkins/model/JenkinsLocationConfiguration.java

Jenkins Java: How to get the userid of the user who triggered the build in jenkins

Is there a way to find out who started a build in jenkins using java. I understand that below is the approach for figuring out as to who started a build
import hudson.model.*
import jenkins.security.*
def userBuild
for(hudson.model.Cause cause : build.causes) {
userBuild = cause.getUserName()
}
println "Build Started by: "+userBuild
Likewise, can someone please let me know if there is a similar working way to figure out as to who initiated a jenkins build?
Ther is a Plugin that injects the informations in Build Variables.
https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin
BUILD_USER – full name of user started build,
BUILD_USER_FIRST_NAME – first name of user started build,
BUILD_USER_LAST_NAME – last name of user started build,
BUILD_USER_ID – id of user started build.
Maybe this is an option for you.
Was able to figure it out on my own. Added the below changes in buildCaption.jelly so that only the user who started the build will be able to abort the build. Mind that this change only gets reflected in the console-output of a build, but there are other places(such as jenkins landing page and job home page) where the abort button gets displayed and will have to make the changes accordingly
<j:forEach var="cause" items="${it.causes}">
<j:if test="${cause.userName == it.currentlyLoggedInUser}">
<l:stopButton href="stop" alt="[${%cancel}]"/>
</j:if>
</j:forEach>

How to configure Cucumber and Capybara to use the Rack::Test driver?

I am a newbie to Capybara.
Here is my configuration within file env.rb
Capybara.configure do |config|
config.run_server = false
#config.default_driver = :selenium
config.default_driver = :rack_test
config.app_host = 'point to my localhost port 3000'
end
Everything runs just fine if I set default_driver to :selenium. But I need to set the driver to :rack_test, so that when running cucumber command, it will not open the web browser.
Many thanks,
P/S If you are an expert, please show me the learning path, I'm not expecting someone showing them selves.
I presume you want to test against a test server controlled by capybara (which is the normal way to do it), rather than testing against your dev instance (the one at localhost:3000) or a staging server or something.
First, configure capybara to run your Rails app. The usual way to do this is to add the cucumber-rails gem to your Gemfile and require 'cucumber/rails' in your env.rb. You can also set up capybara to run Rails (or any Rack app) manually.
Having done that, capybara will do what you want (use the Rack::Test driver) by default. Remove the configuration that you showed from your env.rb and Cucumber/capybara will work the way you want.
If you also want some scenarios to use Javascript, tag those scenarios with #javascript and add
Capybara.javascript_driver = :selenium
to your env.rb. Capybara will continue to use its Rack::Test driver for scenarios without the tag, and will use its Selenium driver for scenarios with the tag.
Thank you Dave for helping me during the time. Briefly, in order for running "cucumber" without triggering to open a web-browser (which is rack-test), here is the configuration:
1> File env.rb.
require 'cucumber/rails'
Only 1 line above is enough.
2> File .feature
Feature: Post a new Product
Feature: Post a new Product
Scenario: Open new product page
Given I open new product site
When I input new product
Then I should see the product created confirmed
By the way, we don't need "Capybara.javascript_driver = :selenium" within file env.rb.
There's still so many tricky things I need to learn about capybara and cucumber

ClassLoader - Loading and saving data

Hopefully someone can help me with this.
It is my understanding that using a ClassLoader is the most reliable way to load in content.
public class Pipeline{
public static URL getResource(String filename) {
return ClassLoader.getSystemResource(filename);
}
public static InputStream getResourceAsStream(String filename) {
return ClassLoader.getSystemResourceAsStream(filename);
}
}
If you had a file at "[jar bundle]/resources/abc.png" ..You would load it by:
URL url = Pipeline.getResource("resources/abc.png");
Loading is simple.
Saving is what's getting me.
I have a program that collects data while running, saves that data on exit, and then loads the data back in next time and keeps adding to it.
Easiest solution I think would be to save back into the jar bundle so that ClassLoader can get at them. Is this even possible? Or recommended?
I don't mind having my resources outside of the jar, just as long as I don't have to resort to 'File' to get at them and save to them. (Unless it can be done cleanly)
folder/application.jar
folder/resources/abc.png
If you could ../ back one from where the ClassLoader is looking it would be easy to cleanly get data from the directory that actually contains the jar file
Pipeline.getResource("../resources/abc.png");
Any ideas?
This isn't really what class loaders are meant for. Loading resources from the class loader is meant so that you can bundle up your application as one package and components can read each other without worrying about how the system you're deploying to is setup.
If the file in the JAR is meant to be changed by the app, then it isn't part of the app and thus probably shouldn't be in the JAR.
I don't have a lot of context on your app, but hopefully my suggestion will be valid for your situation.
I recommend setting a requirement in your app that it has a work area to which it is allowed to read and write and accept a configuration setting that specifies where this directory is. Typical ways to do this in Java are with environment variables, system properties or JNDI settings (for container deployments).
Examples:
Tomcat's startup scripts figure out where it is installed and sets a system property called catalina.home and allows you to over-ride it with an environment variable called CATALINA_HOME.
JBoss looks for JBOSS_HOME
Java application servers typically look for JAVA_HOME to find the JDK.

Resources