Skip to content

Commit

Permalink
Introduce new generate-labels task
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Sep 25, 2024
1 parent f61a156 commit 90a25b5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
32 changes: 32 additions & 0 deletions task/generate-labels/0.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# generate-labels task

Generate labels based on templates.

Usage may look like the following.

> - name: generate-labels
> params:
> - name: LABEL_TEMPLATES
> value:
> - "release=$SOURCE_DATE_EPOCH"
> - "build-date=$SOURCE_DATE"
The following environment variables are defined for use in LABEL_TEMPLATES

* ACTUAL_DATE - a date time string containing the time this task runs, formatted +'%Y-%m-%dT%H:%M:%S'
* ACTUAL_DATE_EPOCH - the timestamp at the time this task runs
* SOURCE_DATE - a date time string containing the provided source timestamp, formatted +'%Y-%m-%dT%H:%M:%S'
* SOURCE_DATE_EPOCH - the timestamp provided as a param meant to represent the timestamp at which the source was last modified


## Parameters
|name|description|default value|required|
|---|---|---|---|
|LABEL_TEMPLATES|A list of templates that should be rendered and exposed as a list of labels|[]|false|
|SOURCE_DATE_EPOCH|A standardised environment variable for build tools to consume in order to produce reproducible output.|""|false|

## Results
|name|description|
|---|---|
|LABELS|The rendered labels, rendered from the provided templates|

73 changes: 73 additions & 0 deletions task/generate-labels/0.1/generate-labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/pipelines.minVersion: "0.12.1"
tekton.dev/tags: "konflux"
name: generate-labels
spec:
description: |
Generate labels based on templates.
Usage may look like the following.
> - name: generate-labels
> params:
> - name: LABEL_TEMPLATES
> value: '["release=$SOURCE_DATE_EPOCH", "build-date=$SOURCE_DATE"]'
The following environment variables are defined for use in LABEL_TEMPLATES
* ACTUAL_DATE - a date time string containing the time this task runs, formatted +'%Y-%m-%dT%H:%M:%S'
* ACTUAL_DATE_EPOCH - the timestamp at the time this task runs
* SOURCE_DATE - a date time string containing the provided source timestamp, formatted +'%Y-%m-%dT%H:%M:%S'
* SOURCE_DATE_EPOCH - the timestamp provided as a param meant to represent the timestamp at which the source was last modified
params:
- name: label-templates
description: A json array of templates that should be rendered and exposed as a list of labels
type: string
- name: source-date-epoch
description: A standardised environment variable for build tools to consume in order to produce reproducible output.
default: ""
results:
- name: labels
description: The rendered labels, rendered from the provided templates
steps:
- name: render
image: quay.io/konflux-ci/yq:latest@sha256:91a7fe73fd28bbfd93ebdcc68cdfd7087d4f2612c627ccd0d5f984190eb7dba6
env:
- name: LABEL_TEMPLATES
value: "$(params.label-templates)"
- name: SOURCE_DATE_EPOCH
value: "$(params.source-date-epoch)"
script: |
#!/bin/bash
ACTUAL_DATE_EPOCH=$(date -u +'%s')
# shellcheck disable=SC2034 # Unused variable used implicitly in envsubst
ACTUAL_DATE=$(date -u --date=@"$ACTUAL_DATE_EPOCH" +'%Y-%m-%dT%H:%M:%S')

Check failure on line 51 in task/generate-labels/0.1/generate-labels.yaml

View workflow job for this annotation

GitHub Actions / yamllint

51:83 [trailing-spaces] trailing spaces
if [ "$SOURCE_DATE_EPOCH" == "" ]; then
SOURCE_DATE_EPOCH="$ACTUAL_DATE_EPOCH"
fi
# shellcheck disable=SC2034 # Unused variable used implicitly in envsubst
SOURCE_DATE=$(date -u --date=@"$SOURCE_DATE_EPOCH" +'%Y-%m-%dT%H:%M:%S')

Check failure on line 57 in task/generate-labels/0.1/generate-labels.yaml

View workflow job for this annotation

GitHub Actions / yamllint

57:83 [trailing-spaces] trailing spaces
export ACTUAL_DATE
export ACTUAL_DATE_EPOCH
export SOURCE_DATE
export SOURCE_DATE_EPOCH
printf "[]" > result.json
for template in $(echo "${LABEL_TEMPLATES}" | yq -r '.[]'); do
echo "Processing template $template"
label=$(echo "$template" | envsubst)
yq -oj -i '. += ["'"$label"'"]' result.json
done
echo "Created the following labels:"
tee "$(results.labels.path)" < result.json
7 changes: 7 additions & 0 deletions task/generate-labels/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See the OWNERS docs: https://go.k8s.io/owners
approvers:
- build-team
- ralphbean
reviewers:
- build-team
- ralphbean

0 comments on commit 90a25b5

Please sign in to comment.