|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Publishes the Maven artifacts of this project into a separate, public Git |
| 4 | +# repository that holds a plain Maven repository layout. That repository is |
| 5 | +# served read-only (and token-free) through raw.githubusercontent.com and can |
| 6 | +# hold the artifacts of several libraries side by side, because Maven's layout |
| 7 | +# already namespaces them by groupId and artifactId. |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# .github/scripts/publish-maven-repo.sh [version] |
| 11 | +# |
| 12 | +# Without an argument the version from pom.xml is published as-is (typically a |
| 13 | +# SNAPSHOT). With an argument the pom version is set to it for the build only; |
| 14 | +# pom.xml is restored afterwards, so the working tree keeps its development |
| 15 | +# version and nothing has to be committed back to the source branch. |
| 16 | +# |
| 17 | +# Environment: |
| 18 | +# MAVEN_REPO_REMOTE git remote of the target repository |
| 19 | +# (default: git@github.com:Tob1as864/maven-repo.git) |
| 20 | +# MAVEN_REPO_BRANCH branch to publish to (default: main) |
| 21 | +# CHECKOUT_DIR where to clone it (default: .maven-repo) |
| 22 | +# PUSH set to "false" for a local dry run (default: true) |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +REMOTE="${MAVEN_REPO_REMOTE:-git@github.com:Tob1as864/maven-repo.git}" |
| 27 | +BRANCH="${MAVEN_REPO_BRANCH:-main}" |
| 28 | +CHECKOUT_DIR="${CHECKOUT_DIR:-.maven-repo}" |
| 29 | +PUSH="${PUSH:-true}" |
| 30 | +RELEASE_VERSION="${1:-}" |
| 31 | + |
| 32 | +MVN="${MVN:-mvn}" |
| 33 | +REPO_ROOT="$(git rev-parse --show-toplevel)" |
| 34 | +cd "$REPO_ROOT" |
| 35 | + |
| 36 | +log() { printf '\n==> %s\n' "$*"; } |
| 37 | + |
| 38 | +# --- clone the target repository --------------------------------------------- |
| 39 | +log "Cloning $REMOTE" |
| 40 | +rm -rf "$CHECKOUT_DIR" |
| 41 | +git clone --quiet --depth 1 "$REMOTE" "$CHECKOUT_DIR" |
| 42 | + |
| 43 | +REPO_DIR="$(cd "$CHECKOUT_DIR" && pwd)" |
| 44 | + |
| 45 | +# A freshly created repository has no commits, so HEAD is unborn and no branch |
| 46 | +# ref exists yet; -B creates the branch in that case and switches to it in all |
| 47 | +# others. |
| 48 | +if [ "$(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD)" != "$BRANCH" ]; then |
| 49 | + git -C "$REPO_DIR" checkout -q -B "$BRANCH" |
| 50 | +fi |
| 51 | + |
| 52 | +# --- determine and validate the version to publish --------------------------- |
| 53 | +if [ -n "$RELEASE_VERSION" ]; then |
| 54 | + log "Setting project version to $RELEASE_VERSION" |
| 55 | + POM_BACKUP="$(mktemp)" |
| 56 | + cp pom.xml "$POM_BACKUP" |
| 57 | + # Restore the development version even when the build below fails. |
| 58 | + trap 'cp "$POM_BACKUP" "$REPO_ROOT/pom.xml"; rm -f "$POM_BACKUP"' EXIT |
| 59 | + "$MVN" -B --no-transfer-progress versions:set \ |
| 60 | + -DnewVersion="$RELEASE_VERSION" -DgenerateBackupPoms=false |
| 61 | +fi |
| 62 | + |
| 63 | +VERSION="$("$MVN" -B -q --no-transfer-progress help:evaluate \ |
| 64 | + -Dexpression=project.version -DforceStdout)" |
| 65 | +GROUP_ID="$("$MVN" -B -q --no-transfer-progress help:evaluate \ |
| 66 | + -Dexpression=project.groupId -DforceStdout)" |
| 67 | +ARTIFACT_ID="$("$MVN" -B -q --no-transfer-progress help:evaluate \ |
| 68 | + -Dexpression=project.artifactId -DforceStdout)" |
| 69 | +ARTIFACT_DIR="$REPO_DIR/$(printf '%s' "$GROUP_ID" | tr '.' '/')/$ARTIFACT_ID/$VERSION" |
| 70 | + |
| 71 | +case "$VERSION" in |
| 72 | + *-SNAPSHOT) ;; |
| 73 | + *) |
| 74 | + # Released versions are immutable: never silently overwrite one. |
| 75 | + if [ -d "$ARTIFACT_DIR" ]; then |
| 76 | + echo "ERROR: $GROUP_ID:$ARTIFACT_ID:$VERSION already exists in $REMOTE." >&2 |
| 77 | + echo " Bump the version or delete it there first." >&2 |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + ;; |
| 81 | +esac |
| 82 | + |
| 83 | +# --- build and deploy into the checkout -------------------------------------- |
| 84 | +log "Deploying $GROUP_ID:$ARTIFACT_ID:$VERSION into $REPO_DIR" |
| 85 | +"$MVN" -B --no-transfer-progress -Prelease clean deploy -Dmaven.repo.dir="$REPO_DIR" |
| 86 | + |
| 87 | +# --- landing page ------------------------------------------------------------ |
| 88 | +# The repository is shared by several libraries, so only write a README when it |
| 89 | +# has none yet; an existing one is maintained by hand and must not be clobbered. |
| 90 | +if [ ! -e "$REPO_DIR/README.md" ]; then |
| 91 | + cat > "$REPO_DIR/README.md" <<'README' |
| 92 | +# Maven repository |
| 93 | +
|
| 94 | +This repository holds released Java artifacts in Maven repository layout. Its |
| 95 | +contents are **generated** by the release workflows of the individual library |
| 96 | +repositories - do not commit here by hand. |
| 97 | +
|
| 98 | +Consume it without any authentication: |
| 99 | +
|
| 100 | +```xml |
| 101 | +<repositories> |
| 102 | + <repository> |
| 103 | + <id>tob1as864</id> |
| 104 | + <url>https://raw.githubusercontent.com/Tob1as864/maven-repo/main</url> |
| 105 | + </repository> |
| 106 | +</repositories> |
| 107 | +``` |
| 108 | +
|
| 109 | +Then declare the library you need as an ordinary dependency. Browse the |
| 110 | +directory tree above for the available groupIds, artifacts and versions. |
| 111 | +README |
| 112 | +fi |
| 113 | + |
| 114 | +# --- commit and push ---------------------------------------------------------- |
| 115 | +git -C "$REPO_DIR" add -A |
| 116 | +if git -C "$REPO_DIR" diff --cached --quiet; then |
| 117 | + log "No changes to publish" |
| 118 | + exit 0 |
| 119 | +fi |
| 120 | + |
| 121 | +git -C "$REPO_DIR" commit -q -m "Publish $GROUP_ID:$ARTIFACT_ID $VERSION" |
| 122 | +log "Committed $GROUP_ID:$ARTIFACT_ID $VERSION" |
| 123 | + |
| 124 | +if [ "$PUSH" != "true" ]; then |
| 125 | + log "PUSH=$PUSH - skipping push (dry run)" |
| 126 | + exit 0 |
| 127 | +fi |
| 128 | + |
| 129 | +for delay in 2 4 8 16 0; do |
| 130 | + if git -C "$REPO_DIR" push -u origin "$BRANCH"; then |
| 131 | + log "Pushed to $REMOTE ($BRANCH)" |
| 132 | + exit 0 |
| 133 | + fi |
| 134 | + [ "$delay" -eq 0 ] && break |
| 135 | + echo "Push failed, retrying in ${delay}s ..." >&2 |
| 136 | + sleep "$delay" |
| 137 | +done |
| 138 | + |
| 139 | +echo "ERROR: could not push to $REMOTE" >&2 |
| 140 | +exit 1 |
0 commit comments