如何在Jenkins中为工件添加时间戳

时间:2018-09-13 10:07:11

标签: jenkins groovy jenkins-pipeline artifactory

我一直在关注Jenkisfile,并且尝试使用时间戳上载工件。

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

但是,尝试此操作时出现以下错误。

[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的工件中,是否有其他替代/简单的方法来添加时间戳?

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

1 个答案:

答案 0 :(得分:1)

错误消息指出*是文件名的无效字符,因此我认为您不能在目标字段中使用它。但是,人工文档说,您可以改为执行此操作(以下是文档的链接):

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

在此代码中,{1}代表“在pattern的第一个括号内匹配的所有内容”(正则表达式中的每个打开+关闭括号都定义了一个capture group)。

注意:我不使用人工制品,因此我没有测试上面的代码,我将使用人工制品文档: https://www.jfrog.com/confluence/display/RTF/Using+File+Specs https://www.jfrog.com/confluence/display/RTF/Using+File+Specs#UsingFileSpecs-UsingPlaceholders

我还建议您将时间戳记移至文件名而不是文件扩展名,以便在下载文件时,计算机知道使用哪个程序打开文件。所以我将目标更改为:

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