diff --git a/.tekton/tasks/check-git-files-changed.yaml b/.tekton/tasks/check-git-files-changed.yaml new file mode 100644 index 00000000..a7343ccf --- /dev/null +++ b/.tekton/tasks/check-git-files-changed.yaml @@ -0,0 +1,46 @@ +apiVersion: tekton.dev/v1alpha1 +kind: Task +metadata: + name: check-git-files-changed + annotations: + description: | + Returns "passed" in the "check" result if any of the files changed + in the source on the workspace matches the regular expression. + Returns a "files" result as a list of files modified by the PR that + match the regex in the parameters. + ci.tekton.dev/condition: | + The result of this task is used to evaluate a condition. + Events from this task are ignored downstream, when it comes to update the CI job status. +spec: + params: + - name: gitCloneDepth + description: Number of commits + 1 + - name: regex + description: Regular expression to match files changed + workspaces: + - name: input + results: + - name: check + description: passed or failed + - name: files + description: list of files modified that match + steps: + - name: check-files-changed + image: alpine/git + env: + - name: GIT_CLONE_DEPTH + value: $(params.gitCloneDepth) + - name: REGEX + value: $(params.regex) + script: | + #!/bin/sh + set -ex + set -o pipefail + BACK="HEAD~$(( ${GIT_CLONE_DEPTH} - 1 ))" + CHECK="failed" + cd $(workspaces.input.path) + git diff-tree --no-commit-id --name-only -r HEAD $BACK | \ + grep -E "${REGEX}" > $(results.files.path) || true + git diff-tree --no-commit-id --name-only -r HEAD $BACK | \ + grep -E "${REGEX}" && CHECK="passed" + printf $CHECK > $(results.check.path) diff --git a/.tekton/tasks/kustomization.yaml b/.tekton/tasks/kustomization.yaml index b22a1a25..f6ed600a 100644 --- a/.tekton/tasks/kustomization.yaml +++ b/.tekton/tasks/kustomization.yaml @@ -4,3 +4,4 @@ kind: Kustomization resources: - external.yaml - lint.yaml + - check-git-files-changed.yaml