|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Given a list of PostgreSQL versions (defined as directories in the root |
| 4 | +# folder of the project), this script generates a JSON object that will be used |
| 5 | +# inside the Github workflows as a strategy to create a matrix of jobs to run. |
| 6 | +# The JSON object contains, for each PostgreSQL version, the tags of the |
| 7 | +# container image to be built. |
| 8 | +# |
| 9 | +set -eu |
| 10 | + |
| 11 | +# Define an optional aliases for some major versions |
| 12 | +declare -A aliases=( |
| 13 | + [14]='latest' |
| 14 | +) |
| 15 | + |
| 16 | +cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}/..")")" |
| 17 | +BASE_DIRECTORY="$(pwd)" |
| 18 | + |
| 19 | + |
| 20 | +# Retrieve the PostgreSQL versions for Debian |
| 21 | +cd ${BASE_DIRECTORY}/Debian |
| 22 | +for version in */; do |
| 23 | + [[ $version == src/ ]] && continue |
| 24 | + debian_versions+=("$version") |
| 25 | +done |
| 26 | +debian_versions=("${debian_versions[@]%/}") |
| 27 | + |
| 28 | +# Sort the version numbers with highest first |
| 29 | +mapfile -t debian_versions < <(IFS=$'\n'; sort -rV <<< "${debian_versions[*]}") |
| 30 | + |
| 31 | +# prints "$2$1$3$1...$N" |
| 32 | +join() { |
| 33 | + local sep="$1" |
| 34 | + shift |
| 35 | + local out |
| 36 | + printf -v out "${sep//%/%%}%s" "$@" |
| 37 | + echo "${out#$sep}" |
| 38 | +} |
| 39 | + |
| 40 | +entries=() |
| 41 | +for version in "${debian_versions[@]}"; do |
| 42 | + |
| 43 | + # Read versions from the definition file |
| 44 | + versionFile="${version}/.versions.json" |
| 45 | + postgresImageVersion=$(jq -r '.POSTGRES_IMAGE_VERSION | split("-") | .[0]' "${versionFile}") |
| 46 | + releaseVersion=$(jq -r '.IMAGE_RELEASE_VERSION' "${versionFile}") |
| 47 | + |
| 48 | + # Initial aliases are "major version", "optional alias", "full version with release" |
| 49 | + # i.e. "14", "latest", "14.2-1", "14.2-debian","14.2" |
| 50 | + versionAliases=( |
| 51 | + "${version}" |
| 52 | + ${aliases[$version]:+"${aliases[$version]}"} |
| 53 | + "${postgresImageVersion}-${releaseVersion}" |
| 54 | + "${postgresImageVersion}" |
| 55 | + ) |
| 56 | + # Add all the version prefixes between full version and major version |
| 57 | + # i.e "13.2" |
| 58 | + while [ "$postgresImageVersion" != "$version" ] && [ "${postgresImageVersion%[.-]*}" != "$postgresImageVersion" ]; do |
| 59 | + versionAliases+=("$postgresImageVersion-debian") |
| 60 | + postgresImageVersion="${postgresImageVersion%[.-]*}" |
| 61 | + done |
| 62 | + # Support platform for container images |
| 63 | + platforms="linux/amd64" |
| 64 | + |
| 65 | + # Build the json entry |
| 66 | + entries+=( |
| 67 | + "{\"name\": \"Debian ${postgresImageVersion}\", \"platforms\": \"$platforms\", \"dir\": \"Debian/$version\", \"file\": \"Debian/$version/Dockerfile\", \"version\": \"$version\", \"tags\": [\"$(join "\", \"" "${versionAliases[@]}")\"]}" |
| 68 | + ) |
| 69 | +done |
| 70 | + |
| 71 | +# Build the strategy as a JSON object |
| 72 | +strategy="{\"fail-fast\": false, \"matrix\": {\"include\": [$(join ', ' "${entries[@]}")]}}" |
| 73 | +jq -C . <<<"$strategy" # sanity check / debugging aid |
| 74 | +echo "::set-output name=strategy::$(jq -c . <<<"$strategy")" |
0 commit comments