简体   繁体   English

Jenkins 构建后步骤作为 Function

[英]Jenkins post build step as a Function

In Jenkins, I know I can do this...在 Jenkins 中,我知道我可以做到这一点......

pipeline {
    agent any
    stages {
        stage('Demo') {
            steps {
                MyFunction()
            }
        }
    }
}

void MyFunction() {
    sh 'ls /'
}

In this situation, the function is within the pipeline, but then I can always extract MyFunction into a shared library for reuse across pipelines.在这种情况下,function 在管道内,但我总是可以将MyFunction提取到共享库中,以便跨管道重用。

But would it be possible to do this with a post-build step?但是有可能通过后期构建步骤来做到这一点吗?

In this case, I would like to convert this into a function and extract it into a library.在这种情况下,我想将其转换为 function 并将其提取到库中。

    post {
        always {
            /* clean up our workspace */
            deleteDir()
            /* clean up tmp directory */
            dir("${workspace}@tmp") {
                deleteDir()
            }
            /* clean up script directory */
            dir("${workspace}@script") {
                deleteDir()
            }
            dir("${workspace}@2") {
                deleteDir()
            }
            dir("${workspace}@2@tmp") {
                deleteDir()
            }            
        }
    }

I've tried this我试过这个

    post {
        always{
            test()
        }
    }
}

With Function带 Function

void test() {
         {
            /* clean up our workspace */
            deleteDir()
            /* clean up tmp directory */
            dir("${workspace}@tmp") {
                deleteDir()
            }
            /* clean up script directory */
            dir("${workspace}@script") {
                deleteDir()
            }
            dir("${workspace}@2") {
                deleteDir()
            }
            dir("${workspace}@2@tmp") {
                deleteDir()
            }            
        }
}

But it doesn't seem to work.但这似乎不起作用。

Is this possible at all or am I just missing something really obvious?这是可能的,还是我只是错过了一些非常明显的东西?

Passing the name of the workspace as a parameter in the function will solve your issue.在 function 中将工作区的名称作为参数传递将解决您的问题。 The below script works.下面的脚本有效。

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post{
        always{
            echo "In : ${env.WORKSPACE}"
            test(env.WORKSPACE)
        }
    }
}

void test(workspace){
    echo "In test : " + workspace
    deleteDir()
    dir("${workspace}@tmp") {
        deleteDir()
    }
}

Also, instead of calling deleteDir() for multiple tmp directories, if you call deleteDir() only once, then it will delete the workspace as well as tmp directories此外,如果您只调用一次 deleteDir( deleteDir() ,而不是为多个 tmp 目录调用deleteDir() ,那么它将删除工作区以及 tmp 目录

The way that works for us to clean up the workspace after a specific stage and without trying to guess the folder name is to make your post function in stage:我们在特定阶段之后清理工作区而不试图猜测文件夹名称的方法是在阶段发布 function:

pipeline {
    agent any

    stages {
        stage('1') {
            steps {
                echo 'Hello World'
            }
            post {
                cleanup {
                    script {
                        // Workspace Cleanup plugin
                        cleanWs deleteDirs: true, 
                            notFailBuild: true, 
                            cleanWhenAborted: true, 
                            cleanWhenFailure: true, 
                            cleanWhenSuccess: true

                    }
                }
            }
        }
    }
}

We use WorkspaceCleanup plugin .我们使用WorkspaceCleanup 插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM