3.3. GitLab에서 Pipeline을 코드로 사용
조직 또는 프로젝트에서 GitLab을 기본 플랫폼으로 사용하는 경우 GitLab에서 Webhook가 있는 리포지토리의 코드로 Pipeline을 사용할 수 있습니다.
사전 요구 사항
- Pipeline as Code가 클러스터에 설치되어 있는지 확인합니다.
승인을 위해 GitLab에서 프로젝트 또는 조직 관리자로 개인 액세스 토큰을 생성합니다.
참고-
tkn pac
CLI를 사용하여 Webhook를 구성하려면admin:repo_hook
범위를 토큰에 추가합니다. - 특정 프로젝트에 토큰 범위를 사용하면 분기된 리포지토리에서 전송된 병합 요청(MR)에 대한 API 액세스를 제공할 수 없습니다. 이러한 경우 Code로 Pipeline은 파이프라인의 결과를 MR에 대한 주석으로 표시합니다.
-
프로세스
Webhook를 구성하고
Repository
CR(사용자 정의 리소스)을 생성합니다.tkn pac
CLI 툴을 사용하여 Webhook를 구성하고리포지토리
CR을 자동으로 생성하려면 다음 명령을 사용합니다.Copy to Clipboard Copied! Toggle word wrap Toggle overflow tkn pac create repo
$ tkn pac create repo
대화형 출력 샘플
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ? 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.
? 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를 구성하고
리포지토리
CR을 수동으로 생성하려면 다음 단계를 수행합니다.OpenShift 클러스터에서 Pipeline의 공용 URL을 코드 컨트롤러로 추출합니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
GitLab 프로젝트에서 다음 단계를 수행합니다.
- 왼쪽 사이드바를 사용하여 설정 -> Webhook 로 이동합니다.
- URL 을 코드 컨트롤러 공용 URL로 Pipeline으로 설정합니다.
웹 후크 시크릿을 추가하고 대체 위치에서 기록해 둡니다. 로컬 시스템에
openssl
을 설치하면 임의의 보안을 생성합니다.Copy to Clipboard Copied! Toggle word wrap Toggle overflow openssl rand -hex 20
$ openssl rand -hex 20
- 개별 이벤트 선택을 클릭하고 Commit comments,Issue comments,Pull request, Pushes 이벤트를 선택합니다.
- 변경 사항 저장을 클릭합니다.
OpenShift 클러스터에서 개인 액세스 토큰 및 Webhook 시크릿을 사용하여
Secret
오브젝트를 생성합니다.Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc -n target-namespace create secret generic gitlab-webhook-config \ --from-literal provider.token="<GITLAB_PERSONAL_ACCESS_TOKEN>" \ --from-literal webhook.secret="<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>"
리포지토리
CR을 생성합니다.예:
리포지토리
CRCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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/" 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
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/
입니다.
참고-
코드로서의 파이프라인은 OpenShift
Secret
오브젝트 및Repository
CR이 동일한 네임스페이스에 있다고 가정합니다.
선택 사항: 기존
리포지토리
CR의 경우 GitLab Webhook 보안을 여러 개 추가하거나 삭제된 보안을 대신 제공합니다.tkn pac
CLI 툴을 사용하여 Webhook를 추가합니다.예:
tkn pac
CLI를 사용하여 추가 Webhook 추가Copy to Clipboard Copied! Toggle word wrap Toggle overflow tkn pac webhook add -n repo-pipelines
$ tkn pac webhook add -n repo-pipelines
대화형 출력 샘플
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ✓ 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.
✓ 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
키를 업데이트합니다.
선택 사항: 기존
리포지토리
CR의 경우 개인 액세스 토큰을 업데이트합니다.tkn pac
CLI 툴을 사용하여 개인 액세스 토큰을 업데이트합니다.예:
tkn pac
CLI를 사용하여 개인 액세스 토큰 업데이트Copy to Clipboard Copied! Toggle word wrap Toggle overflow tkn pac webhook update-token -n repo-pipelines
$ tkn pac webhook update-token -n repo-pipelines
대화형 출력 샘플
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ? Please enter your personal access token: **************************************** 🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.
? Please enter your personal access token: **************************************** 🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.
또는
Repository
CR을 수정하여 개인 액세스 토큰을 업데이트합니다.Repository
CR에서 시크릿 이름을 찾습니다.Copy to Clipboard Copied! Toggle word wrap Toggle overflow ... spec: git_provider: secret: name: "gitlab-webhook-config" ...
... spec: git_provider: secret: name: "gitlab-webhook-config" ...
oc patch
명령을 사용하여$target_namespace
네임스페이스에서$NEW_TOKEN
값을 업데이트합니다.Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc -n $target_namespace patch secret gitlab-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"
$ oc -n $target_namespace patch secret gitlab-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"
추가 리소스