搜索

1.4. 组装管道

download PDF

管道(pipeline)代表一个 CI/CD 流,由要执行的任务定义。它被设计为在多个应用程序和环境中通用且可重复使用。

管道指定任务如何使用 fromrunAfter 参数相互交互以及它们执行的顺序。它使用 workspaces 字段指定管道中每个任务在执行过程中所需的一个或多个卷。

在本小节中,您将创建一个管道,从 GitHub 获取应用程序的源代码,然后在 OpenShift Container Platform 上构建和部署应用程序。

管道为后端应用程序 pipelines-vote-api 和前端应用程序 pipelines-vote-ui 执行以下任务:

  • 通过引用 git-urlgit-revision 参数,从 Git 存储库中克隆应用程序的源代码。
  • 使用 openshift-pipelines 命名空间中提供的 buildah 任务构建容器镜像。
  • 通过引用 image 参数将镜像推送到 OpenShift 镜像 registry。
  • 通过使用 apply-manifestsupdate-deployment 任务在 OpenShift Container Platform 上部署新镜像。

流程

  1. 复制以下管道 YAML 文件示例内容并保存:

    apiVersion: tekton.dev/v1
    kind: Pipeline
    metadata:
      name: build-and-deploy
    spec:
      workspaces:
      - name: shared-workspace
      params:
      - name: deployment-name
        type: string
        description: name of the deployment to be patched
      - name: git-url
        type: string
        description: url of the git repo for the code of deployment
      - name: git-revision
        type: string
        description: revision to be used from repo of the code for deployment
        default: "pipelines-1.15"
      - name: IMAGE
        type: string
        description: image to be built from the code
      tasks:
      - name: fetch-repository
        taskRef:
          resolver: cluster
          params:
          - name: kind
            value: task
          - name: name
            value: git-clone
          - name: namespace
            value: openshift-pipelines
        workspaces:
        - name: output
          workspace: shared-workspace
        params:
        - name: URL
          value: $(params.git-url)
        - name: SUBDIRECTORY
          value: ""
        - name: DELETE_EXISTING
          value: "true"
        - name: REVISION
          value: $(params.git-revision)
      - name: build-image
        taskRef:
          resolver: cluster
          params:
          - name: kind
            value: task
          - name: name
            value: buildah
          - name: namespace
            value: openshift-pipelines
        workspaces:
        - name: source
          workspace: shared-workspace
        params:
        - name: IMAGE
          value: $(params.IMAGE)
        runAfter:
        - fetch-repository
      - name: apply-manifests
        taskRef:
          name: apply-manifests
        workspaces:
        - name: source
          workspace: shared-workspace
        runAfter:
        - build-image
      - name: update-deployment
        taskRef:
          name: update-deployment
        params:
        - name: deployment
          value: $(params.deployment-name)
        - name: IMAGE
          value: $(params.IMAGE)
        runAfter:
        - apply-manifests

    Pipeline 定义提取 Git 源存储库和镜像 registry 的特定内容。当一个管道被触发并执行时,这些详细信息会作为 params 添加。

  2. 创建管道:

    $ oc create -f <pipeline-yaml-file-name.yaml>

    或者,还可以从 Git 存储库直接执行 YAML 文件:

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.15/01_pipeline/04_pipeline.yaml
  3. 使用 tkn pipeline list 命令来验证管道是否已添加到应用程序中:

    $ tkn pipeline list

    检查输出来验证创建了 build-and-deploy pipeline:

    NAME               AGE            LAST RUN   STARTED   DURATION   STATUS
    build-and-deploy   1 minute ago   ---        ---       ---        ---
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.