Skip to content

Commit 1019aaa

Browse files
authored
Add v1beta1 support to tekton-example (#97)
1 parent 3cc9dd1 commit 1019aaa

File tree

13 files changed

+415
-16
lines changed

13 files changed

+415
-16
lines changed

tekton-example/README.md

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Tekton Pipeline/Task Example
22

3-
An example of using `promote` in a Tekton Pipeline to promote a service's config to a GitOps repository. Creation of the PipelineRun (using `service-promote-pipeline-run.yaml`) will drive the pipeline to clone, build and push the service and then promote the config from the local clone into your staging/test GitOps repository.
3+
An example of using `promote` in a Tekton Pipeline to promote a service's config to a GitOps repository. Creation of the PipelineRun will drive the pipeline to clone, build and push the service and then promote the config from the local clone into your staging/test GitOps repository.
44

5-
Creation of a TaskRun (using `promote-run.yaml`) will then further promote from one GitOps repo to another, e.g. from staging to production.
5+
## Tekton APIs: v1alpha1 and v1beta1
6+
7+
Tekton Pipelines introduced new [v1beta1](https://github.com/tektoncd/pipeline/blob/master/docs/migrating-v1alpha1-to-v1beta1.md) APIs with its 0.11.x release. We first developed this sample against v1alpha1 APIs, but now include both v1alpha1 and v1beta1 versions of this sample. The v1beta1 version uses no Tekton PipelineResources, in the spirit of [this](https://github.com/tektoncd/pipeline/blob/master/docs/migrating-v1alpha1-to-v1beta1.md#replacing-pipelineresources-with-tasks) section of the migration document. You should use Tekton Pipelines v0.12.1 or higher and the v1beta1 samples unless you are unable to do so, or wish to use the code in the final section, 'Promote to Next Managed Environment'. This has only been implemented under the v1alpha1 sample. We're still working through how promotion between GitOps repositories should work, so this last section is fairly expermimental.
68

79
## Template Files
810

911
- `auth.yaml`: Creates secrets for a GitHub repository and image registry, an access token for the GitHub repository and the ServiceAccount
10-
- `resources.yaml`: Creates PipelineResources for GitHub and Docker repositories
12+
- `resources.yaml`: Creates PipelineResources for GitHub and Docker repositories. This file only exists in the v1alpha1 sample.
1113

1214
## Other Files
15+
1316
- `service-promote.yaml`: This is the Tekton Task used for promoting from one repository to another. It creates a PullRequest and this represents the promotion from one environment to another (for example, from development to production - in this case represented as repositories)
14-
- `service-promote-pipeline.yaml`: Creates a Pipeline that executes `build-task.yaml` and `service-promote.yaml`
15-
- `promote.yaml`: Creates a pull request from one repository to another repository
17+
- `service-promote-pipeline.yaml`: Creates a Pipeline that executes `build-task.yaml` and `service-promote.yaml`.
18+
- `promote.yaml`: Creates a pull request from one repository to another repository.
1619
- `build-task.yaml`: This task builds a Git source into a container image image and pushes to an image registry
20+
- `git-clone.yaml`: A copy of the Tekton Catalog [git-clone](https://github.com/tektoncd/catalog/blob/v1beta1/git/git-clone.yaml) Task, replacing the Git PipelineResource used in v1alpha1.
1721

1822
## Pre-requisites
1923

@@ -26,52 +30,90 @@ Creation of a TaskRun (using `promote-run.yaml`) will then further promote from
2630
## Create Tekton Resources
2731

2832
- Create a new namespace:
33+
2934
```shell
3035
kubectl create ns <namespace>
3136
```
3237

3338
- Modify your Kubernetes context to use this namespace by default:
34-
```shell
39+
40+
```shell
3541
kubectl config set-context --current --namespace=<namespace>
3642
```
3743

44+
- Choose whether to use the `v1alpha1` or `v1beta1` resources. As per the introduction, we recommend Tekton 0.12.x+ and the `v1beta1` path unless you have clear reasons to choose `v1alpha1`.
45+
46+
```sh
47+
cd v1beta1
48+
# OR
49+
cd v1alpha1
50+
```
51+
3852
- Apply the resources folder:
39-
```shell
53+
54+
```shell
4055
kubectl apply -f resources
4156
```
4257

43-
- Edit both files in the template folder to contain real values. Entries of the form `REPLACE_ME.x` must be replaced with the value you wish to use, i.e at occurences such as `REPLACE_ME.IMAGE_NAME`, `REPLACE_ME.GITHUB_ORG/REPLACE_ME.GITHUB_REPO` etc... There are eight instances to replace in this folder.
58+
- Edit the files in the template folder to contain real values. Entries of the form `REPLACE_ME.x` must be replaced with the value you wish to use, i.e at occurences such as `REPLACE_ME.IMAGE_NAME`, `REPLACE_ME.GITHUB_ORG/REPLACE_ME.GITHUB_REPO` etc... There are eight instances to replace in the v1alpha1 templates/ folder and nine in the v1beta1 templates/ folder.
59+
60+
- If you are using v1alpha1 you can apply the templates folder:
4461

45-
- Apply the templates folder:
46-
```shell
62+
```shell
4763
kubectl apply -f templates
4864
```
4965

66+
If you are using v1beta1 then only apply templates/auth.yaml. The templates/ folder also contains a PipelineRun which we don't want to run yet.
67+
68+
```sh
69+
kubectl apply -f templates/auth.yaml
70+
```
71+
5072
## Execute Pipeline
5173

5274
The PipelineRun you will create is designed to build your microservice from its development repository and then promote the new configuration to a GitOps repository (representing a different environment, for example development, staging, test or production).
5375

54-
- To create the PipelineRun, use:
76+
This example promotes from the `promote-demo` repository, containing a service with the same name.
77+
78+
- This creates a PipelineRun that executes the `service-promote-pipeline`, which will build the code and promote it to a repository you have specified
79+
- The logs will be outputted to your console, and you can also view its progress in the Tekton Dashboard.
80+
81+
The exact steps to create a PipelineRun depend on whether you are using the v1alpha1 or v1beta1 APIs.
82+
83+
### v1alpha1
84+
85+
To create the PipelineRun using v1alpha1, use:
86+
5587
```shell
5688
tkn pipeline start service-promote-pipeline --resource git-source=git-app-repo --resource docker-image=docker-app-image --param commitId=v1 --param github-secret=promote-secret --param commit-name=<yourgitname> --param commit-email=<yourgitemail> --param to=https://github.com/<github username>/<github repo>.git --param service=promote-demo --workspace name=repo-space,claimName=repopvc,subPath=dir -s demo --showlog
5789
```
5890

59-
This example promotes from the `promote-demo` repository, containing a service with the same name.
91+
### v1beta1
6092

93+
One of the main differences between our v1alpha1 and v1beta1 samples is that v1beta1 does not use a Tekton Git PipelineResource. Instead we use Tekton workspaces, which must be backed by persistent storage since they contain more than one task. Persistent storage requires a PersistentVolumeClaim. These can be dynamically generated using the `volumeClaimTemplate` stanza, but this is not supported in `tkn` until https://github.com/tektoncd/cli/issues/1006 is resolved. In the meantime we provide a PipelineRun:
6194

62-
- This creates a PipelineRun that executes the `service-promote-pipeline`, which will build the code and promote it to a repository you have specified
63-
- The logs will be outputted to your console, and you can also view its progress in the Tekton Dashboard.
95+
```sh
96+
kubectl create -f templates/promote-pipelinerun.yaml
97+
```
98+
99+
You will need to locate and tail the logs of this PipelineRun for yourself. For example you can use,
100+
101+
```sh
102+
kubectl get pipelineruns
103+
tkn pipelinerun logs [pipelinerun] -f
104+
```
64105

65106
## Promote to Next Managed Environment
66107

67108
Optionally, you can run a subsequent promote from one GitOps repository to another (e.g staging to prod) after merging the pull request on your first GitOps repository. For this you will need a third repository, and for this you can fork: https://github.com/rhd-gitops-example/gitops-example-staging
68109

69-
- To do this second promote, you will need to create a TaskRun that executes a task promoting from a testing repository to a production repository
110+
- To do this second promote, you will need to create a TaskRun that executes a task promoting from a testing repository to a production repository
70111
- To create the TaskRun (again this uses a service called `promote-demo`), use:
112+
71113
```shell
72114
tkn task start promote --param github-secret=promote-secret --param from=https://github.com/<yourorg>/<yourdevrepo>.git --param to=https://github.com/<yourorg>/<yourstagingrepo>.git --param commit-name=<yourgitname> --param commit-email=<yourgitemail> --param service=promote-demo -s demo --showlog
73115
```
116+
74117
This will start the TaskRun and output its logs, and you can also view its progress in the Tekton Dashboard.
75118

76119
The TaskRun will clone the code from the initial repository locally, build it and promote it to the final repository. This will open a pull request which you will be able to view in the repository you chose to promote to.
77-
File renamed without changes.
File renamed without changes.

tekton-example/resources/service-promote-pipeline.yaml renamed to tekton-example/v1alpha1/resources/service-promote-pipeline.yaml

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
apiVersion: tekton.dev/v1alpha1
2+
kind: Task
3+
metadata:
4+
name: build-push
5+
spec:
6+
workspaces:
7+
- name: repo
8+
mountPath: /repo
9+
inputs:
10+
params:
11+
- name: buildahImage
12+
description: The location of the buildah builder image.
13+
default: quay.io/buildah/stable:v1.11.0
14+
- name: pathToDockerfile
15+
description: The path to the Dockerfile to build
16+
default: /repo/Dockerfile
17+
- name: commitId
18+
description: commit ID of the source
19+
default: v2
20+
- name: tlsVerify
21+
description: whether we should verify the TLS on the registry endpoint (for push/pull to a non-TLS registry)
22+
default: "false"
23+
- name: pathToContext
24+
description: The build context to use
25+
default: /repo
26+
- name: destImage
27+
description: Built image location
28+
steps:
29+
- name: build
30+
image: $(inputs.params.buildahImage)
31+
securityContext:
32+
privileged: true
33+
script: |
34+
#!/bin/sh
35+
buildah bud --tls-verify=$(inputs.params.tlsVerify) --layers -f $(inputs.params.pathToDockerfile) -t $(inputs.params.destImage):$(inputs.params.commitId) $(inputs.params.pathToContext)
36+
volumeMounts:
37+
- name: varlibcontainers
38+
mountPath: /var/lib/containers
39+
- name: push
40+
image: $(inputs.params.buildahImage)
41+
securityContext:
42+
privileged: true
43+
script: |
44+
#!/bin/sh
45+
buildah push --tls-verify=$(inputs.params.tlsVerify) $(inputs.params.destImage):$(inputs.params.commitId)
46+
volumeMounts:
47+
- name: varlibcontainers
48+
mountPath: /var/lib/containers
49+
- name: update
50+
image: alpine
51+
script: |
52+
#!/bin/sh
53+
sed -i -e "s#\$IMAGE#\$(inputs.params.destImage):\$(inputs.params.commitId)#" $(inputs.params.pathToContext)/config/deploy.yaml
54+
volumes:
55+
- name: varlibcontainers
56+
emptyDir: {}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: git-clone
5+
spec:
6+
workspaces:
7+
- name: output
8+
description: The git repo will be cloned onto the volume backing this workspace
9+
params:
10+
- name: url
11+
description: git url to clone
12+
type: string
13+
- name: revision
14+
description: git revision to checkout (branch, tag, sha, ref…)
15+
type: string
16+
default: master
17+
- name: refspec
18+
description: (optional) git refspec to fetch before checking out revision
19+
default: ""
20+
- name: submodules
21+
description: defines if the resource should initialize and fetch the submodules
22+
type: string
23+
default: "true"
24+
- name: depth
25+
description: performs a shallow clone where only the most recent commit(s) will be fetched
26+
type: string
27+
default: "1"
28+
- name: sslVerify
29+
description: defines if http.sslVerify should be set to true or false in the global git config
30+
type: string
31+
default: "true"
32+
- name: subdirectory
33+
description: subdirectory inside the "output" workspace to clone the git repo into
34+
type: string
35+
default: ""
36+
- name: deleteExisting
37+
description: clean out the contents of the repo's destination directory (if it already exists) before trying to clone the repo there
38+
type: string
39+
default: "false"
40+
- name: httpProxy
41+
description: git HTTP proxy server for non-SSL requests
42+
type: string
43+
default: ""
44+
- name: httpsProxy
45+
description: git HTTPS proxy server for SSL requests
46+
type: string
47+
default: ""
48+
- name: noProxy
49+
description: git no proxy - opt out of proxying HTTP/HTTPS requests
50+
type: string
51+
default: ""
52+
results:
53+
- name: commit
54+
description: The precise commit SHA that was fetched by this Task
55+
steps:
56+
- name: clone
57+
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.12.1
58+
script: |
59+
CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)"
60+
61+
cleandir() {
62+
# Delete any existing contents of the repo directory if it exists.
63+
#
64+
# We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/"
65+
# or the root of a mounted volume.
66+
if [[ -d "$CHECKOUT_DIR" ]] ; then
67+
# Delete non-hidden files and directories
68+
rm -rf "$CHECKOUT_DIR"/*
69+
# Delete files and directories starting with . but excluding ..
70+
rm -rf "$CHECKOUT_DIR"/.[!.]*
71+
# Delete files and directories starting with .. plus any other character
72+
rm -rf "$CHECKOUT_DIR"/..?*
73+
fi
74+
}
75+
76+
if [[ "$(params.deleteExisting)" == "true" ]] ; then
77+
cleandir
78+
fi
79+
80+
test -z "$(params.httpProxy)" || export HTTP_PROXY=$(params.httpProxy)
81+
test -z "$(params.httpsProxy)" || export HTTPS_PROXY=$(params.httpsProxy)
82+
test -z "$(params.noProxy)" || export NO_PROXY=$(params.noProxy)
83+
84+
/ko-app/git-init \
85+
-url "$(params.url)" \
86+
-revision "$(params.revision)" \
87+
-refspec "$(params.refspec)" \
88+
-path "$CHECKOUT_DIR" \
89+
-sslVerify="$(params.sslVerify)" \
90+
-submodules="$(params.submodules)" \
91+
-depth "$(params.depth)"
92+
cd "$CHECKOUT_DIR"
93+
RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')"
94+
EXIT_CODE="$?"
95+
if [ "$EXIT_CODE" != 0 ]
96+
then
97+
exit $EXIT_CODE
98+
fi
99+
# Make sure we don't add a trailing newline to the result!
100+
echo -n "$RESULT_SHA" > $(results.commit.path)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
apiVersion: tekton.dev/v1alpha1
2+
kind: Pipeline
3+
metadata:
4+
name: service-promote-pipeline
5+
spec:
6+
workspaces:
7+
- name: repo-space
8+
params:
9+
- name: source-repo
10+
type: string
11+
description: GitHub repository url to be promoted from
12+
- name: revision
13+
type: string
14+
description: The git revision of source-repo to promote
15+
default: master
16+
- name: github-secret
17+
type: string
18+
description: name of the secret that contains the GitHub access token, the access token must be in a token key.
19+
- name: gitops-repo
20+
type: string
21+
description: GitOps repository to promote service into
22+
- name: service
23+
type: string
24+
description: service name to be promoted
25+
- name: commit-name
26+
type: string
27+
description: the GitHub name to use on the commit for promotion
28+
- name: commit-email
29+
type: string
30+
description: the GitHub email to use on the commit for promotion
31+
- name: dest-image
32+
type: string
33+
description: Image registry coordinates for the built image
34+
tasks:
35+
- name: fetch-source
36+
taskRef:
37+
name: git-clone
38+
workspaces:
39+
- name: output
40+
workspace: repo-space
41+
params:
42+
- name: url
43+
value: $(params.source-repo)
44+
- name: revision
45+
value: $(params.revision)
46+
- name: build-simple
47+
runAfter: [fetch-source]
48+
taskRef:
49+
name: build-push
50+
workspaces:
51+
- name: repo
52+
workspace: repo-space
53+
params:
54+
- name: commitId
55+
value: $(tasks.fetch-source.results.commit)
56+
- name: destImage
57+
value: $(params.dest-image)
58+
- name: promote
59+
runAfter: [build-simple]
60+
taskRef:
61+
name: service-promote
62+
workspaces:
63+
- name: repo
64+
workspace: repo-space
65+
params:
66+
- name: github-secret
67+
value: $(params.github-secret)
68+
- name: gitops-repo
69+
value: $(params.gitops-repo)
70+
- name: service
71+
value: $(params.service)
72+
- name: commit-name
73+
value: $(params.commit-name)
74+
- name: commit-email
75+
value: $(params.commit-email)
76+
77+
78+
79+

0 commit comments

Comments
 (0)