Edg*_* Yu 2 groovy jenkins jenkins-pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
echo "whoami".execute().text
script {
File f = new File('/home/jenkins/test2.txt');
f.createNewFile();
}
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jenkins控制台日志:(例外:由用户Edgar Yu开始运行耐久性级别:MAX_SURVIVABILITY [Pipeline]节点在/ var/jenkins_home/workspace/test2 [Pipeline] {[Pipeline] stage [Pipeline] {(Build)上的Jenkins上运行[Pipeline] echo Building .. [Pipeline] echo jenkins
[管道]脚本[管道] {[管道]} [管道] //脚本[管道]} [管道] //阶段[管道]阶段[管道] {(测试)阶段'测试'由于早期故障而被跳过(s )[Pipeline]} [Pipeline] // stage [Pipeline] stage [Pipeline] {(Deploy)Stage'Deeploy'由于早期失败而被跳过[Pipeline]} [Pipeline] // stage [Pipeline]} [Pipeline] ] // node [Pipeline] Endline of Pipeline java.io.IOException:在java.io.File.createFileExclusively(Native Method)java.io.File.createNewFile(File.java:1012)上的权限被拒绝
这是因为Jenkins没有实现Groovy本身而是一个解释器(CPS) - https://github.com/cloudbees/groovy-cps
为了帮助处理所引入的复杂性,已经实施了一些常见步骤来解决诸如创建文件之类的任务.
要使用开箱即用的Jenkins管道步骤,请使用writeFile:https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-writefile-code-write-file-to-workspace
writeFile([file: 'file.txt', text: filetxt])
Run Code Online (Sandbox Code Playgroud)
如果你自己写的是死的,我建议把它拆分成一个共享库,注意这可能会导致需要批准的ScriptSecurity警报:
final class PipelineUtils implements Serializable {
private script=null
private static final PipelineUtils instance = new PipelineUtils()
@NonCPS
String saveFile(String filename, String text) {
String PWD = script.pwd()
String filePath = "${PWD}/${filename}"
File file = new File(filePath)
file.text = text
}
}
Run Code Online (Sandbox Code Playgroud)
有关@NonCPS和非可序列化对象的信息,请参阅https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md.
| 归档时间: |
|
| 查看次数: |
3631 次 |
| 最近记录: |