步骤产物 artifacts 为一系列关于制品文件的信息组合,用于保存流水线运行过程中的产物,以供后续任务 Job、阶段 Stage、甚至流水线 Pipeline 引用。
生成制品
Flow 流水线镜像构建、构建物上传等步骤目前会生成以下两种类型产物:
镜像产物:镜像构建任务构建完成后,上传至 ACR 或者私有镜像仓库的 Docker 镜像。制品信息结构如下:
{ "artifact": "yunxiao-demo", //镜像名称 "type": "dockerImage", //制品类型 "dockerUrl": "registry.cn-beijing.aliyuncs.com/yunxiao/yunxiao-demo:1.0", //镜像地址 "dockerTag": "1.0" //镜像tag }云效公共存储空间通用制品: 构建完成后,使用构建物上传归档至云效公共存储空间的通用制品。制品信息结构如下:
{ "artifact": "yunxiao-demo", //制品名称 "type": "flowPublic", //制品类型 "downloadUrl": "https://sdfdff", //制品下载地址 "md5": "dffff" //制品MD5 }
引用制品
Flow 流水线提供以下几种方式引用制品:
同一条流水线内跨 Job 或跨 stage 引用制品:通常适用于同一条流水线前序任务构建、后续任务部署,CI/CD同一条流水线场景。
跨流水线引用制品:通常适用于源流水线构建产物,目标流水线引用产物部署场景(如生产流水线直接引用预发验证通过的镜像),CI、CD分离场景。
示例1:同一条流水线内跨 Job 或跨 Stage 引用制品
以下是一个类型为 Docker 镜像,同一条流水线内跨 Job 的 artifact 生产和使用示例:
sources: my_repo: …… stages: my_stage: name: 构建并部署 jobs: build_job: name: 镜像构建任务 runsOn: group: public/ap-southeast-1 container: build-steps-public-registry.ap-southeast-1.cr.aliyuncs.com/build-steps/alinux3:latest steps: private_registry_docker_setup: name: 登录镜像仓库 step: DockerLogin with: dockerRepository: registry.cn-hangzhou.aliyuncs.com serviceConnection: <your-service-connection-id> docker_build_push_step: name: 镜像构建推送 step: DockerBuildPush with: artifact: my_image image: registry.cn-hangzhou.aliyuncs.com/ns/demo:1.0 dockerfilePath: Dockerfile cacheType: remote deploy_job: name: Kubernetes 发布任务 runsOn: group: public/ap-southeast-1 container: build-steps-public-registry.ap-southeast-1.cr.aliyuncs.com/build-steps/alinux3:latest steps: kubectl_apply: step: KubectlApply name: Kubectl 发布 with: kubernetesCluster: <your-kubernetes-id> kubectlVersion: "1.16.4" namespace: default yamlPath: app-configs/manifest-app variables: - key: image value: $[jobs.build_job.docker_build_push_step.artifacts.my_image] # 引用 docker_build_step 步骤生成的镜像 my-image以下是一个类型为云效公共存储空间通用制品,同一条流水线内跨 stage 的 artifact 生产和使用示例:
sources: my_repo: …… stages: build_stage: name: 构建阶段 jobs: build_job: name: 构建任务 runsOn: group: public/ap-southeast-1 container: build-steps-public-registry.ap-southeast-1.cr.aliyuncs.com/build-steps/alinux3:latest steps: setup_java_step: name: "安装Java环境" step: SetupJava with: jdkVersion: "1.8" mavenVersion: "3.5.2" command_step: name: "执行命令" step: Command with: run: | mvn -B clean package -Dmaven.test.skip=true -Dautoconfig.skip upload_artifact_step: name: "构建物上传" step: ArtifactUpload # 步骤 ArtifactUpload 会上传生成 artifact with: uploadType: flowPublic artifact: "Artifacts_${PIPELINE_ID}" filePath: - target/ deploy_stage: name: 部署阶段 jobs: deploy_job: name: 主机组部署任务 component: VMDeploy with: artifact: $[stages.build_stage.build_job.upload_artifact_step.artifacts.Artifacts_${PIPELINE_ID}] # 使用步骤 upload_step 生成的制品 default machineGroup: <your-machine-group-id> artifactDownloadPath: /home/admin/app/package.tgz executeUser: root run: | mkdir -p /home/admin/application/ tar zxvf /home/admin/app/package.tgz -C /home/admin/application/ sh /home/admin/application/deploy.sh restart
示例2:跨流水线引用制品
以下示例为源流水线 A 从源代码通过镜像构建任务生成镜像,目标流水线 B 引用 A 的镜像进行部署。跨流水线引用产物,通常用于预发阶段镜像通过验证后,生产阶段流水线直接使用预发镜像发布场景。
源流水线 A
sources: my_repo: type: gitSample name: Java示例代码源 endpoint: https://atomgit.com/flow-example/spring-boot.git branch: master stages: build_stage: name: "构建" jobs: build_job: name: 镜像构建并推送至自定义仓库 runsOn: group: public/ap-southeast-1 container: build-steps-public-intl-test-registry.ap-southeast-1.cr.aliyuncs.com/build-steps/alinux3:latest steps: private_registry_docker_setup: name: 登录镜像仓库 step: DockerLogin with: dockerRepository: registry.cn-hangzhou.aliyuncs.com serviceConnection: <your-service-connection-id> docker_build_push_step: name: 镜像构建推送 step: DockerBuildPush with: artifact: my_image image: registry.cn-hangzhou.aliyuncs.com/ns/demo:1.0 dockerfilePath: Dockerfile cacheType: remote目标流水线 B
sources: pipeline_a: type: flowPipeline name: 源流水线A flowPipeline: n86bmctmphs84icj # 源流水线 A 的 流水线 ID build: lastSuccessfulBuild triggerEvents: buildSuccess stages: kubectl_apply_stage: name: "部署" jobs: deploy_job: name: "Kubectl 发布" runsOn: group: public/ap-southeast-1 container: build-steps-public-registry.ap-southeast-1.cr.aliyuncs.com/build-steps/alinux3:latest steps: kubectl_apply_step: name: "Kubectl 发布" step: "KubectlApply" with: kubernetesCluster: <your-kubernetes-id> kubectlVersion: "1.27.9" namespace: default yamlPath: app-configs/manifest-app variables: - key: image value: $[sources.pipeline_a.stages.build_stage.build_job.docker_build_push_step.artifacts.my_image]
详细说明
生成步骤产物 artifact
通过云效提供的构建物上传、镜像构建等步骤打包上传镜像制品。具体查看流水线步骤 steps。
引用产物 artifact
同一条流水线同一阶段内跨 Job 引用产物:
$[jobs.<job_id>.<step_id>.artifacts.<artifact>]同一条流水线跨 Stage 引用产物:
$[stages.<stage_id>.<job_id>.<step_id>.artifacts.<artifact>]跨流水线引用产物(其中 sources 类型为 flowPipeline):
$[sources.<source_id>.stages.<stage_id>.<job_id>.<step_id>.artifacts.<artifact>]