-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathtekton-catalog-catlin-lint.yaml
196 lines (189 loc) · 6.57 KB
/
tekton-catalog-catlin-lint.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: catlin-lint
spec:
description: >-
This task detects changes in the pull request using git diff
and stores the changed tasks names in a workspace and passes
that to the catlin so that catlin can validate only changed
files.
workspaces:
- name: source
description: >
Workspace where the git repo is prepared for linting and catlin
output is stored.
params:
- name: gitCloneDepth
description: Number of commits in the change + 1
- name: versioning
description: The versioning strategy of the catalog, either "git" or "directory"
default: git
steps:
- name: find-changed-tasks
image: docker.io/alpine/git:v2.26.2@sha256:23618034b0be9205d9cc0846eb711b12ba4c9b468efdd8a59aac1d7b1a23363f
workingDir: $(workspaces.source.path)
env:
- name: GIT_CLONE_DEPTH
value: $(params.gitCloneDepth)
script: |
function detect_new_changed_tasks() {
# detect for changes in the task manifest
git --no-pager diff --name-only HEAD~$(( ${GIT_CLONE_DEPTH} - 1 ))|grep 'task/[^\/]*/[^\/]*/*[^/]*.yaml'|xargs -I {} dirname {}
}
all_tests=$(detect_new_changed_tasks |sort -u || true)
final_tests=""
# check for the tasks which are removed completely and skip them
for task in $all_tests; do
[[ ! -d $task ]] && continue
final_tests="$final_tests $task"
done
echo -n $final_tests > changed-files.txt
- name: lint-catalog
image: ghcr.io/tektoncd/plumbing/catlin:latest
workingDir: $(workspaces.source.path)
script: |
set +e
[[ ! -s changed-files.txt ]] && {
echo "No file change detected in task directory"
echo -n "" > catlin.txt
exit 0
}
# creating a file which will contain the final formatted output
# which needs to be added as a comment(if any)
echo '<details><summary>Catlin Output</summary>' >> catlin.txt
echo '' >> catlin.txt
echo '```' >> catlin.txt
# performing catlin validate
catlin validate $(cat changed-files.txt) --versioning $(params.versioning) | tee -a catlin.txt
echo '```' >> catlin.txt
echo "</details>" >> catlin.txt
# performing catlin script validattion only on yaml files
for file in $(cat changed-files.txt);do
cat ${file}/*.yaml | grep 'tekton.dev/deprecated: \"true\"' && continue
catlin lint script ${file}/*.yaml | tee -a catlin-script.txt
done
if [[ -s catlin-script.txt ]];then
echo '<details><summary>Catlin script lint Output</summary>' >> catlin.txt
echo '' >> catlin.txt
echo '```' >> catlin.txt
cat catlin-script.txt >> catlin.txt
echo '```' >> catlin.txt
fi
# checking if there are any ERROR or WARN messages produced by catlin
isWarning=$(cat catlin.txt | grep -c "WARN")
isError=$(cat catlin.txt | grep -c "ERROR")
# if there are no ERROR or WARN messages then add a empty string which will not
# add a comment on the Github PR
[[ $isWarning -eq 0 ]] && [[ $isError -eq 0 ]] && \
echo -n "" > catlin.txt
# if catlin produced the error then fail the task else success
[[ $isError -eq 1 ]] && exit 1 || exit 0
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: catlin-linter
spec:
workspaces:
- name: source
description: Workspace where the git repo is prepared for linting.
params:
- name: gitCloneDepth
description: Number of commits in the change + 1
- name: gitHubCommand
description: The command that was used to trigger testing
- name: checkName
description: The name of the GitHub check that this pipeline is used for
- name: pullRequestUrl
description: The HTML URL for the pull request
- name: gitRepository
description: The git repository that hosts context and Dockerfile
- name: pullRequestBaseRef
description: The pull request base branch
- name: pullRequestNumber
description: The pullRequestNumber
- name: versioning
description: The versioning strategy of the catalog, either "git" or "directory"
default: git
tasks:
- name: check-name-match
taskRef:
name: check-name-matches
params:
- name: gitHubCommand
value: $(params.gitHubCommand)
- name: checkName
value: $(params.checkName)
- name: clone-repo
when:
- input: $(tasks.check-name-match.results.check)
operator: in
values: ["passed"]
taskRef:
resolver: bundles
params:
- name: bundle
value: ghcr.io/tektoncd/catalog/upstream/tasks/git-batch-merge:0.2
- name: name
value: git-batch-merge
- name: kind
value: task
workspaces:
- name: output
workspace: source
params:
- name: url
value: $(params.gitRepository)
- name: mode
value: "merge"
- name: revision
value: $(params.pullRequestBaseRef)
- name: refspec
value: refs/heads/$(params.pullRequestBaseRef):refs/heads/$(params.pullRequestBaseRef)
- name: batchedRefs
value: "refs/pull/$(params.pullRequestNumber)/head"
- name: lint-catalog
runAfter:
- "clone-repo"
when:
- input: $(tasks.check-name-match.results.check)
operator: in
values: ["passed"]
taskRef:
name: catlin-lint
workspaces:
- name: source
workspace: source
params:
- name: gitCloneDepth
value: $(params.gitCloneDepth)
- name: versioning
value: $(params.versioning)
finally:
- name: post-comment
when:
- input: $(tasks.check-name-match.results.check)
operator: in
values: ["passed"]
taskRef:
resolver: bundles
params:
- name: bundle
value: ghcr.io/tektoncd/catalog/upstream/tasks/github-add-comment:0.3
- name: name
value: github-add-comment
- name: kind
value: task
params:
- name: COMMENT_OR_FILE
value: "catlin.txt"
- name: GITHUB_TOKEN_SECRET_NAME
value: bot-token-github
- name: GITHUB_TOKEN_SECRET_KEY
value: bot-token
- name: REQUEST_URL
value: $(params.pullRequestUrl)
workspaces:
- name: comment-file
workspace: source