3.4. 使用经过身份验证的 Git API 指定远程管道、任务或步骤操作


您可以使用 Git 解析器从 Git 存储库指定远程管道、任务或 StepAction 定义。存储库必须包含定义管道或任务的 YAML 文件。您可以使用经过身份验证的 API 安全地访问存储库,它支持用户身份验证。

3.4.1. 为经过身份验证的 API 配置 Git 解析器

对于经过身份验证的 Source Control Management (SCM) API,您必须为经过身份验证的 Git 连接设置配置。

您可以使用 go-scm 库支持的 Git 存储库供应商。并非所有 go-scm 实现都已使用 Git 解析器测试,但以下供应商已知可以正常工作:

  • github.com 和 GitHub Enterprise
  • gitlab.com 和自托管的 Gitlab
  • Gitea
  • Bitbucket 服务器
  • Bitbucket 云
注意
  • 您可以使用经过身份验证的 SCM API 配置 Git 连接。您可以提供一个安全令牌,使集群中的所有用户能够访问一个存储库。另外,您可以为特定管道或任务指定不同的 SCM 供应商和令牌。
  • 如果将 Git 解析器配置为使用经过身份验证的 SCM API,您还可以使用匿名 Git 克隆引用来检索管道和任务。

流程

  1. 要编辑 TektonConfig 自定义资源,请输入以下命令:

    $ oc edit TektonConfig config
  2. TektonConfig 自定义资源中,编辑 pipeline.git-resolver-config spec:

    apiVersion: operator.tekton.dev/v1alpha1
    kind: TektonConfig
    metadata:
      name: config
    spec:
      pipeline:
        git-resolver-config:
          default-revision: main 1
          fetch-timeout: 1m 2
          scm-type: github 3
          server-url: api.internal-github.com 4
          api-token-secret-name: github-auth-secret 5
          api-token-secret-key: github-auth-key 6
          api-token-secret-namespace: github-auth-namespace 7
          default-org: tektoncd 8
    1
    如果未指定,使用默认 Git 修订。
    2
    单个 Git 克隆解析可能需要的最大时间,如 1m2s700ms。Red Hat OpenShift Pipelines 还在所有解析请求中强制实施全局最大超时时间为 1 分钟。
    3
    SCM 提供程序类型。
    4
    用于经过身份验证的 SCM API 的基本 URL。如果您使用 github.comgitlab.com 或 BitBucket Cloud,则不需要此设置。
    5
    包含 SCM 提供程序 API 令牌的 secret 名称。
    6
    包含令牌的令牌 secret 中的密钥。
    7
    包含令牌 secret 的命名空间(如果不是 default)。
    8
    可选:使用经过身份验证的 API 时存储库的默认机构。如果您没有在解析器参数中指定机构,则使用此机构。
注意

使用经过身份验证的 SCM API 需要 scm-type,api-token-secret-name, 和 api-token-secret-key 设置。

3.4.2. 配置多个 Git 供应商

您可以配置多个 Git 供应商,也可以为同一 Git 供应商添加多个配置,以便在不同的任务运行和管道运行中使用。

使用您的唯一标识符密钥前缀在 TektonConfig 自定义资源(CR)中添加详情。

流程

  1. 运行以下命令来编辑 TektonConfig CR:

    $ oc edit TektonConfig config
  2. TektonConfig CR 中,编辑 pipeline.git-resolver-config spec:

    apiVersion: operator.tekton.dev/v1alpha1
    kind: TektonConfig
    metadata:
      name: config
    spec:
    # ...
      pipeline:
        git-resolver-config:
          # configuration 1 1
          fetch-timeout: "1m"
          default-url: "https://github.com/tektoncd/catalog.git"
          default-revision: "main"
          scm-type: "github"
          server-url: ""
          api-token-secret-name: ""
          api-token-secret-key: ""
          api-token-secret-namespace: "default"
          default-org: ""
          # configuration 2 2
          test1.fetch-timeout: "5m"
          test1.default-url: ""
          test1.default-revision: "stable"
          test1.scm-type: "github"
          test1.server-url: "api.internal-github.com"
          test1.api-token-secret-name: "test1-secret"
          test1.api-token-secret-key: "token"
          test1.api-token-secret-namespace: "test1"
          test1.default-org: "tektoncd"
          # configuration 3 3
          test2.fetch-timeout: "10m"
          test2.default-url: ""
          test2.default-revision: "stable"
          test2.scm-type: "gitlab"
          test2.server-url: "api.internal-gitlab.com"
          test2.api-token-secret-name: "test2-secret"
          test2.api-token-secret-key: "pat"
          test2.api-token-secret-namespace: "test2"
          test2.default-org: "tektoncd-infra"
    # ...
    1
    如果未提供 configKey 键,或者键附带 默认值,则要使用的默认配置。
    2
    如果 configKey 键通过 test1 值传递,则使用配置。
    3
    如果 configKey 键通过 test2 值传递,则使用配置。
    警告

    不支持带有 . 符号的 configKey 值。如果您尝试传递包含 . 符号的 configKey 值,则您传递了该值的 TaskRunPipelineRun 资源将无法运行。

3.4.3. 使用带有经过身份验证的 SCM API 的 Git 解析器指定远程管道、任务或步骤操作

在创建管道运行时,您可以使用经过身份验证的 SCM API 从 Git 存储库指定远程管道。在创建管道或任务运行时,您可以从 Git 仓库指定远程任务。在任务中创建步骤时,您可以从 Git 存储库中引用远程 StepAction 定义。

先决条件

  • 如果要使用经过身份验证的 SCM API,您必须为 Git 解析器配置经过身份验证的 Git 连接。

流程

  • 要指定 Git 存储库中的远程管道、任务或 StepAction 定义,请在 pipelineRef,taskRef, 或 step.ref spec 中使用以下引用格式:

    # ...
      resolver: git
      params:
      - name: org
        value: <git_organization_name>
      - name: repo
        value: <git_repository_name>
      - name: revision
        value: <branch_name>
      - name: pathInRepo
        value: <path_in_repository>
    # ...
    表 3.4. Git 解析器支持的参数
    参数描述示例值

    org

    使用经过身份验证的 SCM API 时存储库的机构。

    tektoncd

    repo

    使用经过身份验证的 SCM API 时的存储库名称。

    test-infra

    revision

    存储库中的 Git 修订。您可以指定分支名称、标签名称或提交 SHA 哈希。

    aeb957601cf41c012be462827053a21a420befca
    main
    v0.38.2

    pathInRepo

    存储库中的 YAML 文件的路径名称。

    task/golang-build/0.3/golang-build.yaml

    注意

    要匿名克隆和获取存储库,请使用 url 参数。要使用经过身份验证的 SCM API,请使用 repo 参数。不要同时指定 url 参数和 repo 参数。

    如果管道或任务需要额外的参数,在管道、管道运行或任务运行规格的 params 部分中指定这些参数的值。pipelineReftaskRef 规格的 params 部分必须包含解析器支持的参数。

例子

以下示例管道运行引用 Git 存储库中的远程管道:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: git-pipeline-reference-demo
spec:
  pipelineRef:
    resolver: git
    params:
    - name: org
      value: tektoncd
    - name: repo
      value: catalog
    - name: revision
      value: main
    - name: pathInRepo
      value: pipeline/simple/0.1/simple.yaml
  params:
  - name: name
    value: "testPipelineRun"
  - name: sample-pipeline-parameter
    value: test

以下示例管道引用 Git 存储库中的远程任务:

apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: pipeline-with-git-task-reference-demo
spec:
  tasks:
  - name: "git-task-reference-demo"
    taskRef:
      resolver: git
      params:
      - name: org
        value: tektoncd
      - name: repo
        value: catalog
      - name: revision
        value: main
      - name: pathInRepo
        value: task/git-clone/0.6/git-clone.yaml
    params:
    - name: sample-task-parameter
      value: test

以下示例任务运行引用 Git 存储库中的远程任务:

apiVersion: tekton.dev/v1
kind: TaskRun
metadata:
  name: git-task-reference-demo
spec:
  taskRef:
    resolver: git
    params:
    - name: org
      value: tektoncd
    - name: repo
      value: catalog
    - name: revision
      value: main
    - name: pathInRepo
      value: task/git-clone/0.6/git-clone.yaml
  params:
  - name: sample-task-parameter
    value: test

以下示例任务包含引用 Git 存储库中的 StepAction 定义的步骤:

apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: git-stepaction-reference-demo
spec:
  steps:
  - name: example-step
    ref:
      resolver: git
      - name: org
        value: openshift-pipelines
      - name: repo
        value: tektoncd-catalog
      - name: revision
        value: p
      - name: pathInRepo
        value: stepactions/stepaction-git-clone/0.4.1/stepaction-git-clone.yaml
    params:
    - name: sample-stepaction-parameter
      value: test

3.4.4. 指定多个 Git 供应商

您可以在创建 TaskRunPipelineRun 资源时传递唯一的 configKey 参数来指定多个 Git 供应商。

如果没有传递 configKey 参数,则会使用默认配置。您还可以通过将 configKey 值设置为 default 来指定默认配置。

警告

不支持带有 . 符号的 configKey 值。如果您尝试传递包含 . 符号的 configKey 值,则您传递了该值的 TaskRunPipelineRun 资源将无法运行。

先决条件

  • 通过 Tektonconfig 自定义资源配置多个 Git 供应商。如需更多信息,请参阅"配置多个 Git 提供程序"。

流程

  • 要指定 Git 供应商,在 pipelineReftaskRef spec 中使用以下引用格式:

    # ...
      resolver: git
      params:
      # ...
      - name: configKey
        value: <your_unique_key> 1
    # ...
    1
    您与其中一个配置键匹配的唯一键,如 test1

3.4.5. 使用带有经过身份验证的 SCM API 的 Git 解析器覆盖 Git 解析器配置来指定远程管道或任务

您可以覆盖特定管道运行或任务中的初始配置设置,以根据不同的用例自定义行为。您可以使用此方法访问没有在 TektonConfig 自定义资源(CR)中配置的经过身份验证的供应商。

以下示例任务运行引用 Git 存储库中的远程任务来覆盖之前的解析器配置:

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: git-task-reference-demo
spec:
  taskRef:
    resolver: git
    params:
    - name: org
      value: tektoncd
    - name: repo
      value: catalog
    - name: revision
      value: main
    - name: pathInRepo
      value: task/git-clone/0.6/git-clone.yaml
    - name: token
      value: my-secret-token
    - name: tokenKey
      value: token
    - name: scmType
      value: github
    - name: serverURL
      value: https://ghe.mycompany.com
表 3.5. 覆盖 Git 解析器支持的参数
参数描述示例值

org

仓库的组织。

tektoncd

repo

存储库名称。

catalog

revision

存储库中的 Git 修订。您可以指定分支名称、标签名称或提交 SHA 哈希。

main

pathInRepo

存储库中的 YAML 文件的路径名称。

task/git-clone/0.6/git-clone.yaml

token

用于身份验证的 secret 名称。

my-secret-token

tokenKey

令牌的密钥名称。

token

scmType

SCM (源控制管理)系统的类型。

github

serverURL

托管存储库的服务器的 URL。

https://ghe.mycompany.com

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.