| 1 | #!/usr/bin/env bash |
| 2 | # Upload dist/ release files to the GitLab Generic Package Registry and attach |
| 3 | # them to the GitLab Release so downloads do not depend on job-artifact |
| 4 | # permalinks (those 404 when the latest tag pipeline is failed or still running). |
| 5 | # |
| 6 | # Usage: |
| 7 | # publish-gitlab-release.sh [--dry-run] [--dist DIR] [tag] |
| 8 | # |
| 9 | # In CI, tag defaults to $CI_COMMIT_TAG. Dry-run prints the upload/link plan |
| 10 | # and does not call the GitLab API. |
| 11 | set -euo pipefail |
| 12 | |
| 13 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 14 | DIST="$ROOT/dist" |
| 15 | DRY_RUN=0 |
| 16 | TAG="${CI_COMMIT_TAG:-}" |
| 17 | |
| 18 | usage() { |
| 19 | echo "Usage: $0 [--dry-run] [--dist DIR] [tag]" >&2 |
| 20 | exit 2 |
| 21 | } |
| 22 | |
| 23 | while [[ $# -gt 0 ]]; do |
| 24 | case "$1" in |
| 25 | --dry-run) DRY_RUN=1; shift ;; |
| 26 | --dist) |
| 27 | [[ $# -ge 2 ]] || usage |
| 28 | DIST="$2" |
| 29 | shift 2 |
| 30 | ;; |
| 31 | -h|--help) usage ;; |
| 32 | *) |
| 33 | if [[ -n "$TAG" && "$TAG" != "$1" ]]; then |
| 34 | echo "Unexpected argument: $1" >&2 |
| 35 | usage |
| 36 | fi |
| 37 | TAG="$1" |
| 38 | shift |
| 39 | ;; |
| 40 | esac |
| 41 | done |
| 42 | |
| 43 | if [[ -z "$TAG" ]]; then |
| 44 | TAG="$(grep '^version = ' "$ROOT/Cargo.toml" | sed -E 's/.*"([^"]+)"/\1/')" |
| 45 | fi |
| 46 | VERSION="${TAG#v}" |
| 47 | |
| 48 | is_debug_pkg() { |
| 49 | local base |
| 50 | base="$(basename -- "$1")" |
| 51 | [[ "$base" == posprint-debug-* || "$base" == *-debug-*.pkg.tar.zst ]] |
| 52 | } |
| 53 | |
| 54 | # Prints: label<TAB>absolute-path |
| 55 | collect_assets() { |
| 56 | local f |
| 57 | shopt -s nullglob |
| 58 | local debs=("$DIST"/*.deb) |
| 59 | local pkgs=("$DIST"/posprint-*-x86_64.pkg.tar.zst) |
| 60 | local tgzs=("$DIST"/posprint-*-x86_64.tar.gz) |
| 61 | shopt -u nullglob |
| 62 | |
| 63 | if [[ ${#debs[@]} -ne 1 ]]; then |
| 64 | echo "Expected exactly one .deb in $DIST, found ${#debs[@]}" >&2 |
| 65 | ls -la "$DIST" >&2 || true |
| 66 | return 1 |
| 67 | fi |
| 68 | printf '%s\t%s\n' "Linux .deb (amd64)" "${debs[0]}" |
| 69 | |
| 70 | local main_pkg="" |
| 71 | for f in "${pkgs[@]+"${pkgs[@]}"}"; do |
| 72 | if ! is_debug_pkg "$f"; then |
| 73 | if [[ -n "$main_pkg" ]]; then |
| 74 | echo "Multiple non-debug Arch packages in $DIST" >&2 |
| 75 | return 1 |
| 76 | fi |
| 77 | main_pkg="$f" |
| 78 | fi |
| 79 | done |
| 80 | if [[ -z "$main_pkg" ]]; then |
| 81 | echo "No non-debug Arch .pkg.tar.zst in $DIST" >&2 |
| 82 | ls -la "$DIST" >&2 || true |
| 83 | return 1 |
| 84 | fi |
| 85 | printf '%s\t%s\n' "Arch Linux .pkg.tar.zst (x86_64)" "$main_pkg" |
| 86 | |
| 87 | if [[ ${#tgzs[@]} -ne 1 ]]; then |
| 88 | echo "Expected exactly one posprint-*-x86_64.tar.gz in $DIST, found ${#tgzs[@]}" >&2 |
| 89 | ls -la "$DIST" >&2 || true |
| 90 | return 1 |
| 91 | fi |
| 92 | printf '%s\t%s\n' "Linux binary tarball (x86_64, AUR posprint-bin)" "${tgzs[0]}" |
| 93 | |
| 94 | if [[ ! -f "$DIST/posprint.exe" ]]; then |
| 95 | echo "Missing $DIST/posprint.exe" >&2 |
| 96 | ls -la "$DIST" >&2 || true |
| 97 | return 1 |
| 98 | fi |
| 99 | printf '%s\t%s\n' "Windows .exe" "$DIST/posprint.exe" |
| 100 | } |
| 101 | |
| 102 | json_escape() { |
| 103 | printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' |
| 104 | } |
| 105 | |
| 106 | generic_file_url() { |
| 107 | local name="$1" |
| 108 | printf '%s/projects/%s/packages/generic/posprint/%s/%s' \ |
| 109 | "${CI_API_V4_URL}" "${CI_PROJECT_ID}" "${VERSION}" "${name}" |
| 110 | } |
| 111 | |
| 112 | ASSETS="$(collect_assets)" |
| 113 | |
| 114 | if [[ "$DRY_RUN" -eq 1 ]]; then |
| 115 | echo "tag=${TAG}" |
| 116 | echo "version=${VERSION}" |
| 117 | echo "dist=${DIST}" |
| 118 | while IFS=$'\t' read -r label path; do |
| 119 | name="$(basename -- "$path")" |
| 120 | printf 'UPLOAD\t%s\t%s\t%s\n' "$label" "$path" "$name" |
| 121 | printf 'LINK\t%s\t/%s\n' "$label" "$name" |
| 122 | done <<<"$ASSETS" |
| 123 | exit 0 |
| 124 | fi |
| 125 | |
| 126 | : "${CI_JOB_TOKEN:?CI_JOB_TOKEN is required to publish a GitLab release}" |
| 127 | : "${CI_API_V4_URL:?CI_API_V4_URL is required to publish a GitLab release}" |
| 128 | : "${CI_PROJECT_ID:?CI_PROJECT_ID is required to publish a GitLab release}" |
| 129 | |
| 130 | api() { |
| 131 | local method="$1" |
| 132 | local path="$2" |
| 133 | local body_file="${3:-}" |
| 134 | local out_file="$4" |
| 135 | local extra=() |
| 136 | if [[ -n "$body_file" ]]; then |
| 137 | extra+=(--header "Content-Type: application/json" --data @"$body_file") |
| 138 | fi |
| 139 | curl -sS -X "$method" \ |
| 140 | --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \ |
| 141 | "${extra[@]}" \ |
| 142 | -o "$out_file" -w "%{http_code}" \ |
| 143 | "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}${path}" |
| 144 | } |
| 145 | |
| 146 | upload_generic() { |
| 147 | local file="$1" |
| 148 | local name |
| 149 | name="$(basename -- "$file")" |
| 150 | local url |
| 151 | url="$(generic_file_url "$name")" |
| 152 | local tmp code |
| 153 | tmp="$(mktemp)" |
| 154 | code="$(curl -sS -o "$tmp" -w "%{http_code}" \ |
| 155 | --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \ |
| 156 | --upload-file "$file" \ |
| 157 | "$url")" |
| 158 | if [[ "$code" == "201" || "$code" == "200" ]]; then |
| 159 | echo "Uploaded $name ($code)" |
| 160 | rm -f "$tmp" |
| 161 | return 0 |
| 162 | fi |
| 163 | if [[ "$code" == "400" || "$code" == "409" ]] && grep -qiE 'already|exist|duplicate|taken|cannot be reused' "$tmp"; then |
| 164 | echo "Already uploaded $name (ok)" |
| 165 | rm -f "$tmp" |
| 166 | return 0 |
| 167 | fi |
| 168 | echo "Upload failed for $name ($code): $(cat "$tmp")" >&2 |
| 169 | rm -f "$tmp" |
| 170 | return 1 |
| 171 | } |
| 172 | |
| 173 | echo "Publishing GitLab release ${TAG} from ${DIST}" |
| 174 | |
| 175 | while IFS=$'\t' read -r _label path; do |
| 176 | upload_generic "$path" |
| 177 | done <<<"$ASSETS" |
| 178 | |
| 179 | LINKS_JSON="" |
| 180 | first=1 |
| 181 | while IFS=$'\t' read -r label path; do |
| 182 | name="$(basename -- "$path")" |
| 183 | url="$(generic_file_url "$name")" |
| 184 | item="$(printf '{"name":"%s","url":"%s","link_type":"package","direct_asset_path":"/%s"}' \ |
| 185 | "$(json_escape "$label")" \ |
| 186 | "$(json_escape "$url")" \ |
| 187 | "$(json_escape "$name")")" |
| 188 | if [[ "$first" -eq 1 ]]; then |
| 189 | LINKS_JSON="$item" |
| 190 | first=0 |
| 191 | else |
| 192 | LINKS_JSON="${LINKS_JSON},${item}" |
| 193 | fi |
| 194 | done <<<"$ASSETS" |
| 195 | |
| 196 | DESC="Cross-platform POS print designer release ${TAG}" |
| 197 | CREATE_BODY="$(mktemp)" |
| 198 | RESP="$(mktemp)" |
| 199 | trap 'rm -f "$CREATE_BODY" "$RESP"' EXIT |
| 200 | |
| 201 | cat >"$CREATE_BODY" <<EOF |
| 202 | {"name":"posprint ${TAG}","tag_name":"${TAG}","description":"$(json_escape "$DESC")","assets":{"links":[${LINKS_JSON}]}} |
| 203 | EOF |
| 204 | |
| 205 | code="$(api GET "/releases/${TAG}" "" "$RESP")" |
| 206 | if [[ "$code" == "404" ]]; then |
| 207 | code="$(api POST "/releases" "$CREATE_BODY" "$RESP")" |
| 208 | if [[ "$code" != "201" && "$code" != "200" ]]; then |
| 209 | echo "Failed to create release ($code): $(cat "$RESP")" >&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | echo "Created GitLab release ${TAG}" |
| 213 | exit 0 |
| 214 | fi |
| 215 | |
| 216 | if [[ "$code" != "200" ]]; then |
| 217 | echo "Failed to query release ${TAG} ($code): $(cat "$RESP")" >&2 |
| 218 | exit 1 |
| 219 | fi |
| 220 | |
| 221 | UPDATE_BODY="$(mktemp)" |
| 222 | cat >"$UPDATE_BODY" <<EOF |
| 223 | {"name":"posprint ${TAG}","description":"$(json_escape "$DESC")"} |
| 224 | EOF |
| 225 | code="$(api PUT "/releases/${TAG}" "$UPDATE_BODY" "$RESP")" |
| 226 | rm -f "$UPDATE_BODY" |
| 227 | if [[ "$code" != "200" ]]; then |
| 228 | echo "Failed to update release ${TAG} ($code): $(cat "$RESP")" >&2 |
| 229 | exit 1 |
| 230 | fi |
| 231 | |
| 232 | # Replace existing asset links so retries pick up new filenames/URLs. |
| 233 | code="$(api GET "/releases/${TAG}/assets/links" "" "$RESP")" |
| 234 | if [[ "$code" == "200" ]]; then |
| 235 | if command -v jq >/dev/null; then |
| 236 | mapfile -t LINK_IDS < <(jq -r '.[].id // empty' "$RESP" || true) |
| 237 | else |
| 238 | mapfile -t LINK_IDS < <(grep -oE '"id":[0-9]+' "$RESP" | sed 's/"id"://' || true) |
| 239 | fi |
| 240 | for link_id in "${LINK_IDS[@]+"${LINK_IDS[@]}"}"; do |
| 241 | [[ -n "$link_id" ]] || continue |
| 242 | del_code="$(api DELETE "/releases/${TAG}/assets/links/${link_id}" "" "$RESP")" |
| 243 | if [[ "$del_code" != "204" && "$del_code" != "200" ]]; then |
| 244 | echo "Warning: could not delete release link ${link_id} ($del_code): $(cat "$RESP")" >&2 |
| 245 | fi |
| 246 | done |
| 247 | fi |
| 248 | |
| 249 | while IFS=$'\t' read -r label path; do |
| 250 | name="$(basename -- "$path")" |
| 251 | url="$(generic_file_url "$name")" |
| 252 | link_body="$(mktemp)" |
| 253 | cat >"$link_body" <<EOF |
| 254 | {"name":"$(json_escape "$label")","url":"$(json_escape "$url")","link_type":"package","direct_asset_path":"/$(json_escape "$name")"} |
| 255 | EOF |
| 256 | code="$(api POST "/releases/${TAG}/assets/links" "$link_body" "$RESP")" |
| 257 | rm -f "$link_body" |
| 258 | if [[ "$code" != "201" && "$code" != "200" ]]; then |
| 259 | echo "Failed to add release link ${name} ($code): $(cat "$RESP")" >&2 |
| 260 | exit 1 |
| 261 | fi |
| 262 | done <<<"$ASSETS" |
| 263 | |
| 264 | echo "Updated GitLab release ${TAG} with Generic Package Registry assets" |