|
| 1 | +apiVersion: tekton.dev/v1alpha1 |
| 2 | +kind: StepAction |
| 3 | +metadata: |
| 4 | + name: git-clone |
| 5 | + labels: |
| 6 | + app.kubernetes.io/version: "0.1" |
| 7 | + annotations: |
| 8 | + tekton.dev/pipelines.minVersion: "0.54.0" |
| 9 | + tekton.dev/categories: Git |
| 10 | + tekton.dev/tags: git |
| 11 | + tekton.dev/displayName: "git clone" |
| 12 | + tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64" |
| 13 | +spec: |
| 14 | + params: |
| 15 | + - name: output-path |
| 16 | + description: The git repo will be cloned onto this path |
| 17 | + - name: ssh-directory-path |
| 18 | + description: | |
| 19 | + A .ssh directory with private key, known_hosts, config, etc. Copied to |
| 20 | + the user's home before git commands are executed. Used to authenticate |
| 21 | + with the git remote when performing the clone. We recommend providing this |
| 22 | + path from a workspace that is bound by a Secret over other volume types. |
| 23 | + default: "no-path" |
| 24 | + - name: basic-auth-path |
| 25 | + description: | |
| 26 | + A directory path containing a .gitconfig and .git-credentials file. These |
| 27 | + will be copied to the user's home before any git commands are run. Any |
| 28 | + other files in this directory are ignored. It is strongly recommended |
| 29 | + to use ssh-directory over basic-auth whenever possible and to bind a |
| 30 | + Secret to the Workspace providing this path over other volume types. |
| 31 | + default: "no-path" |
| 32 | + - name: ssl-ca-directory-path |
| 33 | + description: | |
| 34 | + A directory containing CA certificates, this will be used by Git to |
| 35 | + verify the peer with when fetching or pushing over HTTPS. |
| 36 | + default: "no-path" |
| 37 | + - name: url |
| 38 | + description: Repository URL to clone from. |
| 39 | + type: string |
| 40 | + - name: revision |
| 41 | + description: Revision to checkout. (branch, tag, sha, ref, etc...) |
| 42 | + type: string |
| 43 | + default: "" |
| 44 | + - name: refspec |
| 45 | + description: Refspec to fetch before checking out revision. |
| 46 | + default: "" |
| 47 | + - name: submodules |
| 48 | + description: Initialize and fetch git submodules. |
| 49 | + type: string |
| 50 | + default: "true" |
| 51 | + - name: depth |
| 52 | + description: Perform a shallow clone, fetching only the most recent N commits. |
| 53 | + type: string |
| 54 | + default: "1" |
| 55 | + - name: sslVerify |
| 56 | + description: Set the `http.sslVerify` global git config. Setting this to `false` is not advised unless you are sure that you trust your git remote. |
| 57 | + type: string |
| 58 | + default: "true" |
| 59 | + - name: crtFileName |
| 60 | + description: file name of mounted crt using ssl-ca-directory workspace. default value is ca-bundle.crt. |
| 61 | + type: string |
| 62 | + default: "ca-bundle.crt" |
| 63 | + - name: subdirectory |
| 64 | + description: Subdirectory inside the `output` Workspace to clone the repo into. |
| 65 | + type: string |
| 66 | + default: "" |
| 67 | + - name: sparseCheckoutDirectories |
| 68 | + description: Define the directory patterns to match or exclude when performing a sparse checkout. |
| 69 | + type: string |
| 70 | + default: "" |
| 71 | + - name: deleteExisting |
| 72 | + description: Clean out the contents of the destination directory if it already exists before cloning. |
| 73 | + type: string |
| 74 | + default: "true" |
| 75 | + - name: httpProxy |
| 76 | + description: HTTP proxy server for non-SSL requests. |
| 77 | + type: string |
| 78 | + default: "" |
| 79 | + - name: httpsProxy |
| 80 | + description: HTTPS proxy server for SSL requests. |
| 81 | + type: string |
| 82 | + default: "" |
| 83 | + - name: noProxy |
| 84 | + description: Opt out of proxying HTTP/HTTPS requests. |
| 85 | + type: string |
| 86 | + default: "" |
| 87 | + - name: verbose |
| 88 | + description: Log the commands that are executed during `git-clone`'s operation. |
| 89 | + type: string |
| 90 | + default: "true" |
| 91 | + - name: gitInitImage |
| 92 | + description: The image providing the git-init binary that this StepAction runs. |
| 93 | + type: string |
| 94 | + default: "ghcr.io/tektoncd/github.com/tektoncd/pipeline/cmd/git-init:v0.40.2" |
| 95 | + - name: userHome |
| 96 | + description: | |
| 97 | + Absolute path to the user's home directory. |
| 98 | + type: string |
| 99 | + default: "/home/git" |
| 100 | + results: |
| 101 | + - name: commit |
| 102 | + description: The precise commit SHA that was fetched by this StepAction. |
| 103 | + - name: url |
| 104 | + description: The precise URL that was fetched by this StepAction. |
| 105 | + - name: committer-date |
| 106 | + description: The epoch timestamp of the commit that was fetched by this StepAction. |
| 107 | + image: "$(params.gitInitImage)" |
| 108 | + env: |
| 109 | + - name: HOME |
| 110 | + value: "$(params.userHome)" |
| 111 | + - name: PARAM_URL |
| 112 | + value: $(params.url) |
| 113 | + - name: PARAM_REVISION |
| 114 | + value: $(params.revision) |
| 115 | + - name: PARAM_REFSPEC |
| 116 | + value: $(params.refspec) |
| 117 | + - name: PARAM_SUBMODULES |
| 118 | + value: $(params.submodules) |
| 119 | + - name: PARAM_DEPTH |
| 120 | + value: $(params.depth) |
| 121 | + - name: PARAM_SSL_VERIFY |
| 122 | + value: $(params.sslVerify) |
| 123 | + - name: PARAM_CRT_FILENAME |
| 124 | + value: $(params.crtFileName) |
| 125 | + - name: PARAM_SUBDIRECTORY |
| 126 | + value: $(params.subdirectory) |
| 127 | + - name: PARAM_DELETE_EXISTING |
| 128 | + value: $(params.deleteExisting) |
| 129 | + - name: PARAM_HTTP_PROXY |
| 130 | + value: $(params.httpProxy) |
| 131 | + - name: PARAM_HTTPS_PROXY |
| 132 | + value: $(params.httpsProxy) |
| 133 | + - name: PARAM_NO_PROXY |
| 134 | + value: $(params.noProxy) |
| 135 | + - name: PARAM_VERBOSE |
| 136 | + value: $(params.verbose) |
| 137 | + - name: PARAM_SPARSE_CHECKOUT_DIRECTORIES |
| 138 | + value: $(params.sparseCheckoutDirectories) |
| 139 | + - name: PARAM_USER_HOME |
| 140 | + value: $(params.userHome) |
| 141 | + - name: PARAM_OUTPUT_PATH |
| 142 | + value: $(params.output-path) |
| 143 | + - name: PARAM_SSH_DIRECTORY_PATH |
| 144 | + value: $(params.ssh-directory-path) |
| 145 | + - name: PARAM_BASIC_AUTH_DIRECTORY_PATH |
| 146 | + value: $(params.basic-auth-path) |
| 147 | + - name: PARAM_SSL_CA_DIRECTORY_PATH |
| 148 | + value: $(params.ssl-ca-directory-path) |
| 149 | + securityContext: |
| 150 | + runAsNonRoot: true |
| 151 | + runAsUser: 65532 |
| 152 | + script: | |
| 153 | + #!/usr/bin/env sh |
| 154 | + set -eu |
| 155 | +
|
| 156 | + if [ "${PARAM_VERBOSE}" = "true" ] ; then |
| 157 | + set -x |
| 158 | + fi |
| 159 | +
|
| 160 | + if [ "${PARAM_BASIC_AUTH_DIRECTORY_PATH}" != "no-path" ] ; then |
| 161 | + cp "${PARAM_BASIC_AUTH_DIRECTORY_PATH}/.git-credentials" "${PARAM_USER_HOME}/.git-credentials" |
| 162 | + cp "${PARAM_BASIC_AUTH_DIRECTORY_PATH}/.gitconfig" "${PARAM_USER_HOME}/.gitconfig" |
| 163 | + chmod 400 "${PARAM_USER_HOME}/.git-credentials" |
| 164 | + chmod 400 "${PARAM_USER_HOME}/.gitconfig" |
| 165 | + fi |
| 166 | +
|
| 167 | + if [ "${PARAM_SSH_DIRECTORY_PATH}" != "no-path" ] ; then |
| 168 | + cp -R "${PARAM_SSH_DIRECTORY_PATH}" "${PARAM_USER_HOME}"/.ssh |
| 169 | + chmod 700 "${PARAM_USER_HOME}"/.ssh |
| 170 | + chmod -R 400 "${PARAM_USER_HOME}"/.ssh/* |
| 171 | + fi |
| 172 | +
|
| 173 | + if [ "${PARAM_SSL_CA_DIRECTORY_PATH}" != "no-path" ] ; then |
| 174 | + export GIT_SSL_CAPATH="${PARAM_SSL_CA_DIRECTORY_PATH}" |
| 175 | + if [ "${PARAM_CRT_FILENAME}" != "" ] ; then |
| 176 | + export GIT_SSL_CAINFO="${PARAM_SSL_CA_DIRECTORY_PATH}/${PARAM_CRT_FILENAME}" |
| 177 | + fi |
| 178 | + fi |
| 179 | + CHECKOUT_DIR="${PARAM_OUTPUT_PATH}/${PARAM_SUBDIRECTORY}" |
| 180 | +
|
| 181 | + cleandir() { |
| 182 | + # Delete any existing contents of the repo directory if it exists. |
| 183 | + # |
| 184 | + # We don't just "rm -rf ${CHECKOUT_DIR}" because ${CHECKOUT_DIR} might be "/" |
| 185 | + # or the root of a mounted volume. |
| 186 | + if [ -d "${CHECKOUT_DIR}" ] ; then |
| 187 | + # Delete non-hidden files and directories |
| 188 | + rm -rf "${CHECKOUT_DIR:?}"/* |
| 189 | + # Delete files and directories starting with . but excluding .. |
| 190 | + rm -rf "${CHECKOUT_DIR}"/.[!.]* |
| 191 | + # Delete files and directories starting with .. plus any other character |
| 192 | + rm -rf "${CHECKOUT_DIR}"/..?* |
| 193 | + fi |
| 194 | + } |
| 195 | +
|
| 196 | + if [ "${PARAM_DELETE_EXISTING}" = "true" ] ; then |
| 197 | + cleandir || true |
| 198 | + fi |
| 199 | +
|
| 200 | + test -z "${PARAM_HTTP_PROXY}" || export HTTP_PROXY="${PARAM_HTTP_PROXY}" |
| 201 | + test -z "${PARAM_HTTPS_PROXY}" || export HTTPS_PROXY="${PARAM_HTTPS_PROXY}" |
| 202 | + test -z "${PARAM_NO_PROXY}" || export NO_PROXY="${PARAM_NO_PROXY}" |
| 203 | +
|
| 204 | + git config --global --add safe.directory "${PARAM_OUTPUT_PATH}" |
| 205 | + /ko-app/git-init \ |
| 206 | + -url="${PARAM_URL}" \ |
| 207 | + -revision="${PARAM_REVISION}" \ |
| 208 | + -refspec="${PARAM_REFSPEC}" \ |
| 209 | + -path="${CHECKOUT_DIR}" \ |
| 210 | + -sslVerify="${PARAM_SSL_VERIFY}" \ |
| 211 | + -submodules="${PARAM_SUBMODULES}" \ |
| 212 | + -depth="${PARAM_DEPTH}" \ |
| 213 | + -sparseCheckoutDirectories="${PARAM_SPARSE_CHECKOUT_DIRECTORIES}" |
| 214 | + cd "${CHECKOUT_DIR}" |
| 215 | + RESULT_SHA="$(git rev-parse HEAD)" |
| 216 | + EXIT_CODE="$?" |
| 217 | + if [ "${EXIT_CODE}" != 0 ] ; then |
| 218 | + exit "${EXIT_CODE}" |
| 219 | + fi |
| 220 | + RESULT_COMMITTER_DATE="$(git log -1 --pretty=%ct)" |
| 221 | + printf "%s" "${RESULT_COMMITTER_DATE}" > "$(step.results.committer-date.path)" |
| 222 | + printf "%s" "${RESULT_SHA}" > "$(step.results.commit.path)" |
| 223 | + printf "%s" "${PARAM_URL}" > "$(step.results.url.path)" |
0 commit comments