全部產品
Search
文件中心

Alibaba Cloud DevOps:產生和引用製品 artifacts

更新時間:Mar 26, 2025

步驟產物 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>]