|
| 1 | +#!/bin/bash |
| 2 | +# ----------------------------------------------- |
| 3 | +# Publish package to Cloudsmith.io |
| 4 | +# ----------------------------------------------- |
| 5 | +help() { |
| 6 | + cat <<EOF |
| 7 | +Publish packages from a path to a package repository |
| 8 | +
|
| 9 | +All the necessary dependencies will be downloaded automatically if they are not already present |
| 10 | +
|
| 11 | +Usage: |
| 12 | + $0 |
| 13 | +
|
| 14 | +Flags: |
| 15 | + --token <string> Debian access token used to authenticate the commands |
| 16 | + --owner <string> Debian repository owner |
| 17 | + --repo <string> Name of the debian repository to publish to |
| 18 | + --help|-h Show this help |
| 19 | +
|
| 20 | +Optional Environment variables (instead of flags) |
| 21 | +
|
| 22 | +PUBLISH_TOKEN Equivalent to --token flag |
| 23 | +PUBLISH_OWNER Equivalent to --owner flag |
| 24 | +PUBLISH_REPO Equivalent to --repo flag |
| 25 | +
|
| 26 | +Examples: |
| 27 | + $0 \\ |
| 28 | + --token "mywonderfultoken" \\ |
| 29 | + --repo "community" \\ |
| 30 | + --path ./dist |
| 31 | +
|
| 32 | + \$ Publish all debian/alpine/rpm packages found under ./dist |
| 33 | +EOF |
| 34 | +} |
| 35 | + |
| 36 | +PUBLISH_TOKEN="${PUBLISH_TOKEN:-}" |
| 37 | +PUBLISH_OWNER="${PUBLISH_OWNER:-thinedge}" |
| 38 | +PUBLISH_REPO="${PUBLISH_REPO:-community}" |
| 39 | +SOURCE_PATH="./" |
| 40 | + |
| 41 | +# |
| 42 | +# Argument parsing |
| 43 | +# |
| 44 | +POSITIONAL=() |
| 45 | +while [[ $# -gt 0 ]] |
| 46 | +do |
| 47 | + case "$1" in |
| 48 | + # Repository owner |
| 49 | + --owner) |
| 50 | + PUBLISH_OWNER="$2" |
| 51 | + shift |
| 52 | + ;; |
| 53 | + |
| 54 | + # Token used to authenticate publishing commands |
| 55 | + --token) |
| 56 | + PUBLISH_TOKEN="$2" |
| 57 | + shift |
| 58 | + ;; |
| 59 | + |
| 60 | + # Where to look for the debian files to publish |
| 61 | + --path) |
| 62 | + SOURCE_PATH="$2" |
| 63 | + shift |
| 64 | + ;; |
| 65 | + |
| 66 | + # Which debian repo to publish to (under the given host url) |
| 67 | + --repo) |
| 68 | + PUBLISH_REPO="$2" |
| 69 | + shift |
| 70 | + ;; |
| 71 | + |
| 72 | + --help|-h) |
| 73 | + help |
| 74 | + exit 0 |
| 75 | + ;; |
| 76 | + |
| 77 | + -*) |
| 78 | + echo "Unrecognized flag" >&2 |
| 79 | + help |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + |
| 83 | + *) |
| 84 | + POSITIONAL+=("$1") |
| 85 | + ;; |
| 86 | + esac |
| 87 | + shift |
| 88 | +done |
| 89 | +set -- "${POSITIONAL[@]}" |
| 90 | + |
| 91 | +# Add local tools path |
| 92 | +LOCAL_TOOLS_PATH="$HOME/.local/bin" |
| 93 | +export PATH="$LOCAL_TOOLS_PATH:$PATH" |
| 94 | + |
| 95 | +# Install tooling if missing |
| 96 | +if ! [ -x "$(command -v cloudsmith)" ]; then |
| 97 | + echo 'Install cloudsmith cli' >&2 |
| 98 | + if command -v pip3 &>/dev/null; then |
| 99 | + pip3 install --upgrade cloudsmith-cli |
| 100 | + elif command -v pip &>/dev/null; then |
| 101 | + pip install --upgrade cloudsmith-cli |
| 102 | + else |
| 103 | + echo "Could not install cloudsmith cli. Reason: pip3/pip is not installed" |
| 104 | + exit 2 |
| 105 | + fi |
| 106 | +fi |
| 107 | + |
| 108 | + |
| 109 | +publish() { |
| 110 | + local sourcedir="$1" |
| 111 | + local pattern="$2" |
| 112 | + local package_type="$3" |
| 113 | + local distribution="$4" |
| 114 | + local distribution_version="$5" |
| 115 | + |
| 116 | + # Notes: Currently Cloudsmith does not support the following (this might change in the future) |
| 117 | + # * distribution and distribution_version must be selected from values in the list. use `cloudsmith list distros` to get the list |
| 118 | + # * The component can not be set and is currently fixed to 'main' |
| 119 | + find "$sourcedir" -name "$pattern" -print0 | while read -r -d $'\0' file |
| 120 | + do |
| 121 | + cloudsmith upload "$package_type" "${PUBLISH_OWNER}/${PUBLISH_REPO}/${distribution}/${distribution_version}" "$file" \ |
| 122 | + --no-wait-for-sync \ |
| 123 | + --api-key "${PUBLISH_TOKEN}" |
| 124 | + done |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +publish "$SOURCE_PATH" "*.deb" deb "any-distro" "any-version" |
| 129 | +publish "$SOURCE_PATH" "*.rpm" rpm "any-distro" "any-version" |
| 130 | +publish "$SOURCE_PATH" "*.apk" alpine "alpine" "any-version" |
0 commit comments