|
| 1 | +--- |
| 2 | +weight: 50 |
| 3 | +--- |
| 4 | + |
| 5 | +# Prepare Git Credential |
| 6 | + |
| 7 | +This guide shows you how to create a git credential Secret that help you run your Tekton Tasks and Pipelines. |
| 8 | + |
| 9 | +This document will use the configuration of a `git-clone` Task as an example. |
| 10 | +If you are using a different Task, you can refer to the steps here and modify the `taskRef.name` and `workspaces` to match those defined in your Task. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- kubectl installed and configured to access the cluster. |
| 15 | +- Permissions to read and write Secrets. |
| 16 | + |
| 17 | +## Steps |
| 18 | + |
| 19 | +### Optional 1: Using ssh Credentials |
| 20 | + |
| 21 | +This Task supports fetching private repositories. There are three ways to |
| 22 | +authenticate: |
| 23 | + |
| 24 | +1. The simplest approach is to bind an `ssh-directory` workspace to this |
| 25 | +Task. The workspace should contain private keys (e.g. `id_rsa`), `config` |
| 26 | +and `known_hosts` files - anything you need to interact with your git remote |
| 27 | +via SSH. It's **strongly** recommended that you use Kubernetes `Secrets` to |
| 28 | +hold your credentials and bind to this workspace. |
| 29 | + |
| 30 | + In a TaskRun that would look something like this: |
| 31 | + |
| 32 | + ```yaml |
| 33 | + kind: TaskRun |
| 34 | + spec: |
| 35 | + workspaces: |
| 36 | + - name: ssh-directory |
| 37 | + secret: |
| 38 | + secretName: my-ssh-credentials |
| 39 | + ``` |
| 40 | +
|
| 41 | + And in a Pipeline and PipelineRun it would look like this: |
| 42 | +
|
| 43 | + ```yaml |
| 44 | + kind: Pipeline |
| 45 | + spec: |
| 46 | + workspaces: |
| 47 | + - name: ssh-creds |
| 48 | + # ... |
| 49 | + tasks: |
| 50 | + - name: fetch-source |
| 51 | + taskRef: |
| 52 | + name: git-clone |
| 53 | + workspaces: |
| 54 | + - name: ssh-directory |
| 55 | + workspace: ssh-creds |
| 56 | + # ... |
| 57 | + --- |
| 58 | + kind: PipelineRun |
| 59 | + spec: |
| 60 | + workspaces: |
| 61 | + - name: ssh-creds |
| 62 | + secret: |
| 63 | + secretName: my-ssh-credentials |
| 64 | + # ... |
| 65 | + ``` |
| 66 | + |
| 67 | + The `Secret` would appear the same in both cases - structured like a `.ssh` |
| 68 | + directory: |
| 69 | + |
| 70 | + ```yaml |
| 71 | + kind: Secret |
| 72 | + apiVersion: v1 |
| 73 | + metadata: |
| 74 | + name: my-ssh-credentials |
| 75 | + data: |
| 76 | + id_rsa: # ... base64-encoded private key ... |
| 77 | + known_hosts: # ... base64-encoded known_hosts file ... |
| 78 | + config: # ... base64-encoded ssh config file ... |
| 79 | + ``` |
| 80 | +
|
| 81 | + Including `known_hosts` is optional but strongly recommended. Without it |
| 82 | + the `git-clone` Task will blindly accept the remote server's identity. |
| 83 | + |
| 84 | +2. Another approach is to bind an `ssl-ca-directory` workspace to this |
| 85 | +Task. The workspace should contain crt keys (e.g. `ca-bundle.crt`)files - anything you need to interact with your git remote |
| 86 | +via custom CA . It's **strongly** recommended that you use Kubernetes `Secrets` to |
| 87 | +hold your credentials and bind to this workspace. |
| 88 | + |
| 89 | + In a TaskRun that would look something like this: |
| 90 | + |
| 91 | + ```yaml |
| 92 | + kind: TaskRun |
| 93 | + spec: |
| 94 | + workspaces: |
| 95 | + - name: ssl-ca-directory |
| 96 | + secret: |
| 97 | + secretName: my-ssl-credentials |
| 98 | + ``` |
| 99 | + |
| 100 | + And in a Pipeline and PipelineRun it would look like this: |
| 101 | + |
| 102 | + ```yaml |
| 103 | + kind: Pipeline |
| 104 | + spec: |
| 105 | + workspaces: |
| 106 | + - name: ssl-creds |
| 107 | + # ... |
| 108 | + tasks: |
| 109 | + - name: fetch-source |
| 110 | + taskRef: |
| 111 | + name: git-clone |
| 112 | + workspaces: |
| 113 | + - name: ssl-ca-directory |
| 114 | + workspace: ssl-creds |
| 115 | + # ... |
| 116 | + --- |
| 117 | + kind: PipelineRun |
| 118 | + spec: |
| 119 | + workspaces: |
| 120 | + - name: ssl-creds |
| 121 | + secret: |
| 122 | + secretName: my-ssl-credentials |
| 123 | + # ... |
| 124 | + ``` |
| 125 | + |
| 126 | + The `Secret` would appear like below: |
| 127 | + |
| 128 | + ```yaml |
| 129 | + kind: Secret |
| 130 | + apiVersion: v1 |
| 131 | + metadata: |
| 132 | + name: my-ssl-credentials |
| 133 | + data: |
| 134 | + ca-bundle.crt: # ... base64-encoded crt ... # If key/filename is other than ca-bundle.crt then set crtFileName param as explained under Parameters section |
| 135 | + ``` |
| 136 | + |
| 137 | +### Optional 2: Using basic-auth Credentials |
| 138 | + |
| 139 | +**Note**: It is strongly advised that you use `ssh` credentials when the option |
| 140 | +is available to you before using basic auth. You can generate a short |
| 141 | +lived token from WebVCS platforms (Github, Gitlab, Bitbucket etc..) to be used |
| 142 | +as a password and generally be able to use `git` as the username. |
| 143 | +On bitbucket server the token may have a / into it so you would need |
| 144 | +to urlquote them before in the `Secret`, see this stackoverflow answer : |
| 145 | + |
| 146 | +https://stackoverflow.com/a/24719496 |
| 147 | + |
| 148 | +To support basic-auth this Task exposes an optional `basic-auth` Workspace. |
| 149 | +The bound Workspace should contain a `.gitconfig` or `.git-credentials` file. |
| 150 | +Any other files on this Workspace are ignored. A typical `Secret` containing |
| 151 | +these credentials looks as follows: |
| 152 | + |
| 153 | +```yaml |
| 154 | +kind: Secret |
| 155 | +apiVersion: v1 |
| 156 | +metadata: |
| 157 | + name: my-basic-auth-secret |
| 158 | +type: Opaque |
| 159 | +stringData: |
| 160 | + .gitconfig: | |
| 161 | + [credential "https://<hostname>"] |
| 162 | + helper = store |
| 163 | + .git-credentials: | |
| 164 | + https://<user>:<pass>@<hostname> |
| 165 | +``` |
| 166 | + |
| 167 | +### Optional 3: Using Git Connector |
| 168 | + |
| 169 | +The task can be used with Git Connector to enhance security. |
| 170 | + |
| 171 | +You need to create the Git Connector first, then in the TaskRun, use [CSI](https://tekton.dev/docs/pipelines/workspaces/#csi) to configure the `basic-auth` workspace. |
| 172 | + |
| 173 | +Git Connector currently only supports cloning with basic-auth, not with ssh. |
| 174 | + |
| 175 | +Here is an example of how to use Git Connector in git-clone TaskRun: |
| 176 | + |
| 177 | +**Create Git Connector** |
| 178 | + |
| 179 | +``` bash |
| 180 | +cat <<EOF | kubectl apply -f - |
| 181 | +kind: Secret |
| 182 | +apiVersion: v1 |
| 183 | +metadata: |
| 184 | + name: github |
| 185 | +type: kubernetes.io/basic-auth |
| 186 | +stringData: |
| 187 | + username: your-username # Replace with your Git username |
| 188 | + password: your-token # Replace with your Git password or token |
| 189 | +--- |
| 190 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 191 | +kind: Connector |
| 192 | +metadata: |
| 193 | + name: github |
| 194 | +spec: |
| 195 | + connectorClassName: git |
| 196 | + address: https://github.com # Replace with your Git server address |
| 197 | + auth: |
| 198 | + name: basicAuth |
| 199 | + secretRef: |
| 200 | + name: github |
| 201 | + params: |
| 202 | + - name: repository |
| 203 | + value: your-org/your-repo.git # Replace with your repository path which could access by your token, this is used for health check |
| 204 | +EOF |
| 205 | +``` |
| 206 | + |
| 207 | +**Create TaskRun** |
| 208 | + |
| 209 | +``` bash |
| 210 | +cat << EOF | kubectl apply -f - |
| 211 | +apiVersion: tekton.dev/v1 |
| 212 | +kind: TaskRun |
| 213 | +metadata: |
| 214 | + name: git-clone-demo |
| 215 | +spec: |
| 216 | + params: |
| 217 | + - name: url |
| 218 | + value: https://github.com/your-org/your-repo.git # Replace with your repository path which you want to clone |
| 219 | + - name: revision |
| 220 | + value: refs/heads/main |
| 221 | + taskRef: |
| 222 | + name: git-clone |
| 223 | + timeout: 10m0s |
| 224 | + computeResources: |
| 225 | + limits: |
| 226 | + cpu: 200m |
| 227 | + memory: 200Mi |
| 228 | + requests: |
| 229 | + cpu: 200m |
| 230 | + memory: 200Mi |
| 231 | + workspaces: |
| 232 | + - name: output |
| 233 | + emptyDir: {} |
| 234 | + - csi: |
| 235 | + driver: connectors-csi |
| 236 | + readOnly: true |
| 237 | + volumeAttributes: |
| 238 | + connector.name: github # the name of the connector |
| 239 | + connector.namespace: "" # the namespace of the connector, if not specified, the same namespace as the TaskRun will be used |
| 240 | + configuration.names: "gitconfig" # the name of the configuration, which is fixed as "gitconfig" |
| 241 | + name: basic-auth |
| 242 | +EOF |
| 243 | +``` |
| 244 | + |
| 245 | +More about Connector, please refer to <ExternalSiteLink name="connectors" href="/" children="Alauda DevOps Connectors Doc" />. |
| 246 | + |
| 247 | +- <ExternalSiteLink name="connectors" href="/connectors/how_to/create_scoped_connectors.html" children="How to Create Connector Resources at Different Levels" /> |
| 248 | +- <ExternalSiteLink name="connectors" href="/connectors/concepts/connector_scope_permissions.html" children="Connector Resource Levels and Permissions" /> |
| 249 | +- <ExternalSiteLink name="connectors" href="/connectors-git/functions/using-in-tekton-task" children="Using Git Connector in Tekton Task" /> |
0 commit comments