-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A new task to generate labels #1443
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,10 @@ spec: | |
description: Whether to skip stages in Containerfile that seem unused by subsequent stages | ||
type: string | ||
default: "true" | ||
- name: LABELS | ||
description: Additional key=value labels that should be applied to the image | ||
type: array | ||
default: [] | ||
|
||
results: | ||
- description: Digest of the image just built | ||
|
@@ -179,7 +183,11 @@ spec: | |
- name: COMMIT_SHA | ||
value: $(params.COMMIT_SHA) | ||
args: | ||
- --build-args | ||
- $(params.BUILD_ARGS[*]) | ||
- --labels | ||
- $(params.LABELS[*]) | ||
|
||
script: | | ||
#!/bin/bash | ||
set -e | ||
|
@@ -239,11 +247,28 @@ spec: | |
sed -e '/^#/d' -e '/^\s*$/d' "${SOURCE_CODE_DIR}/${BUILD_ARGS_FILE}" | ||
) | ||
fi | ||
# Append BUILD_ARGS | ||
# Note: this may result in multiple --build-arg=KEY=value flags with the same KEY being | ||
# passed to buildah. In that case, the *last* occurrence takes precedence. This is why | ||
# we append BUILD_ARGS after the content of the BUILD_ARGS_FILE - they take precedence. | ||
build_args+=("$@") | ||
|
||
LABELS=() | ||
# Split `args` into two sets of arguments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks complicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started that way @mmorhun, but if we make the labels available to the script as an env var, then I don't think I can make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will a delimiter in the env var work? Like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Label values are too free-form for delimiters to work Ralph's original approach was to use a JSON string of an array, but that's much less user-friendly You can't really make something like this work with a JSON string params:
- name: LABELS
value:
- $(tasks.generate-labels.results.LABELS[*])
- foo=bar |
||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--build-args) | ||
shift | ||
# Note: this may result in multiple --build-arg=KEY=value flags with the same KEY being | ||
# passed to buildah. In that case, the *last* occurrence takes precedence. This is why | ||
# we append BUILD_ARGS after the content of the BUILD_ARGS_FILE - they take precedence. | ||
while [[ $# -gt 0 && $1 != --* ]]; do build_args+=("$1"); shift; done | ||
;; | ||
--labels) | ||
shift | ||
while [[ $# -gt 0 && $1 != --* ]]; do LABELS+=("--label" "$1"); shift; done | ||
;; | ||
*) | ||
echo "unexpected argument: $1" >&2 | ||
exit 2 | ||
;; | ||
esac | ||
done | ||
|
||
BUILD_ARG_FLAGS=() | ||
for build_arg in "${build_args[@]}"; do | ||
|
@@ -317,13 +342,16 @@ spec: | |
VOLUME_MOUNTS="${VOLUME_MOUNTS} --volume ${mount_point}:${YUM_REPOS_D_TARGET}" | ||
fi | ||
|
||
LABELS=( | ||
DEFAULT_LABELS=( | ||
"--label" "build-date=$(date -u +'%Y-%m-%dT%H:%M:%S')" | ||
"--label" "architecture=$(uname -m)" | ||
"--label" "vcs-type=git" | ||
) | ||
[ -n "$COMMIT_SHA" ] && LABELS+=("--label" "vcs-ref=$COMMIT_SHA") | ||
[ -n "$IMAGE_EXPIRES_AFTER" ] && LABELS+=("--label" "quay.expires-after=$IMAGE_EXPIRES_AFTER") | ||
[ -n "$COMMIT_SHA" ] && DEFAULT_LABELS+=("--label" "vcs-ref=$COMMIT_SHA") | ||
[ -n "$IMAGE_EXPIRES_AFTER" ] && DEFAULT_LABELS+=("--label" "quay.expires-after=$IMAGE_EXPIRES_AFTER") | ||
|
||
# Concatenate defaults and explicit labels. If a label appears twice, the last one wins. | ||
LABELS=("${DEFAULT_LABELS[@]}" "${LABELS[@]}") | ||
|
||
ACTIVATION_KEY_PATH="/activation-key" | ||
ENTITLEMENT_PATH="/entitlement" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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:%SZ' | ||
* 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:%SZ' | ||
* 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|An array of templates that should be rendered and exposed as an array of labels||true| | ||
|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| | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's just the TA task generation doing some formatting ¯\_(ツ)_/¯