|
| 1 | +// This module is included in the following assembly: |
| 2 | +// |
| 3 | +// *openshift-docs/cicd/pipelines/authenticating-pipelines-using-git-secret.adoc |
| 4 | + |
| 5 | +[id="op-configuring-ssh-authentication-for-git_{context}"] |
| 6 | += Configuring SSH authentication for Git |
| 7 | + |
| 8 | +[role="_abstract"] |
| 9 | +For a pipeline to retrieve resources from repositories configured with SSH keys, you must configure the SSH-based authentication for that pipeline. |
| 10 | + |
| 11 | +To configure SSH-based authentication for a pipeline, update the `secret.yaml`, `serviceaccount.yaml`, and `run.yaml` files with the credentials from the SSH private key for the specified repository. When you complete this process, {pipelines-shortname} can use that information to retrieve the specified pipeline resources. |
| 12 | + |
| 13 | +[NOTE] |
| 14 | +==== |
| 15 | +Consider using SSH-based authentication rather than basic authentication. |
| 16 | +==== |
| 17 | + |
| 18 | +.Procedure |
| 19 | + |
| 20 | +. Generate an link:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent[SSH private key], or copy an existing private key, which is usually available in the `~/.ssh/id_rsa` file. |
| 21 | +. In the `secret.yaml` file, set the value of `ssh-privatekey` to the name of the SSH private key file, and set the value of `known_hosts` to the name of the known hosts file. |
| 22 | ++ |
| 23 | +[source,yaml,subs="attributes+"] |
| 24 | +---- |
| 25 | +apiVersion: v1 |
| 26 | +kind: Secret |
| 27 | +metadata: |
| 28 | + name: ssh-key <1> |
| 29 | + annotations: |
| 30 | + tekton.dev/git-0: github.com |
| 31 | +type: kubernetes.io/ssh-auth |
| 32 | +stringData: |
| 33 | + ssh-privatekey: <2> |
| 34 | + known_hosts: <3> |
| 35 | +---- |
| 36 | +<1> Name of the secret containing the SSH private key. In this example, `ssh-key`. |
| 37 | +<2> Name of the file containing the SSH private key string. |
| 38 | +<3> Name of the file containing a list of known hosts. |
| 39 | ++ |
| 40 | +[CAUTION] |
| 41 | +==== |
| 42 | +If you omit the private key, {pipelines-shortname} accepts the public key of any server. |
| 43 | +==== |
| 44 | ++ |
| 45 | +. Optional: To specify a custom SSH port, add `:<port number>` to the end of the `annotation` value. For example, `tekton.dev/git-0: github.com:2222`. |
| 46 | +. In the `serviceaccount.yaml` file, associate the `ssh-key` secret with the `build-bot` service account. |
| 47 | ++ |
| 48 | +[source,yaml,subs="attributes+"] |
| 49 | +---- |
| 50 | +apiVersion: v1 |
| 51 | +kind: ServiceAccount |
| 52 | +metadata: |
| 53 | + name: build-bot <1> |
| 54 | +secrets: |
| 55 | + - name: ssh-key <2> |
| 56 | +---- |
| 57 | +<1> Name of the service account. In this example, `build-bot`. |
| 58 | +<2> Name of the secret containing the SSH private key. In this example, `ssh-key`. |
| 59 | ++ |
| 60 | +. In the `run.yaml` file, associate the service account with a task run or a pipeline run. |
| 61 | ++ |
| 62 | +* Associate the service account with a task run: |
| 63 | ++ |
| 64 | +[source,yaml,subs="attributes+"] |
| 65 | +---- |
| 66 | +apiVersion: tekton.dev/v1beta1 |
| 67 | +kind: TaskRun |
| 68 | +metadata: |
| 69 | + name: build-push-task-run-2 <1> |
| 70 | +spec: |
| 71 | + serviceAccountName: build-bot <2> |
| 72 | + taskRef: |
| 73 | + name: build-push <3> |
| 74 | +---- |
| 75 | +<1> Name of the task run. In this example, `build-push-task-run-2`. |
| 76 | +<2> Name of the service account. In this example, `build-bot`. |
| 77 | +<3> Name of the task. In this example, `build-push`. |
| 78 | ++ |
| 79 | +* Associate the service account with a pipeline run: |
| 80 | ++ |
| 81 | +[source,yaml,subs="attributes+"] |
| 82 | +---- |
| 83 | +apiVersion: tekton.dev/v1beta1 |
| 84 | +kind: PipelineRun |
| 85 | +metadata: |
| 86 | + name: demo-pipeline <1> |
| 87 | + namespace: default |
| 88 | +spec: |
| 89 | + serviceAccountName: build-bot <2> |
| 90 | + pipelineRef: |
| 91 | + name: demo-pipeline <3> |
| 92 | +---- |
| 93 | +<1> Name of the pipeline run. In this example, `demo-pipeline`. |
| 94 | +<2> Name of the service account. In this example, `build-bot`. |
| 95 | +<3> Name of the pipeline. In this example, `demo-pipeline`. |
| 96 | ++ |
| 97 | +. Apply the changes. |
| 98 | ++ |
| 99 | +[source,terminal] |
| 100 | +---- |
| 101 | +$ kubectl apply --filename secret.yaml,serviceaccount.yaml,run.yaml |
| 102 | +---- |
0 commit comments