Jenkins - HTML Publisher 插件 - 在 Jenkins 服务器中查看报告时不显示 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35783964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 19:01:26  来源:igfitidea点击:

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

htmlcssjenkinsjenkins-plugins

提问by Vall

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. If I download the report to local, I am able to see the CSS formatting. Is there a setting in Jenkins which allows CSS to be viewed?

我对 Jenkins HTML Publisher 插件有一个奇怪的问题,其中我添加到报告中的所有花哨 CSS 在 Jenkins 中查看时都被删除了。如果我将报告下载到本地,我可以看到 CSS 格式。Jenkins 中是否有允许查看 CSS 的设置?

My HTML Publisher Settings in Jenkins:

我在 Jenkins 中的 HTML 发布者设置:

enter image description here

在此处输入图片说明

My Report Page when displayed in Jenkins :

在 Jenkins 中显示时的我的报告页面:

enter image description here

在此处输入图片说明

My Report Page when displayed in Local :

在 Local 中显示时的我的报告页面:

enter image description here

在此处输入图片说明

回答by Vall

Figured out the issue. Sharing it here for other users.

想通了这个问题。在这里分享给其他用户。

CSS is stripped out because of the Content Security Policy in Jenkins. (https://wiki.jenkins-ci.org/display/JENKINS/Configuring+Content+Security+Policy)

由于 Jenkins 中的内容安全策略,CSS 被剥离。( 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
  • No plugins (object/embed) allowed
  • No inline CSS, or CSS from other sites allowed
  • No images from other sites allowed
  • No frames allowed
  • No web fonts allowed
  • No XHR/AJAX allowed, etc.
  • 根本不允许使用 JavaScript
  • 不允许使用插件(对象/嵌入)
  • 不允许内联 CSS 或来自其他网站的 CSS
  • 不允许来自其他网站的图片
  • 不允许使用框架
  • 不允许使用网络字体
  • 不允许 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", "")

  1. 管理詹金斯->
  2. 管理节点->
  3. 点击设置(齿轮图标)->
  4. 单击左侧的脚本控制台并输入以下命令:

    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。

回答by jithinkmatthew

In CentOs, to enable images in html report

CentOs 中,启用 html 报告中的图像

  • sudo vi /etc/sysconfig/jenkins
  • set following in JENKINS_JAVA_OPTION
  • sudo vi /etc/sysconfig/jenkins
  • 设置以下 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:;\""

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

img-src:Defines valid sources of images.

default-src: default-src 是加载内容的默认策略,如 JavaScript、图像、CSS、字体、AJAX 请求、框架、HTML5 媒体

img-src:定义有效的图像来源。

Source Value

来源价值

' self ' -Allows loading resources from the same origin (same scheme, host and port).

Usage : 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.

Usage : default-src 'unsafe-inline'

' unsafe-eval '- Allows unsafe dynamic code evaluation such as JavaScript eval()

Usage : default-src 'unsafe-eval'

data:- Allows loading resources via the data scheme (eg Base64 encoded images).

Usage : img-src 'self' data:

' self ' -允许从相同的来源(相同的方案、主机和端口)加载资源。

用法 : default-src 'self'

' unsafe-inline '-允许使用内联源元素,例如 style 属性、onclick 或脚本标记主体(取决于应用它的源的上下文)和 javascript: URI。

用法 : default-src 'unsafe-inline'

' unsafe-eval '-允许不安全的动态代码评估,例如 JavaScript eval()

用法 : default-src 'unsafe-eval'

数据:-允许通过数据方案加载资源(例如 Base64 编码图像)。

用法 : img-src 'self' data:

Please refer more about content security policy here

在此处参考有关内容安全政策的更多信息

回答by Som

Go to “Manage Jenkins” -> “Script console” and run below command:

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

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

回答by twasbrillig

(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 服务以获取更改。

回答by dragosb

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 Triggerssection 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.

保存,一切都应该工作。

回答by abiab

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 重启开始并设置参数。

set to start build after Jenkins is ran

设置在运行 Jenkins 后开始构建

And added system Groovy script to set parametr. Run System Groovy scriptSystem.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; img-src 'self';")

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

回答by Shailendra

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 报告格式

回答by Karen Danielyan

For setting permanently create a Groovy script file $JENKINS_HOME/init.groovy, or any .groovyfile 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

回答by Gunnar

To set the system property permanently if using Jenkins-X, then create the file myvalues.yamlin 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

回答by Siva

Open jenkins.xml file and copy arguments as below. it will fix permanently. Once its done restart your machine.

打开 jenkins.xml 文件并复制如下参数。它将永久修复。完成后重新启动您的机器。

<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