可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
My HTML Publisher Settings in Jenkins:
My Report Page when displayed in Jenkins :
My Report Page when displayed in Local :
回答1:
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)
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.
To relax this rule, go to Manage Jenkins->Script console 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.
回答2:
(The following solution is for Windows.)
A permanent fix is to change a line in [Jenkins directory]\jenkins.xml
(for me it's at 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.
回答3:
You can fix this by using the groovy command as specified in Vall's answer.
The effect is in place until Jenkins restarts and afterwards you have to do it again.
A solution to solve this problem is to configure a job that will do this for you whenever jenkins starts up.
You can do this by using the Startup Trigger plugin.
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.
Then add an Execute system Groovy script build step with the command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
Save and everything should work.
回答4:
In CentOs, to enable images in html report
sudo vi /etc/sysconfig/jenkins
- set following in 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 jenkin server.
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.
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:
Please refer more about content security policy here
回答5:
For Ubuntu 14 version helpful was special plugins:
https://wiki.jenkins-ci.org/display/JENKINS/Startup+Trigger - To start job on jenkins startup
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin - To run System Groovy script
And I made a job, that starts on Jenkins restart and sets parametr.
And added system Groovy script to set parametr.
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; img-src 'self';")
回答6:
Go to “Manage Jenkins” -> “Script console”
and run below command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
回答7:
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.
Made the permanent fix by setting the property in catalina.properties in tomcat.
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)
-Dhudson.model.DirectoryBrowserSupport.CSP=""
回答8:
I had the same issues after adding HTTPS to my jenkins. In case you are having the same issue, the solution is easy - set your Jenkins url to use HTTPS protocol instead of HTTP. It can be configured via jenkins configuration -> jenkins url
回答9:
Go To
Manage Jenkins --> Script console
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
回答10:
On Debian/Ubuntu, in aptitude installations:
sudo vi /etc/default/jenkins
- Add
-Dhudson.model.DirectoryBrowserSupport.CSP=
to the JAVA_ARGS
(e.g. JAVA_ARGS="-Dhudson.model.DirectoryBrowserSupport.CSP="
)
- Restart jenkins -
service jenkins restart
This is was enough for me, but for the full list of parameters see this answer.