3.3. GitLab での Pipelines as Code の使用
組織またはプロジェクトが優先プラットフォームとして GitLab を使用する場合は、GitLab の Webhook を使用してリポジトリーの Pipelines as Code を使用できます。
前提条件
- Pipelines as Code がクラスターにインストールされている。
承認には、GitLab のプロジェクトまたは組織のマネージャーとして Personal Access Token トークンを生成します。
注記-
tkn pacCLI を使用して Webhook を設定する必要がある場合は、admin:repo_hookスコープをトークンに追加します。 - 特定のプロジェクトを対象とするトークンを使用しても、フォークされたリポジトリーから送信されたマージリクエスト (MR) に API でのアクセスはできません。このような場合、Pipelines as Code はパイプラインの結果を MR のコメントとして表示します。
-
手順
Webhook を設定し、
リポジトリーカスタムリソース (CR) を作成します。tkn pacCLI ツールを使用して 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 を設定して
RepositoryCR を 手動 で作成するには、以下の手順を実行します。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>"RepositoryCR を作成します。例:
RepositoryCRapiVersion: "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オブジェクトとRepositoryCR が同じ namespace にあることを前提としています。
オプション: 既存の
RepositoryCR の場合、複数の GitLab Webhook シークレットを追加するか、削除されたシークレットの代わりを提供します。tkn pacCLI ツールを使用して Webhook を追加します。例:
tkn pacCLI を使用した 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キーを更新します。
オプション: 既存の
RepositoryCR の場合は、Personal Access Token を更新します。tkn pacCLI ツールを使用して Personal Access Token を更新します。例:
tkn pacCLI を使用した 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.または、
RepositoryCR を変更して Personal Access Token トークンを更新します。RepositoryCR でシークレットの名前を見つけます。... spec: git_provider: secret: name: "gitlab-webhook-config" ...oc patchコマンドを使用して、$target_namespacenamespace の$NEW_TOKENの値を更新します。$ oc -n $target_namespace patch secret gitlab-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"