Skip to content

Commit

Permalink
Add a delete param to the git-clone task for reusing PVCs
Browse files Browse the repository at this point in the history
An implementation detail of the git-clone task in the catalog: it fails
if the directory it's cloning into is already a git repo. This behaviour
has been carried over from the GitResource. Prior to this commit the
advice for users was to manually delete the directory themselves before
running this Task.

This PR introduces a param that, when set to "true", will empty the
directory of any files and folders before performing the clone.
  • Loading branch information
Scott authored and bobcatfish committed Mar 3, 2020
1 parent 8a1a977 commit 7082865
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
11 changes: 1 addition & 10 deletions git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,12 @@ as well as
* **depth**: performs a shallow clone where only the most recent commit(s) will be fetched (_default_: 1)
* **sslVerify**: defines if http.sslVerify should be set to true or false in the global git config (_default_: true)
* **subdirectory**: subdirectory inside the "output" workspace to clone the git repo into (_default:_ src)
* **deleteExisting**: clean out the contents of the repo's destination directory if it already exists before cloning the repo there (_default_: false)

### Results

* **commit**: The precise commit SHA that was fetched by this Task

### Notes On Usage

Please ensure that the Workspace subdirectory you are cloning into is *empty*
before giving the Workspace to this Task. The `git-init` binary that this task
uses to perform a clone will error out if the directory you're cloning into is
already a git repo. This problem can appear when you reuse a directory on a
`PersistentVolumeClaim` as the "output" workspace multiple times. The current
suggested workaround is to add a Task before git-clone that "cleans" the
workspace first by running `rm -rf` on the directory if it exists.

## Usage

### `git-clone`
Expand Down
27 changes: 26 additions & 1 deletion git/git-clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ spec:
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: "false"
results:
- name: commit
description: The precise commit SHA that was fetched by this Task
Expand All @@ -39,14 +43,35 @@ spec:
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest
script: |
CHECKOUT_DIR="$(workspaces.output.path)/$(inputs.params.subdirectory)"
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 [[ "$(inputs.params.deleteExisting)" == "true" ]] ; then
cleandir
ls -lah "$CHECKOUT_DIR"
fi
/ko-app/git-init \
-url "$(inputs.params.url)" \
-revision "$(inputs.params.revision)" \
-path "$CHECKOUT_DIR" \
-sslVerify "$(inputs.params.sslVerify)" \
-submodules "$(inputs.params.submodules)" \
-depth "$(inputs.params.depth)"
cd $CHECKOUT_DIR
cd "$CHECKOUT_DIR"
RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')"
EXIT_CODE="$?"
if [ "$EXIT_CODE" != 0 ]
Expand Down

0 comments on commit 7082865

Please sign in to comment.