3.3. GitLab での Pipelines as Code の使用


組織またはプロジェクトが優先プラットフォームとして GitLab を使用する場合は、GitLab の Webhook を使用してリポジトリーの Pipelines as Code を使用できます。

前提条件

  • Pipelines as Code がクラスターにインストールされている。
  • 承認には、GitLab のプロジェクトまたは組織のマネージャーとして Personal Access Token トークンを生成します。

    注記
    • tkn pac CLI を使用して Webhook を設定する必要がある場合は、admin:repo_hook スコープをトークンに追加します。
    • 特定のプロジェクトを対象とするトークンを使用しても、フォークされたリポジトリーから送信されたマージリクエスト (MR) に API でのアクセスはできません。このような場合、Pipelines as Code はパイプラインの結果を MR のコメントとして表示します。

手順

  1. Webhook を設定し、リポジトリー カスタムリソース (CR) を作成します。

    • tkn pac CLI ツールを使用して webhook を設定し、リポジトリー CR を 自動的に 作成するには、次のコマンドを使用します。

      $ tkn pac create repo

      対話型出力の例

      ? Enter the Git repository url (default: https://gitlab.com/owner/repo):
      ? Please enter the namespace where the pipeline should run (default: repo-pipelines):
      ! Namespace repo-pipelines is not found
      ? Would you like me to create the namespace repo-pipelines? Yes
      ✓ Repository repositories-project has been created in repo-pipelines namespace
      ✓ Setting up GitLab Webhook for Repository https://gitlab.com/owner/repo
      ? Please enter the project ID for the repository you want to be configured,
        project ID refers to an unique ID (e.g. 34405323) shown at the top of your GitLab project : 17103
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: lFjHIEcaGFlF):  lFjHIEcaGFlF
      ℹ ️You now need to create a GitLab personal access token with `api` scope
      ℹ ️Go to this URL to generate one https://gitlab.com/-/profile/personal_access_tokens, see https://is.gd/rOEo9B for documentation
      ? Please enter the GitLab access token:  **************************
      ? Please enter your GitLab API URL::  https://gitlab.com
      ✓ Webhook has been created on your repository
      🔑 Webhook Secret repositories-project has been created in the repo-pipelines namespace.
      🔑 Repository CR repositories-project has been updated with webhook secret in the repo-pipelines namespace
      ℹ Directory .tekton has been created.
      ✓ A basic template has been created in /home/Go/src/gitlab.com/repositories/project/.tekton/pipelinerun.yaml, feel free to customize it.

    • Webhook を設定して Repository CR を 手動 で作成するには、以下の手順を実行します。

      1. OpenShift クラスターで、Pipelines as Code コントローラーの公開 URL を抽出します。

        $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
      2. GitLab プロジェクトで、以下の手順を実行します。

        1. 左側のサイドバーを使用して Settings -> Webhooks に移動します。
        2. URL を Pipelines as Code コントローラーのパブリック URL に設定します。
        3. Webhook シークレットを追加し、別の場所に書き留めます。openssl がローカルマシンにインストールされた状態で、ランダムなシークレットを生成します。

          $ openssl rand -hex 20
        4. Let me select individual events をクリックし、Commit commentsIssue commentsPull request、および Pushes のイベントを選択します。
        5. Save changes をクリックします。
      3. OpenShift クラスターで、個人アクセストークンおよび Webhook シークレットを使用して Secret オブジェクトを作成します。

        $ oc -n target-namespace create secret generic gitlab-webhook-config \
          --from-literal provider.token="<GITLAB_PERSONAL_ACCESS_TOKEN>" \
          --from-literal webhook.secret="<WEBHOOK_SECRET>"
      4. Repository CR を作成します。

        例: Repository CR

        apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
        kind: Repository
        metadata:
          name: my-repo
          namespace: target-namespace
        spec:
          url: "https://gitlab.com/owner/repo" # The repository URL
          git_provider:
            #url: "https://gitlab.example.com/" 1
            secret:
              name: "gitlab-webhook-config"
              key: "provider.token" # Set this if you have a different key in your secret
            webhook_secret:
              name: "gitlab-webhook-config"
              key: "webhook.secret" # Set this if you have a different key for your secret

        1
        GitLab.com ではなく GitLab のプライベートインスタンスを使用している場合は、このフィールドのコメントを解除して、GitLab API の URL に設定します。GitLab API はリポジトリーと同じホストです。たとえば、リポジトリーが https://gitlab.example.com/owner/repo の場合、API URL は https://gitlab.example.com/ です。
    注記
    • Pipelines as Code は、OpenShift Secret オブジェクトと Repository CR が同じ namespace にあることを前提としています。
  2. オプション: 既存の Repository CR の場合、複数の GitLab Webhook シークレットを追加するか、削除されたシークレットの代わりを提供します。

    1. tkn pac CLI ツールを使用して Webhook を追加します。

      例: tkn pac CLI を使用した Webhook の追加

      $ tkn pac webhook add -n repo-pipelines

      対話型出力の例

      ✓ Setting up GitLab Webhook for Repository https://gitlab.com/owner/repo
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: AeHdHTJVfAeH):  AeHdHTJVfAeH
      ✓ Webhook has been created on repository owner/repo
      🔑 Secret owner-repo has been updated with webhook secert in the repo-pipelines namespace.

    2. 既存の OpenShift Secret オブジェクトの webhook.secret キーを更新します。
  3. オプション: 既存の Repository CR の場合は、Personal Access Token を更新します。

    • tkn pac CLI ツールを使用して Personal Access Token を更新します。

      例: tkn pac CLI を使用した Personal Access Token の更新

      $ tkn pac webhook update-token -n repo-pipelines

      対話型出力の例

      ? Please enter your personal access token:  ****************************************
      🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.

    • または、Repository CR を変更して Personal Access Token を更新します。

      1. Repository CR でシークレットの名前を見つけます。

        ...
        spec:
          git_provider:
            secret:
              name: "gitlab-webhook-config"
        ...
      2. oc patch コマンドを使用して、$target_namespace namespace の $NEW_TOKEN の値を更新します。

        $ oc -n $target_namespace patch secret gitlab-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.