apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: git-clone namespace: apps-build spec: workspaces: - name: source description: The git repo will be cloned into the volume backing this workspace readOnly: false params: - name: repositoryurl description: git url to clone type: string - name: tag description: git tag if present type: string default: "" - name: revision description: git revision to checkout (branch, tag, sha, ref…) type: string default: main - name: branch description: git revision to checkout (branch, tag, sha, ref…) type: string default: main - name: submodules description: defines if the resource should initialize and fetch the submodules type: string default: "true" - name: depth description: performs a shallow clone where only the most recent commit(s) will be fetched type: string default: "1" - name: sslVerify description: defines if http.sslVerify should be set to true or false in the global git config type: string default: "false" - name: subdirectory description: subdirectory inside the "output" workspace to clone the git repo into type: string default: "src" - name: deleteExisting description: clean out the contents of the repo's destination directory (if it already exists) before trying to clone the repo there type: string default: "true" - name: gitInitImage description: The image used where the git-init binary is. default: "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.15.2" type: string results: - name: commit description: The precise commit SHA that was fetched by this Task - name: commitshashort description: The short version of the commit SHA that was fetched by this Task - name: built-image-tag description: The built image tag steps: - name: clone image: $(params.gitInitImage) script: | CHECKOUT_DIR="$(workspaces.source.path)/" cleandir() { # Delete any existing contents of the repo directory if it exists. # # We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/" # or the root of a mounted volume. if [[ -d "$CHECKOUT_DIR" ]] ; then # Delete non-hidden files and directories rm -rf "$CHECKOUT_DIR"/* # Delete files and directories starting with . but excluding .. rm -rf "$CHECKOUT_DIR"/.[!.]* # Delete files and directories starting with .. plus any other character rm -rf "$CHECKOUT_DIR"/..?* fi } if [[ "$(params.deleteExisting)" == "true" ]] ; then cleandir fi REVISION=$(echo $(params.branch)) REVISION_CLEAN="${REVISION#refs/heads/}" REVISION_CLEAN="${REVISION_CLEAN#refs/tags/}" TAG=$(echo $(params.tag)) git clone "$(params.repositoryurl)" --recurse-submodules --depth 1 -b "$REVISION_CLEAN" "$CHECKOUT_DIR" cd "$CHECKOUT_DIR" RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" EXIT_CODE="$?" if [ "$EXIT_CODE" != 0 ] then exit $EXIT_CODE fi # Make sure we don't add a trailing newline to the result! echo -n "$RESULT_SHA" > $(results.commit.path) COMMIT_SHA_SHORT="$(git rev-parse --short HEAD | tr -d '\n')" echo -n "$COMMIT_SHA_SHORT" > $(results.commitshashort.path) BRANCH_NAME="$(echo $REVISION_CLEAN | sed 's/\//-/g')" if [ -z "${TAG##*refs/tags/*}" ] then IMAGE_TAG=$(echo "$TAG" | cut -d '/' -f 3) else IMAGE_TAG="${COMMIT_SHA_SHORT}" fi echo -n "$IMAGE_TAG" > $(results.built-image-tag.path)