Skip to content

Commit 5c8e646

Browse files
add sample tekton resources and README (#35)
1 parent 5c9310e commit 5c8e646

File tree

11 files changed

+388
-0
lines changed

11 files changed

+388
-0
lines changed

docs/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Tekton pipeline / task example
2+
3+
## Files
4+
5+
auth.yaml: (template) create secrets for github and docker registry and create the service account
6+
gitconfig: (template) data file for the gitconfig configmap. The config map is created by `kubectl create configmap promoteconfigmap --from-file=gitconfig`
7+
8+
promotesecret.yaml: (template) create an access token secret for the github repository
9+
resources.yaml: (template) create pipeline resources for github and docker repository
10+
11+
build-task.yaml: create a build push task
12+
servicepromote.yaml: (template)create a promote from service repo to env repo task
13+
servicepromotepipeline.yaml: create a pipeline that executes build, push and promote
14+
servicepromotepipelinerun.yaml: create a pipelinerun that executes the servicepromotepipeline
15+
16+
promote.yaml: (template)create a promote from one env repo to another env repo task
17+
promoterun.yaml: create a taskrun that execute promote task
18+
19+
## Build docker image with `service promote` command
20+
21+
- clone this repository
22+
- run `docker build -t <image name> .` in repository root directory
23+
- run `docker tag <image name> <your docker hub id>/<image name>` toi tag the image
24+
- run `docker login` to login to the docker hub
25+
- run `docker push <your docker hub id>/<image name>` to push the image to the docker hub
26+
27+
## Create Tekton resource
28+
29+
- edit all yaml files marked as (template) and gitconfig file. `<xxx>` must be replaced with the real value
30+
- create a new namespace e.g. `kubectl create ns promote`
31+
- apply auth.yaml, promotesecret.yaml, resources.yaml, build-task.yaml, servicepromote.yaml and servicepromotepipelinerun.yaml in the namespace e.g. `kubectl -n <namespace> apply -f <yaml file name>
32+
- create a configmap by `kubectl create configmap promoteconfigmap --from-file=gitconfig -n <namespace>`
33+
34+
## Execute pipeline
35+
36+
- create the servicepromotepipelinerun by applying servicepromotepipelinerun.yaml e.g. `apply -n <namespace> apply -f servicepromotepipelinerun.yaml`

docs/auth.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: secret-github
5+
annotations:
6+
tekton.dev/git-0: https://github.com
7+
type: kubernetes.io/basic-auth
8+
stringData:
9+
username: <github user name>
10+
password: <github personal access token>
11+
12+
---
13+
14+
apiVersion: v1
15+
kind: Secret
16+
metadata:
17+
name: secret-dockerhub
18+
annotations:
19+
tekton.dev/docker-0: https://index.docker.io/v1/
20+
type: kubernetes.io/basic-auth
21+
stringData:
22+
username: <docker hub user id>
23+
password: <docker hub password>
24+
25+
---
26+
27+
apiVersion: v1
28+
kind: ServiceAccount
29+
metadata:
30+
name: demo
31+
secrets:
32+
- name: secret-github
33+
- name: secret-dockerhub

docs/build-task.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
apiVersion: tekton.dev/v1alpha1
2+
kind: Task
3+
metadata:
4+
name: build-push
5+
spec:
6+
workspaces:
7+
- name: repo
8+
inputs:
9+
resources:
10+
- name: git-source
11+
type: git
12+
params:
13+
- name: pathToDockerFile
14+
description: The path to the dockerfile to build
15+
default: ./repo/Dockerfile
16+
- name: commitId
17+
description: commit ID of the source
18+
default: v2
19+
- name: pathToContext
20+
description: The build context used by Kaniko (https://github.com/GoogleContainerTools/kaniko#kaniko-build-contexts)
21+
default: ./repo
22+
outputs:
23+
resources:
24+
- name: builtImage
25+
type: image
26+
steps:
27+
- name: build
28+
image: docker
29+
script: |
30+
#!/bin/sh
31+
cp -R git-source/* repo
32+
docker build -f $(inputs.params.pathToDockerFile) -t $(outputs.resources.builtImage.url) $(inputs.params.pathToContext)
33+
volumeMounts:
34+
- name: docker-socket
35+
mountPath: /var/run/docker.sock
36+
- name: push
37+
image: docker
38+
script: |
39+
#!/bin/sh
40+
docker tag $(outputs.resources.builtImage.url) $(outputs.resources.builtImage.url):$(inputs.params.commitId)
41+
docker push $(outputs.resources.builtImage.url):$(inputs.params.commitId)
42+
volumeMounts:
43+
- name: docker-socket
44+
mountPath: /var/run/docker.sock
45+
- name: update
46+
image: alpine
47+
script: |
48+
#!/bin/sh
49+
sed -i -e "s#\$IMAGE#\$(outputs.resources.builtImage.url):\$(inputs.params.commitId)#" $(inputs.params.pathToContext)/config/deploy.yaml
50+
volumes:
51+
- name: docker-socket
52+
hostPath:
53+
path: /var/run/docker.sock
54+
type: Socket
55+

docs/gitconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[user]
2+
name = <github user name>
3+
email = <github email>

docs/promote.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: promote
5+
spec:
6+
params:
7+
- name: github-secret
8+
type: string
9+
description: seret name of the github that has the access token. The key name is token.
10+
- name: from
11+
type: string
12+
description: github repository url to be promoted from
13+
- name: to
14+
type: string
15+
description: github repository url to be promoted to
16+
- name: service
17+
type: string
18+
description: service name to be promoted
19+
- name: github-config
20+
type: string
21+
description: configmap name of the gitconfig file that has user name, user e-mail. The key name is gitconfig. It can be created by "kubectl create configmap <configmap name> --from-file=gitconfig -n promote"
22+
volumes:
23+
- name: gitconfig
24+
configMap:
25+
name: $(params.github-config)
26+
items:
27+
- key: gitconfig
28+
path: gitconfig
29+
steps:
30+
- name: promote
31+
#image: akihikokuroda/promote
32+
image: <image name>
33+
imagePullPolicy: Always
34+
volumeMounts:
35+
- name: gitconfig
36+
mountPath: /root
37+
script: |
38+
#!/bin/sh
39+
cp /root/gitconfig $HOME/.gitconfig
40+
services promote --from $(params.from) --to $(params.to) --service $(params.service)
41+
env:
42+
- name: GITHUB_TOKEN
43+
valueFrom:
44+
secretKeyRef:
45+
name: $(params.github-secret)
46+
key: token
47+

docs/promoterun.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: TaskRun
3+
metadata:
4+
name: promoterun
5+
spec:
6+
params:
7+
- name: github-secret
8+
value: promotesecret
9+
- name: github-config
10+
value: promoteconfigmap
11+
- name: from
12+
value: "https://github.com/akihikokuroda/gitops-test.git"
13+
- name: to
14+
value: "https://github.com/akihikokuroda/gitops-prod.git"
15+
- name: service
16+
value: service-a
17+
#serviceAccountName: tekton-triggers-controller
18+
taskRef:
19+
name: promote

docs/promotesecret.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Secret
2+
apiVersion: v1
3+
metadata:
4+
name: promotesecret
5+
stringData:
6+
token: <github personal access token>

docs/resources.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: tekton.dev/v1alpha1
2+
kind: PipelineResource
3+
metadata:
4+
name: git-app-repo
5+
spec:
6+
params:
7+
- name: revision
8+
value: master
9+
- name: url
10+
#value: https://github.com/akihikokuroda/promote-demo.git
11+
value: https://github.com/<github org>/<github repo>.git
12+
type: git
13+
14+
---
15+
16+
apiVersion: tekton.dev/v1alpha1
17+
kind: PipelineResource
18+
metadata:
19+
name: docker-app-image
20+
spec:
21+
params:
22+
- name: url
23+
#value: akihikokuroda/app-image
24+
value: <docker hub image name>
25+
type: image
26+
27+
---
28+
29+
apiVersion: v1
30+
kind: PersistentVolumeClaim
31+
metadata:
32+
name: repopvc
33+
spec:
34+
resources:
35+
requests:
36+
storage: 16Mi
37+
volumeMode: Filesystem
38+
accessModes:
39+
- ReadWriteOnce

docs/servicepromote.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: service-promote
5+
spec:
6+
workspaces:
7+
- name: repo
8+
params:
9+
- name: github-secret
10+
type: string
11+
description: name of the secret that contains the GitHub access token, the access token must be in a token key.
12+
- name: from
13+
type: string
14+
description: github repository url to be promoted from
15+
default: /workspace/repo
16+
- name: to
17+
type: string
18+
description: github repository url to be promoted to
19+
- name: service
20+
type: string
21+
description: service name to be promoted
22+
- name: github-config
23+
type: string
24+
description: configmap name of the gitconfig file that has user name and e-mail. The key name is gitconfig. It can be created by "kubectl create configmap <configmap name> --from-file=$HOME/.gitconfig -n <namespace>"
25+
volumes:
26+
- name: gitconfig
27+
configMap:
28+
name: $(params.github-config)
29+
items:
30+
- key: gitconfig
31+
path: gitconfig
32+
steps:
33+
- name: promote
34+
image: <image name>
35+
imagePullPolicy: Always
36+
volumeMounts:
37+
- name: gitconfig
38+
mountPath: /root
39+
script: |
40+
#!/bin/sh
41+
cp /root/gitconfig $HOME/.gitconfig
42+
services promote --from $(params.from) --to $(params.to) --service $(params.service)
43+
env:
44+
- name: GITHUB_TOKEN
45+
valueFrom:
46+
secretKeyRef:
47+
name: $(params.github-secret)
48+
key: token
49+

docs/servicepromotepipeline.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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: commitId
10+
type: string
11+
description: commit ID of the source repository.
12+
- name: github-secret
13+
type: string
14+
description: name of the secret that contains the GitHub access token, the access token must be in a token key.
15+
- name: from
16+
type: string
17+
description: github repository url to be promoted from
18+
default: /workspace/repo
19+
- name: to
20+
type: string
21+
description: github repository url to be promoted to
22+
- name: service
23+
type: string
24+
description: service name to be promoted
25+
- name: github-config
26+
type: string
27+
description: configmap name of the gitconfig file that has user name, user e-mail. The key name is gitco
28+
resources:
29+
- name: git-source
30+
type: git
31+
- name: docker-image
32+
type: image
33+
tasks:
34+
- name: build-simple
35+
taskRef:
36+
name: build-push
37+
workspaces:
38+
- name: repo
39+
workspace: repo-space
40+
params:
41+
- name: commitId
42+
value: $(params.commitId)
43+
resources:
44+
inputs:
45+
- name: git-source
46+
resource: git-source
47+
outputs:
48+
- name: builtImage
49+
resource: docker-image
50+
- name: promote
51+
runAfter: [build-simple]
52+
taskRef:
53+
name: service-promote
54+
workspaces:
55+
- name: repo
56+
workspace: repo-space
57+
params:
58+
- name: github-secret
59+
value: $(params.github-secret)
60+
- name: from
61+
value: $(params.from)
62+
- name: to
63+
value: $(params.to)
64+
- name: service
65+
value: $(params.service)
66+
- name: github-config
67+
value: $(params.github-config)
68+
69+
70+
71+

0 commit comments

Comments
 (0)