You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Artifactory插件能否实现同仓库内工件路径复制?

问题解答

能否用Artifactory插件实现同实例同仓库内的路径复制?

可以,但你当前的代码用法错误——ArtifactoryServer对象并没有copy()方法,这是导致报错的直接原因。正确的做法是使用Artifactory Pipeline插件提供的rtCopy步骤来实现服务器端复制(全程在Artifactory内部完成,不会产生客户端下载再上传的额外流量)。

修正后的Pipeline代码

pipeline {
  agent any

  stages {
    stage('Copy Artifacts in Artifactory') {
      steps {
        script {
          def sourceRepo = 'libs-release-local'
          def sourcePath = 'path/to/source'
          def targetRepo = 'libs-release-local'
          def targetPath = 'path/to/target'

          // 初始化Artifactory服务器连接
          def server = Artifactory.server('artifactory-server-id')
          def rtBuildInfo = Artifactory.newBuildInfo()

          // 使用rtCopy执行服务器端复制
          rtCopy (
            server: server,
            buildInfo: rtBuildInfo,
            spec: """{
              "files": [
                {
                  "pattern": "${sourceRepo}/${sourcePath}/*",
                  "target": "${targetRepo}/${targetPath}/"
                }
              ]
            }"""
          )

          // 发布构建信息(可选,用于追踪复制操作关联的上游构建)
          server.publishBuildInfo(rtBuildInfo)
        }
      }
    }
  }
}

代码说明

  • rtCopy是Artifactory Pipeline插件专为服务器端复制/移动设计的步骤,支持同实例内仓库间、路径间的文件操作,完全在Artifactory服务器端执行,避免跨网络传输开销。
  • 复制规则通过JSON格式的spec定义:pattern指定源文件路径(支持通配符),target指定目标路径,路径需包含仓库名称。

替代方案:使用Artifactory REST API

如果插件版本限制导致rtCopy无法使用,可直接调用Artifactory REST API实现服务器端复制:

pipeline {
  agent any

  stages {
    stage('Copy via Artifactory REST API') {
      steps {
        script {
          def artifactoryUrl = 'http://your-artifactory-url'
          def credentialsId = 'artifactory-credentials' // Jenkins中配置的Artifactory账号凭证
          def sourcePath = 'libs-release-local/path/to/source/*'
          def targetPath = 'libs-release-local/path/to/target/'

          // 调用Artifactory复制API
          httpRequest(
            url: "${artifactoryUrl}/api/copy/${sourcePath}?to=${targetPath}",
            authentication: credentialsId,
            method: 'POST',
            contentType: 'APPLICATION_JSON'
          )
        }
      }
    }
  }
}

API说明

  • 使用/api/copy端点实现服务器端复制,若需要移动文件而非复制,可替换为/api/move端点,参数用法完全一致。
  • 路径参数需包含仓库名称,支持通配符匹配批量文件。

内容的提问来源于stack exchange,提问作者Shane Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:50:01