Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the git fetch task to include all params used in the Git PipelineResource #182

Merged
merged 1 commit into from Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 70 additions & 28 deletions git/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Git tasks
# Git Tasks

These Tasks are Git tasks to clone a repository in the workspace to be
used by other tasks in a Pipeline.
Expand All @@ -7,59 +7,102 @@ This is a `Task` that does the job of the `GitResource`
`PipelineResource`. This is linked to
[tektoncd/pipeline#1369](https://github.com/tektoncd/pipeline/issues/1369).

## `fetch-git`
## `git-clone`

### Workspaces

* **output**: A workspace for this Task to fetch the git repository in to.

### Inputs

#### Parameters

* **url**: git url to clone
* **url**: git url to clone (_required_)
* **revision**: git revision to checkout (branch, tag, sha, ref…) (_default:_ master)
* **workingDirectory**: working directory to clone (_default:_ .)
* **submodules**: init and fetch recursively submodules (_default:_ true)
* **submodules**: defines if the resource should initialize and fetch the submodules (_default_: true)
* **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)

### Results

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

### Notes On Usage

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. In particular this can become a problem when you reuse a directory on a
Persistent Volume Claim as git-clone's "output" workspace multiple times. One simple fix for this is to add a
Task before git-clone that "cleans" the workspace first by running `rm -rf` if the directory already
exists in the workspace. This may become a configurable parameter of this task at some point in the future.

## Usage

### `fetch-git`
### `git-clone`

This pipeline uses the Task to clone the
This pipeline uses the git-clone Task to check out the
[`tektoncd/pipeline`](https://github.com/tektoncd/pipeline) repository
and display the README in the next step
and then display that repo's README.md.

A workspace called "shared-workspace" is passed first to the `git-clone`
Task for the code to be checked out on and then to the `cat-readme` Task
to print the README.md file from.

After the Pipeline has run you'll be able to see a
[Task Result](https://github.com/tektoncd/pipeline/blob/master/docs/taskruns.md#results)
named `commit` in the PipelineRun's Status with the commit SHA that was
fetched by the `git-clone` Task.

```yaml
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: cat-readme
spec:
workspaces:
- name: source
mountPath: /source
inputs:
params:
- name: subdirectory
description: Subdirectory inside "source" workspace that contains the README.md.
default: "."
steps:
- name: clone
- name: cat-readme
image: ubuntu
workingdir: /workspace/src/
command:
- /bin/bash
- -c
args:
- "cat README"
volumeMounts:
- name: source
mountPath: /workspace/src
script: cat "$(workspaces.source.path)/$(inputs.params.subdirectory)/README.md"
---
apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
name: cat-pipeline-readme
spec:
workspaces:
- name: shared-workspace
# description: The git repo will be cloned into this workspace and the readme will be read from it.
tasks:
- name: fetch-repository
taskRef:
name: fetch-git
name: git-clone
workspaces:
- name: output
workspace: shared-workspace
params:
- name: url
value: github.com/tektoncd/pipeline
- name: cat-readme
value: https://github.com/tektoncd/pipeline.git
- name: subdirectory
value: pipeline-checkout
- name: print-readme
taskRef:
name: echo-readme
name: cat-readme
runAfter:
- fetch-repository # required to ensure clone occurs before reading
workspaces:
- name: source
workspace: shared-workspace
params:
- name: subdirectory
value: pipeline-checkout
```

This pipeline can be used as the following `PipelineRun` does.
Expand All @@ -68,7 +111,7 @@ This pipeline can be used as the following `PipelineRun` does.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: source
name: workspace-pvc
spec:
accessModes:
- ReadWriteOnce
Expand All @@ -83,9 +126,8 @@ metadata:
spec:
pipelineRef:
name: cat-pipeline-readme
podTemplate:
volumes:
- name: source
persistentVolumeClaim:
claimName: source
workspaces:
- name: shared-workspace
persistentVolumeClaim:
claimName: workspace-pvc
```
41 changes: 0 additions & 41 deletions git/fetch-git.yaml

This file was deleted.

55 changes: 55 additions & 0 deletions git/git-clone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: git-clone
spec:
workspaces:
- name: output
description: The git repo will be cloned onto the volume backing this workspace
inputs:
params:
- name: url
description: git url to clone
type: string
- name: revision
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: "true"
- name: subdirectory
description: subdirectory inside the "output" workspace to clone the git repo into
type: string
default: "src"
results:
- name: commit
description: The precise commit SHA that was fetched by this Task
steps:
- name: clone
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest
script: |
CHECKOUT_DIR="$(workspaces.output.path)/$(inputs.params.subdirectory)"
/ko-app/git-init \
-url $(inputs.params.url) \
-revision $(inputs.params.revision) \
-path "$CHECKOUT_DIR" \
-submodules $(inputs.params.submodules)
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)