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

initial implementation for git resource for pipeline #91

Merged
merged 2 commits into from
Oct 4, 2018
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
83 changes: 83 additions & 0 deletions docs/pipeline-resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
### Pipeline Resources

## Git Resource
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so cool, thanks for adding docs!

it would be good at add a link from https://github.com/knative/build-pipeline#resources to here as well, since folks won't know this doc exists


Git resource represents a [git](https://git-scm.com/) repository, that containes the source code to be built by the pipeline. Adding the git resource as an input to a task will clone this repository and allow the task to perform the required actions on the contents of the repo.

Use the following example to understand the syntax and strucutre of a Git Resource

#### Create a git resource using the `PipelineResource` CRD

```
apiVersion: pipeline.knative.dev/v1alpha1
kind: Resource
metadata:
name: wizzbang-git
namespace: default
spec:
type: git
params:
- name: url
value: github.com/wizzbangcorp/wizzbang
- name: Revision
value: master
- name: ServiceAccount
value: pipeline-sa
```

Params that can be added are the following:

1. URL: represents the location of the git repository
1. Revision: Git [revision](https://git-scm.com/docs/gitrevisions#_specifying_revisions ) (branch, tag, commit SHA or ref) to clone. If no revision is specified, the resource will default to `latest` from `master`
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
1. Service Account: specifies the `name` of a `ServiceAccount` resource object. Add this paramater to run your task with the privileges of the specified service account. If no serviceAccountName field is specified, your task runs using the default service account that is in the namespace of the Pipeline resource object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm i wonder if this is a good default - in PipelineParams we currently have a serviceAccount field as well, is there some way we can default to that one?

and/or maybe have an error if there is no serviceaccount?


#### Use the defined git resource in a `Task` definition

```
apiVersion: pipeline.knative.dev/v1alpha1
kind: Task
metadata:
name: build-push-task
namespace: default
spec:
inputs:
resources:
- name: wizzbang-git
type: git
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
params:
- name: PATH_TO_DOCKERFILE
value: string
outputs:
resources:
- name: builtImage
buildSpec:
template:
name: kaniko
arguments:
- name: DOCKERFILE
value: ${PATH_TO_DOCKERFILE}
- name: REGISTRY
value: ${REGISTRY}
```

#### And finally set the version in the `TaskRun` definition

```
apiVersion: pipeline.knative.dev/v1alpha1
kind: TaskRun
metadata:
name: build-push-task-run
namespace: default
spec:
taskRef:
name: build-push-task
inputs:
resourcesVersion:
- resourceRef:
name: wizzbang-git
version: HEAD
outputs:
artifacts:
- name: builtImage
type: image
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great docs!!

36 changes: 36 additions & 0 deletions pkg/apis/pipeline/v1alpha1/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ limitations under the License.

package v1alpha1

import (
"fmt"
"strings"
)

// GitResource is an endpoint from which to get data which is required
// by a Build/Task for context (e.g. a repo from which to build an image).
type GitResource struct {
Expand All @@ -30,6 +35,32 @@ type GitResource struct {
ServiceAccount string `json:"serviceAccount,omitempty"`
}

// NewGitResource create a new git resource to pass to Knative Build
func NewGitResource(r *PipelineResource) (*GitResource, error) {
if r.Spec.Type != PipelineResourceTypeGit {
return nil, fmt.Errorf("GitResource: Cannot create a Git resource from a %s Pipeline Resource", r.Spec.Type)
}
gitResource := GitResource{
Name: r.Name,
Type: r.Spec.Type,
nader-ziada marked this conversation as resolved.
Show resolved Hide resolved
}
for _, param := range r.Spec.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
gitResource.URL = param.Value
case strings.EqualFold(param.Name, "serviceAccount"):
gitResource.ServiceAccount = param.Value
case strings.EqualFold(param.Name, "Revision"):
gitResource.Revision = param.Value
}
}
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
// default revision to master is nothing is provided
if gitResource.Revision == "" {
gitResource.Revision = "master"
}
return &gitResource, nil
}

// GetName returns the name of the resource
func (s GitResource) GetName() string {
return s.Name
Expand All @@ -52,5 +83,10 @@ func (s *GitResource) GetServiceAccountName() string {
return s.ServiceAccount
}

// GetURL returns the url to be used with this resource
func (s *GitResource) GetURL() string {
return s.URL
}

// GetParams returns the resoruce params
func (s GitResource) GetParams() []Param { return []Param{} }
Loading