如何在 Jenkins 中为工件添加时间戳 [英] How to add timestamp for artifacts in Jenkins

查看:49
本文介绍了如何在 Jenkins 中为工件添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了 Jenkisfile,我正在尝试上传带有时间戳的工件.

I have following Jenkisfile and I'm trying to upload the artifacts with a timestamp.

import groovy.transform.Field
@Field def timeStamp = Calendar.getInstance().getTime().format('YYYYMMdd-hhmmss',TimeZone.getTimeZone('CST'))

node {
stage('Creating some artifacts') {
    sh 'touch hello.txt hi.txt'
}

stage('Uploading artifacts') {
    def server = Artifactory.server ('art-1')
    def uploadSpec = """{
        "files": [
        {
        "pattern": "*.txt",
        "target": "repo1/Dev/${env.BUILD_NUMBER}/*.txt.${timeStamp}"
         }
  ]
    }"""
            def buildInfo1 = server.upload(uploadSpec)
            server.publishBuildInfo(buildInfo1)
  }
}

但是,我在尝试此操作时遇到以下错误.

However, I'm getting the following error while trying this.

[consumer_1] Deploying artifact: http://learner.blr.example.com:8081/artifactory/repo1/Dev/12/*.txt.20180913-044451
[Thread consumer_1] An exception occurred during execution:
java.lang.RuntimeException: java.io.IOException: Failed to deploy file. Status code: 500 Response message: Artifactory returned the following errors: 
Invalid path. '*' is not a valid name character: repo1/Dev/12/*.txt.20180913-044451 Status code: 500
    at org.jfrog.build.extractor.clientConfiguration.util.spec.SpecDeploymentConsumer.consumerRun(SpecDeploymentConsumer.java:44)
    at org.jfrog.build.extractor.producerConsumer.ConsumerRunnableBase.run(ConsumerRunnableBase.java:11)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Failed to deploy file. Status code: 500 Response message: Artifactory returned the following errors: 
Invalid path. '*' is not a valid name character: repo1/Dev/12/*.txt.20180913-044451 Status code: 500
    at org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient.uploadFile(ArtifactoryBuildInfoClient.java:692)
    at org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient.doDeployArtifact(ArtifactoryBuildInfoClient.java:374)
    at org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient.deployArtifact(ArtifactoryBuildInfoClient.java:362)
    at org.jfrog.build.extractor.clientConfiguration.util.spec.SpecDeploymentConsumer.consumerRun(SpecDeploymentConsumer.java:39)
    ... 2 more

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.Exception: Error occurred during operation, please refer to logs for more information.
    at org.jfrog.build.extractor.producerConsumer.ProducerConsumerExecutor.start(ProducerConsumerExecutor.java:84)
    at org.jfrog.build.extractor.clientConfiguration.util.spec.SpecsHelper.uploadArtifactsBySpec(SpecsHelper.java:71)
    at org.jfrog.hudson.generic.GenericArtifactsDeployer$FilesDeployerCallable.invoke(GenericArtifactsDeployer.java:190)
Caused: java.lang.RuntimeException: Failed uploading artifacts by spec
    at org.jfrog.hudson.generic.GenericArtifactsDeployer$FilesDeployerCallable.invoke(GenericArtifactsDeployer.java:194)
    at org.jfrog.hudson.generic.GenericArtifactsDeployer$FilesDeployerCallable.invoke(GenericArtifactsDeployer.java:131)
    at hudson.FilePath.act(FilePath.java:1042)
    at hudson.FilePath.act(FilePath.java:1025)
    at org.jfrog.hudson.pipeline.executors.GenericUploadExecutor.execution(GenericUploadExecutor.java:52)
    at org.jfrog.hudson.pipeline.steps.UploadStep$Execution.run(UploadStep.java:65)
    at org.jfrog.hudson.pipeline.steps.UploadStep$Execution.run(UploadStep.java:46)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
    at hudson.security.ACL.impersonate(ACL.java:290)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

是否有任何替代/简单的方法可以在 Jenkins 的工件中添加时间戳?

Are there any alternative/simple way to add timestamp in artifacts in Jenkins?

P.S.:我是 Jenkins groovy 脚本和 JFrog 的新手

P.S.: I'm new to Jenkins groovy scripting and JFrog

推荐答案

错误信息说 * 是文件名的无效字符,所以我认为你不能在目标字段.然而,神器文档说你可以这样做(文档链接如下):

The error message says that * is an invalid character for a file name so I don't think you can use it in the target field. However, the artifactory docs say you can do this instead (links for docs bellow):

def uploadSpec = """{
    "files": [
        {
             "pattern": "(*).txt",
             "target": "repo1/Dev/${env.BUILD_NUMBER}/{1}.txt.${timeStamp}"
        }
    ]

在这段代码中,{1} 代表在 pattern 中第一个括号内匹配的任何内容"(正则表达式中的每个左括号+右括号定义一个 捕获组).

In this code, {1} stands for "whatever got matched inside the first parenthesis in the pattern" (every open+close parenthesis in a regex defines a capture group).

注意:我不使用 artifactory 所以我没有测试上面的代码,我要离开 artifactory 文档:https://www.jfrog.com/confluence/display/RTF/使用+文件+规范https://www.jfrog.com/confluence/显示/RTF/Using+File+Specs#UsingFileSpecs-UsingPlaceholders

Note: I don't use artifactory so I didn't test the above code, I am going off of the artifactory docs: https://www.jfrog.com/confluence/display/RTF/Using+File+Specs https://www.jfrog.com/confluence/display/RTF/Using+File+Specs#UsingFileSpecs-UsingPlaceholders

我还建议您将时间戳移动到文件名而不是文件扩展名,这样当您下载文件时,您的计算机就知道使用哪个程序来打开它.所以我会将目标更改为:

I'd also suggest you move the timestamp to the file name instead of the file extension, so that when you download the file, your computer knows which program to use to open it. So i'd change target to something like:

  • 先按名称然后按时间戳缩短的文件:repo1/Dev/${env.BUILD_NUMBER}/{1}-${timeStamp}.txt
  • 首先按时间戳然后按名称缩短的文件:repo1/Dev/${env.BUILD_NUMBER}/${timeStamp}-{1}.txt

这篇关于如何在 Jenkins 中为工件添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆