Skip to content

Commit

Permalink
Ensure job name is a valid k8s resource name (#23)
Browse files Browse the repository at this point in the history
* Ensure job name is a valid k8s resource name

Fixes #17

* allow numeric at start and end
  • Loading branch information
iffyio authored Apr 7, 2021
1 parent bd36566 commit c4a2495
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ set -euo pipefail

basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"

job_name="${BUILDKITE_PIPELINE_SLUG}-${BUILDKITE_BUILD_NUMBER}-$(head -c 5 /dev/urandom | base32 | tr '[:upper:]' '[:lower:]')"
# Ensure a name is a valid k8s resource name.
function sanitize() {
echo "$@" | sed -E 's/[^A-Za-z0-9]/-/g' | sed -E 's/^[^a-zA-Z0-9]|[^a-zA-Z0-9]$//' | cut -c 1-63
}

job_name=$(sanitize "${BUILDKITE_PIPELINE_SLUG}-${BUILDKITE_BUILD_NUMBER}-$(head -c 5 /dev/urandom | base32 | tr '[:upper:]' '[:lower:]')")
echo "${job_name}" > /tmp/job_name

if [[ ${BUILDKITE_TIMEOUT:-"false"} == "false" ]]; then
Expand Down

0 comments on commit c4a2495

Please sign in to comment.