logo
0
0
WeChat Login
fix: 修复下载文件的问题

附件插件

支持 commit 和 release 上传、下载、删除附件,目前暂支持 64GB 以内的文件上传。

镜像

cnbcool/attachments:latest

支持的事件

以下事件支持 上传下载删除 附件操作:

  • push
  • branch.create
  • branch.delete
  • pull_request
  • pull_request.target
  • pull_request.approved
  • pull_request.changes_requested
  • pull_request.mergeable
  • pull_request.merged
  • tag_push
  • vscode
  • auto_tag
  • tag_deploy.*

输入参数说明

  • type: 操作类型,默认值为UPLOAD,非必填。
    • UPLOAD:上传附件
    • DOWNLOAD:下载附件
    • DELETE:删除附件
  • tag: tag 名称,操作指定 tag 时填写,非必填。tag 需有对应的 release 才能上传附件。
  • commit: commit sha,长 hash,非必填。
  • attachments: 附件。上传操作支持传入文件名和附件存在时间ttl。删除操作缺省为删除所有附件,其他操作必填。
  • slug: 仓库路径,非必填。默认为触发当前流水线的仓库,非跨仓上传无需传入。如需跨仓库上传附件,可使用此参数指定仓库路径。
  • endpoint: 上传或下载附件的地址,非必填。默认为当前平台的 OPENAPI 的地址,非跨平台无需传入。 如需跨平台上传附件,如需要从 CNB PAAS 版 上传到 CNB SAAS 版,可使用此参数指定 endpoint,如 https://api.cnb.cool
  • token: 上传或下载附件的 token,非必填。默认为当前用户的临时票据。如需跨平台上传,或跨仓库上传时当前用户无仓库权限,可传入该参数。
  • ttl: 附件存在时间,单位为天,非必填。默认为 0 天,表示附件永久存在。上传附件时选填,其他操作不填。该参数针对所有附件设置过期时间,优先级低于单个附件设置的 ttl。

tag/commit

其中 tagcommit 都不传时,操作目标默认为:

tag_pushtag_deploy.* 事件,tag 取环境变量 $CNB_BRANCH, 操作对象为 tag 对应的 release 的附件。

其他事件,commit 取环境变量 $CNB_COMMIT,操作对象为 commit 的附件。

其中 tagcommit 都传时,操作目标默认为 tag 对应的 release(没有 release 不能上传)。

attachments

typeUPLOAD时,须传入上传文件的相对路径。 多个文件时用逗号分隔或使用数组,支持 glob 表达式匹配。 支持设置附件存在时间ttl,单位为天,非必填。默认为 0 天,表示附件永久存在。格式为 {file: ttl}。

typeDOWNLOAD时,传入下载文件的文件名。 传路径时,代码中会自动获取文件名,默认下载到工作目录。

typeDELETE时,attachments 可选。传入时支持通配符匹配(如 ./*.zip),仅删除匹配到的附件;缺省则删除全部附件。

输出参数说明

{ "FILES": "file1.txt,file2.txt,file3.txt" }

对于上传附件,会输出 FILES 参数(上传附件的相对地址,多个用英文逗号分隔)到环境变量, 可通过 exports 导出到环境变量。

$: tag_push: - stages: - name: release 上传附件 image: cnbcool/attachments:latest settings: attachments: - "./*.txt" exports: FILES: FILES - name: 输出 files script: echo $FILES

在 云原生构建 中使用

release 附件

下面配置以 push 事件为例,上传 release 附件时需要指定 tag。

main: push: - stages: - name: 指定 tag 上传 release 附件 image: cnbcool/attachments:latest settings: tag: v1.0.0 attachments: - "./test1.txt"

下面配置以 push 事件为例,上传 release 附件时需要指定 tag。且为每个文件指定存在时间。

main: push: - stages: - name: 指定 tag 上传 release 附件,且为每个文件指定存在时间 image: cnbcool/attachments:latest settings: tag: v1.0.0 attachments: "./test1.txt": 1 "./test2.txt": 2 # 直接赋值对象的形式 # attachments: {"./test1.txt": 1, "./test2.txt": 2}

下面配置以 push 事件为例,上传 release 附件时需要指定 tag。且为所有文件指定存在时间。

main: push: - stages: - name: 指定 tag 上传 release 附件,且为每个文件指定存在时间 image: cnbcool/attachments:latest settings: tag: v1.0.0 attachments: - "./test1.txt" - "./test2.txt" ttl: 1

下面配置以 tag_push 事件为例, tag 取环境变量 $CNB_BRANCH,无需指定。

$: tag_push: - stages: - name: release 上传附件 image: cnbcool/attachments:latest settings: attachments: - "./*.txt" - name: 支持否定模式,下面示例会排除 ./test1.txt 文件 image: cnbcool/attachments:latest settings: attachments: - "./*.txt" - "!./test1.txt" - name: release 下载附件 image: cnbcool/attachments:latest settings: type: DOWNLOAD attachments: "./test1.txt,./test2.txt" - name: 删除该 release 的所有附件 image: cnbcool/attachments:latest settings: type: DELETE - name: 删除该 release 的指定附件 image: cnbcool/attachments:latest settings: type: DELETE attachments: - "./test1.txt" - "./*.zip" # attachments: "./test1.txt,./*.zip"

commit 附件

下面配置以 tag_push 事件为例,上传 commit 附件时需要指定 commit ID。

main: tag_push: - stages: - name: 指定 commit 上传 commit 附件 image: cnbcool/attachments:latest settings: commit: abcd1234 attachments: - "./test1.txt"

下面配置以 push 事件为例, commit 取环境变量 $CNB_COMMIT,无需指定。

main: push: - stages: - name: commit 上传附件,多个文件数组写法 image: cnbcool/attachments:latest settings: attachments: - "./test1.txt" - "./test2.txt" - name: commit 上传附件,多个文件逗号分隔 image: cnbcool/attachments:latest settings: attachments: "./test1.txt,./test2.txt" - name: commit 下载附件 image: cnbcool/attachments:latest settings: type: DOWNLOAD attachments: "test1.txt,test2.txt" - name: 删除该 commit 的所有附件 image: cnbcool/attachments:latest settings: type: DELETE - name: 删除该 commit 的指定附件 image: cnbcool/attachments:latest settings: type: DELETE attachments: - "./test1.txt" - "./test2.txt" # 或者逗号分隔 # attachments: "./test1.txt,./test2.txt"

跨仓库上传附件

使用场景举例:某私有仓库构建产物需要上传到公开仓库(构建产物想公开,但仓库不想公开,可创建公开仓库,专门用于公开构建产物) ,可如下配置可实现跨仓上传附件:

$: tag_push: - stages: - name: 跨仓库上传 release 附件 image: cnbcool/attachments:latest settings: attachments: - "./*.txt" # 上传附件到指定仓库 slug: groupname/reponame - name: 跨仓库下载 release 附件 image: cnbcool/attachments:latest settings: type: DOWNLOAD attachments: "./test1.txt,./test2.txt" # 从指定仓库下载附件 slug: groupname/reponame

跨平台上传附件

只支持 CNB 不同平台之间上传,例如可从 CNB PAAS 版上传到 CNB SAAS 版。

$: tag_push: - stages: - name: 跨平台跨仓库上传 release 附件 image: cnbcool/attachments:latest settings: attachments: - "./*.txt" # 跨平台上传附件到指定仓库 slug: groupname/reponame endpoint: https://api.cnb.cool # 跨平台一定需要传入 token token: xxxxx - name: 跨平台跨仓库下载 release 附件 image: cnbcool/attachments:latest settings: type: DOWNLOAD attachments: "./test1.txt,./test2.txt" # 跨平台从指定仓库下载附件 slug: groupname/reponame endpoint: https://api.cnb.cool # 跨平台一定需要传入 token token: xxxxx

使用 Docker 镜像直接上传/下载附件

参数说明:

  • PLUGIN_ATTACHMENTS 附件列表,多个文件逗号分隔
  • PLUGIN_TYPE 上传/下载/删除,UPLOADDOWNLOADDELETE,默认为 UPLOAD
  • PLUGIN_COMMIT commit id,如需上传/下载 commit 附件需要传此参数
  • PLUGIN_TAG tag 名称,如需上传 release 附件需要传此参数。下载 release 附件可以不传此参数,默认下载 latest 对应的 release 附件。如果没有 latest release,不传此参数下载会失败
  • CNB_TOKEN CNB API Token,在 个人设置 -> 访问令牌 中创建并获取 token,注意选中 repo-contents 读写权限
  • CNB_API_ENDPOINT CNB API Endpoint,CNB SAAS 版为 https://api.cnb.cool
  • CNB_WEB_ENDPOINT CNB WEB Endpoint,CNB SAAS 版为 https://cnb.cool
  • CNB_REPO_SLUG 仓库路径,例如 groupname/reponame
  • CNB_IS_TAG 是否为 tag,truefalse,默认为 false。当为 true 时,表示上传/下载 release 附件。当下载 latest release 附件且不指定 PLUGIN_TAG 时,需要传此参数为 true

上传/下载/删除 commit 附件

# commit 上传附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_WEB_ENDPOINT='https://cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_COMMIT='xxx' \ -e PLUGIN_ATTACHMENTS='./xxx.png' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # commit 上传附件且为每个附件指定存在时间 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_WEB_ENDPOINT='https://cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_COMMIT='xxx' \ -e PLUGIN_ATTACHMENTS='{"./xxx.png":2, "./xxx.txt":1}' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # commit 下载附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_COMMIT='xxx' \ -e PLUGIN_ATTACHMENTS='xxx.png' \ -e PLUGIN_TYPE='DOWNLOAD' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # 删除该 commit 的全部附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_COMMIT='xxx' \ -e PLUGIN_TYPE='DELETE' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest

上传/下载/删除 release 附件

需要已经存在 tag,并且有创建过 release

# release 上传附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_WEB_ENDPOINT='https://cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_TAG='v1.0.0' \ -e PLUGIN_ATTACHMENTS='./xxx.png' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # release 上传附件且为所有附件指定存在时间 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_WEB_ENDPOINT='https://cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_TAG='v1.0.0' \ -e PLUGIN_ATTACHMENTS='./xxx.png, ./xxx.txt' \ -e PLUGIN_TTL = 2 \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # release 下载附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_TAG='v1.0.0' \ -e PLUGIN_ATTACHMENTS='xxx.png' \ -e PLUGIN_TYPE='DOWNLOAD' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest # release 下载 latest 附件,不指定 PLUGIN_TAG docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_ATTACHMENTS='xxx.png' \ -e PLUGIN_TYPE='DOWNLOAD' \ -e CNB_IS_TAG='true' \ -v $(pwd):$(pwd) \ -w $(pwd) \ attachments:test # 删除该 release 的全部附件 docker run --rm \ -e TZ=Asia/Shanghai \ -e CNB_TOKEN='xxxx' \ -e CNB_API_ENDPOINT='https://api.cnb.cool' \ -e CNB_REPO_SLUG='groupname/reponame' \ -e PLUGIN_TAG='v1.0.0' \ -e PLUGIN_TYPE='DELETE' \ -v $(pwd):$(pwd) \ -w $(pwd) \ cnbcool/attachments:latest