I have a python script that outputs non-ascii characters. It works fine from the command line (which has LANG en_US.UTF-8), but once I try to run it in Jenkins (launched from the same shell), it fails in the typical manner of an ascii terminal:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 37: ordinal not in range(128)
I've tried preceeding the script call in the "Execute shell" build step like so:
export LANG=en_US开发者_JAVA技巧.UTF-8
However, it seems to ignore that. Putting the following in my python script:
print "Encoding = %s" % sys.getdefaultencoding()
Gives me this, no matter what I do:
Encoding = ascii
So is there something that will allow me to force Jenkins to use UTF-8?
Never mind, found out how to do it:
export PYTHONIOENCODING=UTF-8
If you want to set the encoding on a system level in your Jenkins installation, you can add it as a global properties key-value pair (Jenkins -> Manage Jenkins -> Configure System) with name=LANG
and value=<insert your locale here>
(which in my case equals en_GB.UTF-8
). That way you avoid setting the locale for every Jenkins job.
If you are using the Jenkins Pipeline, you need to set the encoding within the pipeline as:
pipeline {
agent {label 'node name'}
environment {
LC_ALL = 'en_US.UTF-8'
LANG = 'en_US.UTF-8'
LANGUAGE = 'en_US.UTF-8'
}
stages {
stage ('XXXX') {
steps {
echo 'Hello'
}
}
}
}
export PYTHONIOENCODING=UTF-8
did not work for me. However,
export LC_CTYPE=en_US.UTF-8
did.
My solution was: upgrade to the latest version of Jenkins. It seems the latest version respects the system locale settings. See, eg, this patch maybe?
In any case, the default that shipped with Ubuntu 13.10 was out of date. Pulling down the latest version from their official debian repo fixed it for me!
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Ubuntu
Another solution is to use Python 3
brew update
brew install pyenv
pyenv install 3.7.3
pyenv local 3.7.3
make virtualenv
精彩评论