Guide to using Groovy to update the URL of theme CSS in Jenkins?

Currently, I am attempting to customize the Jenkins user interface by incorporating custom CSS. In order to achieve this, I have placed the CSS file within the userContent folder and have successfully installed the Simple Theme Plugin. I am able to manually modify the URL of the theme's CSS by accessing the configuration page within Jenkins. My goal now is to automate this process using Groovy scripting, but I am encountering challenges in finding the appropriate API for this task. Can anyone confirm if this automation is feasible?

Answer №1

Here is another method that showcases the new configuration layout in the updated UI of simple-theme 0.5.1:

import jenkins.model.Jenkins;
import org.jenkinsci.plugins.simpletheme.CssUrlThemeElement;

Jenkins jenkins = Jenkins.get()

def themeDecorator = jenkins.getExtensionList(org.codefirst.SimpleThemeDecorator.class).first()

themeDecorator.setElements([
  new CssUrlThemeElement('https://some.dummy/url.css')
])

jenkins.save()

If more configuration options are required, simply include object instances within the list for setElements(). Each instance would correspond to an item in the configuration menu. Various classes can be utilized as seen here.

Answer №2

To update the URL for CSS styling or JavaScript files in the SimpleThemeDecorator, simply assign a new URL string to either the cssUrl or jsUrl properties. Setting them to null will reset the URL. Any changes made will be applied immediately.

for (pd in PageDecorator.all()) {
  if (pd instanceof org.codefirst.SimpleThemeDecorator) {
    pd.cssUrl = 'https://...'
  }
}

Answer №3

A different approach to the problem:

import jenkins.model.Jenkins

YELLOW = '\u001B[33m';
RESET  = '\u001B[0m';

def applyCustomTheme(def descriptor, String cssUrl, String jsUrl) {
    if ( !(cssUrl == null || cssUrl.trim().isEmpty()) ) {
        println("${YELLOW}Applying custom CSS: ${cssUrl.trim()}${RESET}")
        descriptor.cssUrl = cssUrl.trim()
    }
    if ( !(jsUrl == null ||jsUrl.trim().isEmpty()) ) {
        println("${YELLOW}Applying custom JS: ${jsUrl.trim()}${RESET}")
        descriptor.jsUrl = jsUrl.trim()
    }
    descriptor.save()
}

def customCss = System.getenv('CUSTOM_THEME_CSS') ?: null
def customJs  = System.getenv('CUSTOM_THEME_JS')  ?: null

def themeDecorator = Jenkins.getInstance().getDescriptor("org.codefirst.SimpleThemeDecorator")
applyCustomTheme(themeDecorator, customCss, customJs)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing 'npm start' to launch an HTML file along with a CSS stylesheet - A step-by-step guide

As a newcomer to the world of coding, I've found that asking questions on platforms like Stack Overflow enables me to connect with fellow developers. I usually code using VS Code and rely on the "live server" extension to quickly test my code. Howeve ...

Identify all anchor elements that contain image elements

I am on the hunt. I am looking for a CSS-Selector, but if that's not an option, a jQuery selector would work too. ...

The backstretch slideshow is malfunctioning

I'm really struggling to understand how backstretch works. I've been trying for hours but as a beginner, it's just not clicking. Can someone please take a look at my code and point out what I'm doing wrong? Also, I'm looking for a ...

Design that is specifically tailored for mobile devices and excludes desktop compatibility

I am faced with the challenge of designing a website that is responsive on mobile devices while not being responsive on desktop computers. To address this issue, I have implemented a series of media queries based on specific breakpoints: 320px, 321-480px, ...

Hiding a Class in Bootstrap 4

Code Image Is there a way to remove a specific class in Bootstrap 4 for medium viewport sizes without relying on JavaScript or jQuery? For instance, I would like to remove the border-right class in this code snippet above only when viewed on a medium-size ...

"How can I adjust the positioning of a Materialize modal to be at the top

I am encountering an issue where the modal is being displayed automatically without any trigger when I try to set the top property in the #singlePost. Despite attempting $('#singlePost').css("top", "1%");, it does not seem to work. I have also ex ...

Adjusting the transparency of a div's background using RGBa

I am currently working on adjusting the background opacity of a div using a jquery ui slider. Allowing users to modify the background color of the div, I am seeking guidance on how to adjust only the alpha parameter of the background while keeping the col ...

Is the PNG image displaying poorly in the IE10 browser?

My application features a PNG image with actual dimensions of 300px X 300px. I am using a 20% scale of this image as an input image button, which displays correctly in Mozilla and Chrome browsers. However, in IE, the image appears distorted. Please refer t ...

How to style images in a row using CSS

Currently utilizing bootstrap to create a website and facing the challenge of aligning images horizontally while hiding overflow. Here is the div section: <div class="timeline-gallery col-md-12" style="display: inline-block;"> <img src="/med ...

The button is positioned at the bottom right within a div surrounded by text that flows around it seamlessly

Image Is there a way to position a button in the bottom right corner of a div with a fixed height that is filled with text where the text flows around the button? I have attempted using "float" and positioning the button, but I have not had any success. ...

Tips for simultaneously hovering over SVG and text elementsFeel free to hover over both SVG

After importing an SVG image and creating a box containing both an icon and text, I noticed that when hovering over the box, only the color changes to yellow while the SVG remains unaffected. How can I resolve this issue so that both the text and icon chan ...

Assistance needed in Android Studio for incorporating a button using a Style Sheet (CSS)

Hey there! I'm just starting out with Android programming and could use some guidance. Can someone assist me in creating three simple buttons for my app? The buttons should be colored green, blue, and red, with a thin glowing outline around them. If ...

Customize Bootstrap breadcrumbs with Font Awesome icon divider using SASS

Looking to customize the default breadcrumb style using SASS. Following the guidelines from Bootstrap 4 beta 3 documentation, I made changes in the custom.scss: $breadcrumb-divider: "\f105"; //using FontAwesome icon for fa-angle-right However, setti ...

Displaying data from a Mysqli database using PHP

Looking for some guidance here. I'm currently in the process of developing a PHP website that involves displaying 20 different tables retrieved from a database. So far, I've been using a while loop to generate these tables and populate them with ...

Utilize Flexbox to arrange elements in a column direction without relying on the position property in CSS

Flexbox - align element to the right using flex-direction: column; without relying on position in CSS Hello everyone, I am looking to move my row element to the right without using positioning. This code includes flex-direction: column; and I do not have ...

Transformation effect when hovering over an SVG polygon as it transitions between two states

const createTransitionEffect = (navLI, startCoord, endCoord) => { const changeRate = 0.1; let currentY = startCoord; const animateChange = () => { if (currentY !== endCoord) { currentY += (endCoord - startCoord) * cha ...

Inheriting CSS Styles in a JavaScript Toolbar Plugin

Recently, I created a javascript plugin that can be activated by clicking a button on my browser's toolbar. While it functions perfectly on certain websites, it appears unappealing on others due to inheriting CSS styles from the site itself. Are the ...

Creating a border with tabs and a triangle using CSS

I'm having trouble getting my HTML/CSS tabs to look like the ones in the image. I've experimented with border-radius but haven't been able to achieve the desired result. Can anyone provide guidance on how to replicate these tabs using only ...

Having trouble with the toggle button on the Bootstrap 5 navbar?

I am facing an issue with my HTML code where the toggle button does not display properly when I resize the browser screen. Upon clicking on it, the navigation bar items do not show at all. Here is a screenshot of my website <html> <head> ...

Mastering the art of reading rows in ASP.NET using Java Script

Within the code snippet below, you'll find an image located in the second column. Clicking on this second column should allow me to access the data stored in the first column. Let's say we have a table with 10 rows. If the user clicks on the ico ...