Skip to content

Commit 25b92da

Browse files
committed
feat: move cache stepactions to the repo
Update the cache-fetch and cache-upload task definitions in the doc.yaml and go.yaml files to use the pipelines-as-code repository. Update the cache-upload to try three times to upload the cache. Since the first time always result in error Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
1 parent 6ca75e4 commit 25b92da

File tree

5 files changed

+226
-4
lines changed

5 files changed

+226
-4
lines changed

.tekton/doc.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ spec:
4646
resolver: http
4747
params:
4848
- name: url
49-
value: https://raw.githubusercontent.com/chmouel/tekton-caches/refs/heads/chmouel-images/tekton/cache-fetch.yaml
49+
value: https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/refs/heads/main/.tekton/tasks/cache-fetch.yaml
5050
params:
5151
- name: patterns
5252
value: ["$(workspaces.source.path)/README.md"]
@@ -113,7 +113,7 @@ spec:
113113
resolver: http
114114
params:
115115
- name: url
116-
value: https://raw.githubusercontent.com/chmouel/tekton-caches/refs/heads/chmouel-images/tekton/cache-upload.yaml
116+
value: https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/refs/heads/main/.tekton/tasks/cache-upload.yaml
117117
params:
118118
- name: patterns
119119
value: ["$(workspaces.source.path)/README.md"]

.tekton/go.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ spec:
4444
resolver: http
4545
params:
4646
- name: url
47-
value: https://raw.githubusercontent.com/chmouel/tekton-caches/refs/heads/chmouel-images/tekton/cache-fetch.yaml
47+
value: https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/refs/heads/main/.tekton/tasks/cache-fetch.yaml
4848
params:
4949
- name: patterns
5050
value: ["**go.mod", "**go.sum"]
@@ -116,7 +116,7 @@ spec:
116116
resolver: http
117117
params:
118118
- name: url
119-
value: https://raw.githubusercontent.com/chmouel/tekton-caches/refs/heads/chmouel-images/tekton/cache-upload.yaml
119+
value: https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/refs/heads/main/.tekton/tasks/cache-upload.yaml
120120
params:
121121
- name: patterns
122122
value: ["**go.mod", "**go.sum"]

.tekton/tasks/cache-fetch.yaml

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: StepAction
3+
metadata:
4+
name: cache-fetch
5+
annotations:
6+
tekton.dev/pipelines.minVersion: "0.56.0"
7+
tekton.dev/tags: "cache"
8+
spec:
9+
params:
10+
- name: patterns
11+
description: |
12+
Regular expression to select files to include to compute the hash.
13+
For example, in the case of a Go project, you can use `go.mod` for this, so the value would be "**/go.sum" (to work with possible sub go modules as well).
14+
type: array
15+
- name: source
16+
description: |
17+
The source from where the cache should be fetched. It's a URI with the scheme defining the "provider". In addition, one can add a {{hash}} variable to use the computed hash in the reference (oci image tags, path in s3, …)
18+
Currently supported:
19+
- oci:// (e.g. oci://quay.io/vdemeester/go-cache:{{hash}}
20+
- s3:// (e.g. s3://
21+
type: string
22+
- name: cachePath
23+
description: |
24+
Path where to extract the cache content.
25+
It can refer any folder, backed by a workspace or a volume, or nothing.
26+
type: string
27+
- name: workingdir
28+
description: |
29+
The working dir from where the files patterns needs to be taken
30+
type: string
31+
- name: insecure
32+
description: |
33+
Whether to use insecure mode for fetching the cache
34+
type: string
35+
default: "false"
36+
- name: googleCredentialsPath
37+
description: |
38+
The path where to find the google credentials. If left empty, it is ignored.
39+
type: string
40+
default: ""
41+
- name: awsConfigFile
42+
description: |
43+
The path to the aws config file. If left empty, it is ignored.
44+
type: string
45+
default: ""
46+
- name: awsCredentialFile
47+
description: |
48+
The path to find the aws credentials file. If left empty, it is ignored.
49+
type: string
50+
default: ""
51+
- name: blobQueryParams
52+
description: |
53+
Blob Query Params to support configure s3, gcs and azure. This is optional unless some additional features of storage providers are required like s3 acceleration, fips, pathstyle,etc
54+
type: string
55+
default: ""
56+
results:
57+
- name: fetched
58+
description: |
59+
Whether a cache was fetched or not (true/false). This step won't fail if it didn't manage to fetch cache. This results allows the next step to act whether something was fetched or not.
60+
env:
61+
- name: PARAM_SOURCE
62+
value: $(params.source)
63+
- name: PARAM_CACHE_PATH
64+
value: $(params.cachePath)
65+
- name: PARAM_WORKINGDIR
66+
value: $(params.workingdir)
67+
- name: PARAM_INSECURE
68+
value: $(params.insecure)
69+
- name: GOOGLE_APPLICATION_CREDENTIALS
70+
value: $(params.googleCredentialsPath)
71+
- name: AWS_CONFIG_FILE
72+
value: $(params.awsConfigFile)
73+
- name: AWS_SHARED_CREDENTIALS_FILE
74+
value: $(params.awsCredentialFile)
75+
- name: BLOB_QUERY_PARAMS
76+
value: $(params.blobQueryParams)
77+
image: ghcr.io/chmouel/tekton-caches/cache@sha256:7b543966be2c4ef9bc439c0a6d262dd0f284216c7c292ba35c15b9ef5bca84c6
78+
args: ["$(params.patterns[*])"]
79+
script: |
80+
#!/bin/sh
81+
82+
PATTERN_FLAGS=""
83+
echo "Patterns: $*"
84+
for p in $*; do
85+
PATTERN_FLAGS="${PATTERN_FLAGS} --pattern ${p}"
86+
done
87+
88+
set -x
89+
/ko-app/cache fetch ${PATTERN_FLAGS} \
90+
--source ${PARAM_SOURCE} \
91+
--folder ${PARAM_CACHE_PATH} \
92+
--insecure ${PARAM_INSECURE} \
93+
--workingdir ${PARAM_WORKINGDIR}
94+
if [ $? -eq 0 ]; then
95+
echo -n true > $(step.results.fetched.path)
96+
else
97+
echo -n false > $(step.results.fetched.path)
98+
fi
99+
100+
exit 0

.tekton/tasks/cache-upload.yaml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: StepAction
3+
metadata:
4+
name: cache-upload
5+
annotations:
6+
tekton.dev/pipelines.minVersion: "0.56.0"
7+
tekton.dev/tags: "cache"
8+
spec:
9+
params:
10+
- name: patterns
11+
description: |
12+
Regular expression to select files to include to compute the hash.
13+
For example, in the case of a Go project, you can use `go.mod` for this, so the value would be "**/go.sum" (to work with possible sub go modules as well).
14+
type: array
15+
- name: target
16+
description: |
17+
The target from where the cache should be uploaded. It's a URI with the scheme defining the "provider". In addition, one can add a {{hash}} variable to use the computed hash in the reference (oci image tags, path in s3, …)
18+
Currently supported:
19+
- oci:// (e.g. oci://quay.io/vdemeester/go-cache:{{hash}}
20+
- s3:// (e.g. s3://
21+
type: string
22+
- name: cachePath
23+
description: |
24+
Path where to extract the cache content.
25+
It can refer any folder, backed by a workspace or a volume, or nothing.
26+
type: string
27+
- name: workingdir
28+
description: |
29+
The working dir from where the files patterns needs to be taken
30+
type: string
31+
- name: insecure
32+
description: |
33+
Whether to use insecure mode for fetching the cache
34+
type: string
35+
default: "false"
36+
- name: fetched
37+
description: |
38+
Wether cache was fetched or not previously
39+
type: string
40+
default: "false"
41+
- name: force-cache-upload
42+
description: |
43+
Whether to force the cache upload even if it was fetched previously
44+
type: string
45+
default: "false"
46+
- name: googleCredentialsPath
47+
description: |
48+
The path where to find the google credentials. If left empty, it is ignored.
49+
type: string
50+
default: ""
51+
- name: awsConfigFile
52+
description: |
53+
The path to the aws config file. If left empty, it is ignored.
54+
type: string
55+
default: ""
56+
- name: awsCredentialFile
57+
description: |
58+
The path to find the aws credentials file. If left empty, it is ignored.
59+
type: string
60+
default: ""
61+
- name: blobQueryParams
62+
description: |
63+
Blob Query Params to support configure s3, gcs and azure. This is optional unless some additional features of storage providers are required like s3 acceleration, fips, pathstyle,etc
64+
type: string
65+
default: ""
66+
env:
67+
- name: PARAM_TARGET
68+
value: $(params.target)
69+
- name: PARAM_CACHE_PATH
70+
value: $(params.cachePath)
71+
- name: PARAM_WORKINGDIR
72+
value: $(params.workingdir)
73+
- name: PARAM_INSECURE
74+
value: $(params.insecure)
75+
- name: RESULT_CACHE_FETCHED
76+
value: $(params.fetched)
77+
- name: PARAM_FORCE_CACHE_UPLOAD
78+
value: $(params.force-cache-upload)
79+
- name: GOOGLE_APPLICATION_CREDENTIALS
80+
value: $(params.googleCredentialsPath)
81+
- name: AWS_CONFIG_FILE
82+
value: $(params.awsConfigFile)
83+
- name: AWS_SHARED_CREDENTIALS_FILE
84+
value: $(params.awsCredentialFile)
85+
- name: BLOB_QUERY_PARAMS
86+
value: $(params.blobQueryParams)
87+
image: ghcr.io/chmouel/tekton-caches/cache@sha256:7b543966be2c4ef9bc439c0a6d262dd0f284216c7c292ba35c15b9ef5bca84c6
88+
args: ["$(params.patterns[*])"]
89+
script: |
90+
#!/usr/bin/env sh
91+
set -x
92+
if [[ ${PARAM_FORCE_CACHE_UPLOAD} == "false" && ${RESULT_CACHE_FETCHED} == "true" ]]; then
93+
echo "no need to upload cache"
94+
exit 0
95+
fi
96+
97+
PATTERN_FLAGS=""
98+
echo "Patterns: $*"
99+
for p in $*; do
100+
PATTERN_FLAGS="${PATTERN_FLAGS} --pattern ${p}"
101+
done
102+
103+
# Try three times
104+
for i in {1..3};do
105+
/ko-app/cache upload ${PATTERN_FLAGS} \
106+
--target ${PARAM_TARGET} \
107+
--folder ${PARAM_CACHE_PATH} \
108+
--insecure ${PARAM_INSECURE} \
109+
--workingdir ${PARAM_WORKINGDIR}
110+
ret=$?
111+
if [[ ${ret} == 0 ]];then
112+
exit 0
113+
fi
114+
done
115+
exit 1

pkg/reconciler/event.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import (
1818
"go.uber.org/zap"
1919
)
2020

21+
// detectProvider detects the git provider for the given PipelineRun and
22+
// initializes the corresponding provider interface. It returns the provider
23+
// interface, event information, and an error if any occurs during detection or
24+
// initialization.
25+
//
26+
// Supported providers: github, gitlab, bitbucket-cloud, bitbucket-datacenter, gitea
27+
// any new provider should be added to the switch case below.
2128
func (r *Reconciler) detectProvider(ctx context.Context, logger *zap.SugaredLogger, pr *tektonv1.PipelineRun) (provider.Interface, *info.Event, error) {
2229
gitProvider, ok := pr.GetAnnotations()[keys.GitProvider]
2330
if !ok {

0 commit comments

Comments
 (0)