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 のコメントとして表示します。
-
手順
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 を 手動 で作成するには、以下の手順を実行します。OpenShift クラスターで、Pipelines as Code コントローラーの公開 URL を抽出します。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
GitLab プロジェクトで、以下の手順を実行します。
- 左側のサイドバーを使用して Settings -> Webhooks に移動します。
- URL を Pipelines as Code コントローラーのパブリック URL に設定します。
Webhook シークレットを追加し、別の場所に書き留めます。
openssl
がローカルマシンにインストールされた状態で、ランダムなシークレットを生成します。$ openssl rand -hex 20
- Let me select individual events をクリックし、Commit comments、Issue comments、Pull request、および Pushes のイベントを選択します。
- Save changes をクリックします。
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>"
Repository
CR を作成します。例:
Repository
CRapiVersion: "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 にあることを前提としています。
オプション: 既存の
Repository
CR の場合、複数の GitLab Webhook シークレットを追加するか、削除されたシークレットの代わりを提供します。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.
-
既存の OpenShift
Secret
オブジェクトのwebhook.secret
キーを更新します。
オプション: 既存の
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 を更新します。Repository
CR でシークレットの名前を見つけます。... spec: git_provider: secret: name: "gitlab-webhook-config" ...
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)\"}}"