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: revision description: git revision to checkout (branch, tag, sha, ref…) type: string default: master - name: branch description: git revision to checkout (branch, tag, sha, ref…) type: string default: master - 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" 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: {{ .Values.accountId }}.dkr.ecr.{{ .Values.region }}.amazonaws.com/cloner:latest env: - name: GIT_USERNAME valueFrom: secretKeyRef: name: codecommit-credentials key: username - name: GIT_PASSWORD valueFrom: secretKeyRef: name: codecommit-credentials key: password 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/}" git clone "https://${GIT_USERNAME}:${GIT_PASSWORD}@$(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')" # Compute image tag out of the branch # GIT_TAG=$(git tag --contains | head -1) if [ -z "${REVISION##*refs/tags/*}" ] then IMAGE_TAG=$(echo "$REVISION" | cut -d '/' -f 3) else IMAGE_TAG="${COMMIT_SHA_SHORT}" fi echo -n "$IMAGE_TAG" > $(results.built-image-tag.path)