是jenkinsfile中可以用java,无法通过Jenkins上的管道作业的jenkinsfile中的Groovy代码(或java代码)创建文件...

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

}

}

}

}

Jenkins console log: (got exception: Started by user Edgar Yu Running

in Durability level: MAX_SURVIVABILITY [Pipeline] node Running on

Jenkins in /var/jenkins_home/workspace/test2 [Pipeline] { [Pipeline]

stage [Pipeline] { (Build) [Pipeline] echo Building.. [Pipeline] echo

jenkins

[Pipeline] script [Pipeline] { [Pipeline] } [Pipeline] // script

[Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test)

Stage 'Test' skipped due to earlier failure(s) [Pipeline] } [Pipeline]

// stage [Pipeline] stage [Pipeline] { (Deploy) Stage 'Deploy' skipped

due to earlier failure(s) [Pipeline] } [Pipeline] // stage [Pipeline]

} [Pipeline] // node [Pipeline] End of Pipeline

java.io.IOException: Permission denied at java.io.UnixFileSystem.createFileExclusively(Native Method) at

java.io.File.createNewFile(File.java:1012)

解决方案

This is due to Jenkins not implementing Groovy itself but an interpreter (CPS) - https://github.com/cloudbees/groovy-cps

To help deal with the complexities introduced, there are some common Steps implemented to take the trouble out of tasks such as creating a file.

writeFile([file: 'file.txt', text: filetxt])

If your deadset on writing your own, I suggest splitting it out into a Shared library, note this will probably cause ScriptSecurity alerts that will require approval:

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值