本文介绍了无法通过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....'
            }
        }
    }
}


[管道]脚本[管道] {[管道]} [管道]//脚本 [管道]} [管道]//阶段[管道]阶段[管道] {(测试) 由于较早的失败,跳过了测试"阶段[Pipeline]} [Pipeline] //阶段[Pipeline]阶段[Pipeline] {(部署)跳过了阶段'部署' 由于更早的故障[Pipeline]} [Pipeline]//阶段[Pipeline] } [管道]//节点[管道]管道末端 java.io.IOException:权限被拒绝,位于java.io.UnixFileSystem.createFileExclusively(本机方法) java.io.File.createNewFile(File.java:1012)

[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)

推荐答案

这是由于Jenkins并未实现Groovy本身,而是实现了解释器(CPS)- https://github.com/cloudbees/groovy-cps

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.

要立即使用Jenkins管道步骤,请使用writeFile: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-writefile-code-write-file-to-workspace

To use Jenkins pipeline steps out of the box, use writeFile:https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-writefile-code-write-file-to-workspace

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

如果您在编写自己的文件时陷入僵局,建议将其拆分为共享库,请注意,这可能会导致ScriptSecurity警报需要获得批准:

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
    }
}

请参见 https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md 获取有关@NonCPS和不可序列化对象的信息.

See https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md for information regarding @NonCPS and nonserializable objects.

这篇关于无法通过Jenkins上的管道作业的jenkinsfile中的Groovy代码(或java代码)创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 09:43