|
| 1 | +#!/bin/sh |
| 2 | +# shellcheck shell=sh |
| 3 | + |
| 4 | +# Write the build labels to the build labels path for both the org.label-schema and org.opencontainers.image formats |
| 5 | + |
| 6 | +[ -n "${XDEBUG:-}" ] && set -x |
| 7 | + |
| 8 | +write_to_build_labels() { |
| 9 | + while [ $# -gt 1 ]; do |
| 10 | + eval "[ -n \"\${$#}\" ] && printf '%s %s\n' \"$1\" \"\${$#}\"" >>"${BUILD_LABELS_PATH:-/dev/stdout}" |
| 11 | + shift |
| 12 | + done |
| 13 | + return 0 |
| 14 | +} |
| 15 | + |
| 16 | +write_apptainer_labels() { |
| 17 | + #[ -n "${APPTAINER_ROOTFS:-}" ] || return 1 # Exit if not in an apptainer build |
| 18 | + #BUILD_LABELS_PATH="${BUILD_LABELS_PATH:-${APPTAINER_ROOTFS:+${APPTAINER_ROOTFS}/.build.labels}}" # Set the default build labels path |
| 19 | + if [ -n "${APPTAINER_ROOTFS:-}" ]; then |
| 20 | + BUILD_LABELS_PATH="${BUILD_LABELS_PATH:-${APPTAINER_ROOTFS}/.build.labels}" |
| 21 | + else |
| 22 | + BUILD_LABELS_PATH="${BUILD_LABELS_PATH:-/dev/stdout}" |
| 23 | + fi |
| 24 | + |
| 25 | + # Try to fill in the build labels via git if not already set and git is available |
| 26 | + if git tag >/dev/null 2>&1; then |
| 27 | + IMAGE_VCS_URL="${IMAGE_VCS_URL:-$(git remote get-url origin || true)}" # Set the default VCS URL to the origin remote |
| 28 | + [ -z "${IMAGE_URL:-}" ] && [ -n "${IMAGE_VCS_URL:-}" ] && IMAGE_URL="${IMAGE_VCS_URL%%.git}" # Set the default URL to the VCS URL without the .git extension |
| 29 | + IMAGE_VCS_REF="${IMAGE_VCS_REF:-$(git rev-parse --short HEAD || true)}" # Set the default VCS ref to the short hash of HEAD |
| 30 | + |
| 31 | + IMAGE_GIT_TAG="${GITHUB_REF_NAME:-"$(git tag --points-at HEAD --list '*@*' --sort=-"creatordate:iso" || true)"}" # Set the default git tag to the most recent tag matching the format *@* sorted by date |
| 32 | + |
| 33 | + if [ -n "${IMAGE_GIT_TAG:-}" ]; then |
| 34 | + if [ -z "${IMAGE_TAG:-}" ]; then |
| 35 | + IMAGE_TAG="$(echo "${IMAGE_GIT_TAG:-}" | sed -nE 's/.*[@]//; s/^v//; 1p')" |
| 36 | + [ -z "${IMAGE_TAG:-}" ] && IMAGE_TAG='latest' |
| 37 | + fi |
| 38 | + |
| 39 | + if [ -n "${IMAGE_TITLE:-}" ]; then |
| 40 | + IMAGE_TITLE="$(echo "${IMAGE_GIT_TAG}" | sed -nE 's/[@].*$//; 1p')" |
| 41 | + fi |
| 42 | + fi |
| 43 | + fi |
| 44 | + IMAGE_TAG="${IMAGE_TAG:-latest}" # Set the default tag to latest if no tag was found |
| 45 | + IMAGE_TITLE="${IMAGE_TITLE:-"$(basename "${PWD}")"}" # Set the default title to the current directory name |
| 46 | + IMAGE_VERSION="${IMAGE_VERSION:-${IMAGE_TAG:-}}" # Set the default version to the tag if set, otherwise the tag if set, otherwise empty |
| 47 | + |
| 48 | + # If no image vendor is set, try to set it to the GitHub organization: |
| 49 | + if [ -z "${IMAGE_VENDOR:=${IMAGE_VENDOR:-}}" ]; then |
| 50 | + # If the GitHub organization is not set, try to set it to the GitHub organization of the upstream remote: |
| 51 | + [ -z "${GH_ORG:-}" ] && GH_ORG="$(git remote get-url upstream | sed -n 's/.*github.com[:/]\([^/]*\)\/.*/\1/p' || true)" |
| 52 | + # If the GitHub organization is not set, try to set it to the GitHub organization of the origin remote: |
| 53 | + [ -z "${GH_ORG:-}" ] && GH_ORG="$(git remote get-url origin | sed -n 's/.*github.com[:/]\([^/]*\)\/.*/\1/p' || true)" |
| 54 | + |
| 55 | + # Assign the image vendor to the GitHub organization or username if it is set, otherwise leave it empty: |
| 56 | + IMAGE_VENDOR="${GH_ORG:-}" |
| 57 | + |
| 58 | + # If the GitHub organization is set to uw-psych, set the image vendor to the University of Washington Department of Psychology: |
| 59 | + [ "${IMAGE_VENDOR:-}" = 'uw-psych' ] && IMAGE_VENDOR='University of Washington Department of Psychology' |
| 60 | + fi |
| 61 | + |
| 62 | + # Try to set image author from GITHUB_REPOSITORY_OWNER if not set: |
| 63 | + IMAGE_AUTHOR="${IMAGE_AUTHOR:-${GITHUB_REPOSITORY_OWNER:-}}" |
| 64 | + |
| 65 | + # If no image author is set, try to set it to the git author via git config: |
| 66 | + if [ -z "${IMAGE_AUTHOR:-}" ] && command -v git >/dev/null 2>&1; then |
| 67 | + [ -n "${IMAGE_AUTHOR_EMAIL:-}" ] || IMAGE_AUTHOR_EMAIL="$(git config --get user.email || git config --get github.email || true)" |
| 68 | + [ -n "${IMAGE_AUTHOR_NAME:-}" ] || IMAGE_AUTHOR_NAME="$(git config --get user.name || git config --get github.user || true)" |
| 69 | + IMAGE_AUTHOR="${IMAGE_AUTHOR_NAME:+${IMAGE_AUTHOR_NAME} }<${IMAGE_AUTHOR_EMAIL:-}>" |
| 70 | + fi |
| 71 | + |
| 72 | + # Write the build labels to the build labels path for both the org.label-schema and org.opencontainers.image formats: |
| 73 | + write_to_build_labels "org.label-schema.title" "org.opencontainers.image.title" "${IMAGE_TITLE:-}" |
| 74 | + write_to_build_labels "org.label-schema.url" "org.opencontainers.image.url" "${IMAGE_URL:-}" |
| 75 | + write_to_build_labels "org.label-schema.vcs-ref" "org.opencontainers.image.revision" "${IMAGE_VCS_REF:-}" |
| 76 | + write_to_build_labels "org.label-schema.vcs-url" "org.opencontainers.image.source" "${IMAGE_VCS_URL:-}" |
| 77 | + write_to_build_labels "org.label-schema.vendor" "org.opencontainers.image.vendor" "${IMAGE_VENDOR:-}" |
| 78 | + write_to_build_labels "MAINTAINER" "maintainer" "org.opencontainers.image.authors" "${IMAGE_AUTHOR:-}" |
| 79 | + write_to_build_labels "org.label-schema.description" "org.opencontainers.image.description" "${IMAGE_DESCRIPTION:-}" |
| 80 | + write_to_build_labels "org.label-schema.usage" "org.opencontainers.image.documentation" "${IMAGE_DOCUMENTATION:-}" |
| 81 | + write_to_build_labels "org.label-schema.version" "org.opencontainers.image.version" "${IMAGE_VERSION:-}" |
| 82 | +} |
| 83 | + |
| 84 | +! (return 0 2>/dev/null) || write_apptainer_labels "$@" |
0 commit comments