简体   繁体   English

"Jenkins - HTML Publisher Plugin - 在 Jenkins Server 中查看报告时不显示 CSS"

[英]Jenkins - HTML Publisher Plugin - No CSS is displayed when report is viewed in Jenkins Server

I have a strange problem with the Jenkins HTML Publisher plugin, wherein all the fancy CSS I have added to the report is stripped out when viewed in Jenkins.我对 Jenkins HTML Publisher 插件有一个奇怪的问题,在 Jenkins 中查看时,我添加到报告中的所有花哨的 CSS 都被删除了。 If I download the report to local, I am able to see the CSS formatting.如果我将报告下载到本地,我可以看到 CSS 格式。 Is there a setting in Jenkins which allows CSS to be viewed? Jenkins 中是否有允许查看 CSS 的设置?

My HTML Publisher Settings in Jenkins:我在 Jenkins 中的 HTML Publisher 设置:

在此处输入图像描述

My Report Page when displayed in Jenkins :在 Jenkins 中显示的我的报告页面:

在此处输入图像描述

My Report Page when displayed in Local :我的报告页面在本地显示时:

在此处输入图像描述

Figured out the issue.想通了这个问题。 Sharing it here for other users.在这里分享给其他用户。

CSS is stripped out because of the Content Security Policy in Jenkins.由于 Jenkins 中的内容安全策略,CSS 被剥离。 ( https://wiki.jenkins-ci.org/display/JENKINS/Configuring+Content+Security+Policy ) ( https://wiki.jenkins-ci.org/display/JENKINS/Configuring+Content+Security+Policy )

The default rule is set to:默认规则设置为:

sandbox; default-src 'none'; img-src 'self'; style-src 'self';

This rule set results in the following:此规则集的结果如下:

  • No JavaScript allowed at all根本不允许使用 JavaScript
  • No plugins (object/embed) allowed不允许插件(对象/嵌入)
  • No inline CSS, or CSS from other sites allowed不允许内联 CSS 或来自其他网站的 CSS
  • No images from other sites allowed不允许来自其他网站的图片
  • No frames allowed不允许使用框架
  • No web fonts allowed不允许使用网络字体
  • No XHR/AJAX allowed, etc.不允许 XHR/AJAX 等。

To relax this rule, go to要放宽此规则,请转到

  1. Manage Jenkins->管理詹金斯->
  2. Manage Nodes->管理节点->
  3. Click settings(gear icon)->点击设置(齿轮图标)->
  4. click Script console on left and type in the following command:单击左侧的脚本控制台并输入以下命令:

    System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

and Press Run.并按运行。 If you see the output as 'Result:' below "Result" header then the protection disabled.如果您在“结果”标题下方看到输出为“结果:”,则保护被禁用。 Re-Run your build and you can see that the new HTML files archived will have the CSS enabled.重新运行您的构建,您可以看到存档的新 HTML 文件将启用 CSS。

In CentOs , to enable images in html reportCentOs 中,启用 html 报告中的图像

  • sudo vi /etc/sysconfig/jenkins
  • set following in JENKINS_JAVA_OPTIONJENKINS_JAVA_OPTION设置以下JENKINS_JAVA_OPTION

JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\\"default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;\\""

This will work even after restarting jenkins' server.即使在重新启动 jenkins 的服务器后,这也将起作用。


Directive指示

default-src : The default-src is the default policy for loading content such as JavaScript, Images, CSS, Fonts, AJAX requests, Frames, HTML5 Media default-src : default-src 是加载内容的默认策略,如 JavaScript、图像、CSS、字体、AJAX 请求、框架、HTML5 媒体

img-src: Defines valid sources of images. img-src:定义有效的图像来源。

Source Value来源价值

' self ' - Allows loading resources from the same origin (same scheme, host and port). ' self ' -允许从相同的来源(相同的方案、主机和端口)加载资源。

Usage : default-src 'self'用法: default-src 'self'

' unsafe-inline ' - Allows use of inline source elements such as style attribute, onclick, or script tag bodies (depends on the context of the source it is applied to) and javascript: URIs. ' unsafe-inline ' -允许使用内联源元素,例如 style 属性、onclick 或脚本标记主体(取决于应用它的源的上下文)和 javascript: URI。

Usage : default-src 'unsafe-inline'用法: default-src 'unsafe-inline'

' unsafe-eval ' - Allows unsafe dynamic code evaluation such as JavaScript eval() ' unsafe-eval ' -允许不安全的动态代码评估,例如 JavaScript eval()

Usage : default-src 'unsafe-eval'用法: default-src 'unsafe-eval'

data: - Allows loading resources via the data scheme (eg Base64 encoded images).数据: -允许通过数据方案加载资源(例如 Base64 编码图像)。

Usage : img-src 'self' data:用法: img-src 'self' data:

Please refer more about content security policy here在此处参考有关内容安全政策的更多信息

转到“管理詹金斯”->“脚本控制台”并运行以下命令:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

(The following solution is for Windows.) (以下解决方案适用于 Windows。)

A permanent fix is to change a line in [Jenkins directory]\\jenkins.xml (for me it's at C:\\Jenkins\\jenkins.xml )永久修复是更改[Jenkins directory]\\jenkins.xml (对我来说它在C:\\Jenkins\\jenkins.xml

<executable>java.exe</executable>
<arguments>[arguments are here]</arguments>

Add the following argument to the whitespace-separated list of arguments:将以下参数添加到以空格分隔的参数列表中:

-Dhudson.model.DirectoryBrowserSupport.CSP=

Then restart the Jenkins service to pick up the change.然后重新启动 Jenkins 服务以获取更改。

You can fix this by using the groovy command as specified in Vall 's answer.您可以使用Vall的回答中指定的 groovy 命令来解决此问题。

The effect is in place until Jenkins restarts and afterwards you have to do it again.效果会一直存在,直到 Jenkins 重新启动,之后您必须再次执行此操作。

A solution to solve this problem is to configure a job that will do this for you whenever jenkins starts up.解决此问题的一个解决方案是配置一个作业,该作业将在 jenkins 启动时为您执行此操作。

You can do this by using the Startup Trigger plugin .您可以使用Startup Trigger 插件来完成此操作。

After you install it create a new job and you will have a new checkbox under the Build Triggers section that you will have to check.安装后创建一个新作业,您将在Build Triggers部分下有一个新复选框,您必须检查该复选框。

Then add an Execute system Groovy script build step with the command:然后使用以下命令添加执行系统 ​​Groovy 脚本构建步骤:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

Save and everything should work.保存,一切都应该工作。

For Ubuntu 14 version helpful was special plugins:对于 Ubuntu 14 版本有用的是特殊插件:

https://wiki.jenkins-ci.org/display/JENKINS/Startup+Trigger - To start job on jenkins startup https://wiki.jenkins-ci.org/display/JENKINS/Startup+Trigger - 在 jenkins 启动时开始工作

https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin - To run System Groovy script https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin - 运行系统 Groovy 脚本

And I made a job, that starts on Jenkins restart and sets parametr.我做了一份工作,从 Jenkins 重启开始并设置参数。

设置在运行 Jenkins 后开始构建

And added system Groovy script to set parametr.并添加了系统 Groovy 脚本来设置参数。 运行系统 Groovy 脚本 System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; img-src 'self';") System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; img-src 'self';")

On CentOS , the below solution (which was suggested in comments of another answer) is the only one which has worked for me:CentOS 上,以下解决方案(在另一个答案的评论中建议)是唯一对我有用的解决方案:

  1. Go to: Manage Jenkins > Manage Nodes and Clouds转到:管理 Jenkins > 管理节点和云
  2. Click Gear icon on the right hand side for the node (by default there will be just one Node called Master)单击节点右侧的齿轮图标(默认情况下只有一个节点称为主节点)
  3. Click 'Script Console' on the left单击左侧的“脚本控制台”
  4. Enter the following into the console window: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;")在控制台窗口中输入以下内容: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;")
  5. Click Run点击运行
  6. You should see some output in the Result section, similar to below screenshot:您应该在 Result 部分看到一些输出,类似于下面的截图:

脚本控制台结果

My particular issue was missing images/css in Serenity BDD reports.我的特殊问题是 Serenity BDD 报告中缺少图像/css。 After performing these steps my Serenity reports had all images/css rendering properly, including reports from builds which had executed prior to this change.执行这些步骤后,我的 Serenity 报告已正确呈现所有图像/CSS,包括此更改之前执行的构建报告。 This solution will also work for any published html-based report.此解决方案也适用于任何已发布的基于 html 的报告。

Go To

Manage Jenkins --> Script console管理 Jenkins --> 脚本控制台

and type in the following command:并输入以下命令:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

then Press Run.然后按运行。 if you get the output as 'Result', then rerun the build check the HTML report format如果输出为“结果”,则重新运行构建检查 HTML 报告格式

For setting permanently create a Groovy script file $JENKINS_HOME/init.groovy , or any .groovy file in the directory $JENKINS_HOME/init.groovy.d/ with the following content:为了设置永久创建一个 Groovy 脚本文件$JENKINS_HOME/init.groovy ,或目录$JENKINS_HOME/init.groovy.d/中的任何.groovy文件,其内容如下:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts; default-src 'self'; img-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline';")

systemctl restart jenkins systemctl 重启詹金斯

https://wiki.jenkins.io/display/JENKINS/Post-initialization+script https://wiki.jenkins.io/display/JENKINS/Post-initialization+script

Open jenkins.xml file and copy arguments as below.打开 jenkins.xml 文件并复制如下参数。 it will fix permanently.它将永久修复。 Once its done restart your machine.完成后重新启动您的机器。

<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-scripts; default-src 'self'; style-src 'self' 'unsafe-inline';" -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war"</arguments

It's too late to respond but thought to share.现在回复为时已晚,但想分享。

I was struggling with Jenkins deployed on Tomcat, tried execution of script, it helps but goes away if tomcat is rebooted.我正在努力与部署在 Tomcat 上的 Jenkins 做斗争,尝试执行脚本,如果重新启动 tomcat,它会有所帮助,但它会消失。

Made the permanent fix by setting the property in catalina.properties in tomcat.通过在 tomcat 中设置 catalina.properties 中的属性进行永久修复。

Properties file: tomcat_installation_dir/conf/catalina.properties Just copy paste the below line in catalina.properties at the last (you can set it anywhere just to not mess with existing properties)属性文件:tomcat_installation_dir/conf/catalina.properties 最后复制粘贴下面这行在 catalina.properties 中(你可以在任何地方设置它只是为了不干扰现有的属性)

-Dhudson.model.DirectoryBrowserSupport.CSP="" -Dhudson.model.DirectoryBrowserSupport.CSP=""

To set the system property permanently if using Jenkins-X , then create the file myvalues.yaml in the current directory, with the following content:如果使用Jenkins-X ,要永久设置系统属性,然后在当前目录中创建文件myvalues.yaml ,内容如下:

jenkins:
  Master:
    JavaOpts: >
      -Dhudson.model.DirectoryBrowserSupport.CSP=

Then restart the jenkins-x platform, which can be done by upgrading it:然后重启jenkins-x平台,升级即可:

$ jx upgrade platform --always-upgrade
# Presumably jx.exe is used if on Windows (not tested)

To avoid actually upgrading the platform, just forcibly upgrade it to the same version:为了避免实际升级平台,只需将其强制升级到相同版本:

$ version=$(jx version --no-version-check\
            | grep 'jenkins x platform' | sed -e 's/^jenkins.\+ //')
$ jx upgrade platform --version ${version} --always-upgrade

I had the same issues after adding HTTPS to my jenkins.将 HTTPS 添加到我的 jenkins 后,我遇到了同样的问题。 In case you are having the same issue, the solution is easy - set your Jenkins url to use HTTPS protocol instead of HTTP.如果您遇到同样的问题,解决方案很简单 - 将您的 Jenkins url 设置为使用 HTTPS 协议而不是 HTTP。 It can be configured via jenkins configuration -> jenkins url可以通过 jenkins configuration -> jenkins url 进行配置

For those who are using asciidoctor-maven-plugin plugin to produce an HTML document from the asciidoc file to further publish it to Jenkins, update the plugin configuration, add linkcss attribute:对于那些使用asciidoctor-maven-plugin插件从 asciidoc 文件生成 HTML 文档以进一步发布到 Jenkins 的人,更新插件配置,添加linkcss属性:

<configuration>
    <attributes>
        <linkcss>true</linkcss>
    </attributes>
    <backend>html5</backend>
    <outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
</configuration>

We have a much simpler solution to the problem.我们有一个更简单的解决方案。 Unless you really insist on having the HTML reports, you can just use the Warnings NG plugin (which is a good idea anyway):除非你真的坚持使用 HTML 报告,否则你可以只使用警告 NG 插件(无论如何这是一个好主意):

https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md

We use this for CodeNarc (Groovy), but also for Checkstyle, PMD, SpotBugs and Java warnings (Java).我们将它用于 CodeNarc (Groovy),但也用于 Checkstyle、PMD、SpotBugs 和 Java 警告 (Java)。

I know this is old, but this worked great for me, and it is what seems to be recommended in the Jenkins docs.我知道这很旧,但这对我来说非常有用,这似乎是 Jenkins 文档中推荐的。 I just set the resource root to a different url served from the same location.我只是将资源根设置为从同一位置提供的不同 url。

"It is strongly recommended to set up the Resource Root URL instead of customizing Content-Security-Policy. Most of the documentation below was written when Content-Security-Policy was first introduced and is retained for use by administrators unable to set up Jenkins to serve user content from a different domain." “强烈建议设置资源根 URL,而不是自定义 Content-Security-Policy。下面的大部分文档都是在首次引入 Content-Security-Policy 时编写的,并保留以供无法设置 Jenkins 的管理员使用提供来自不同域的用户内容。”

  1. In MacOS, Jenkins runs a service, that needs to create a groovy script inside $JENKINS_HOME/init.groovy.d在 MacOS 中,Jenkins 运行一个服务,该服务需要在$JENKINS_HOME/init.groovy.d中创建一个 groovy 脚本

  2. call startup-properties.groovy and code :调用startup-properties.groovy和代码:

    import jenkins.model.Jenkins import java.util.logging.LogManager /* Jenkins home directory / def jenkinsHome = Jenkins.instance.getRootDir().absolutePath def logger = LogManager.getLogManager().getLogger("") / Replace the Key and value with the values you want to set. import jenkins.model.Jenkins import java.util.logging.LogManager /* Jenkins 主目录/ def jenkinsHome = Jenkins.instance.getRootDir().absolutePath def logger = LogManager.getLogManager().getLogger("") /替换密钥和要设置的值的值。 / / System.setProperty(key, value) */ System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") logger.info("Jenkins Startup Script: Successfully updated the system properties value for hudson.model.DirectoryBrowserSupport.CSP . Script location : ${jenkinsHome}/init.groovy.d ") // System.setProperty(key, value) */ System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") logger.info("Jenkins启动脚本:成功更新hudson.model.DirectoryBrowserSupport的系统属性值.CSP . 脚本位置:${jenkinsHome}/init.groovy.d")

  3. Restart the Jenkins Service: brew services restart jenkins-lts重启 Jenkins 服务: brew services restart jenkins-lts

  4. Re-build the job and verify the HTML report into the build重新构建作业并将HTML 报告验证到构建中

Warning CSP in Jenkins Jenkins 中的警告 CSP

On Debian/Ubuntu , in aptitude installations :Debian/Ubuntu 上,在aptitude 安装中

  • sudo vi /etc/default/jenkins
  • Add -Dhudson.model.DirectoryBrowserSupport.CSP= to the JAVA_ARGS (eg JAVA_ARGS="-Dhudson.model.DirectoryBrowserSupport.CSP=" )-Dhudson.model.DirectoryBrowserSupport.CSP=添加到JAVA_ARGS (例如JAVA_ARGS="-Dhudson.model.DirectoryBrowserSupport.CSP="
  • Restart jenkins - service jenkins restart重启詹金斯 - 重启詹金斯service jenkins restart

This is was enough for me, but for the full list of parameters see this answer .这对我来说已经足够了,但有关参数的完整列表,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM