|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +export PS4='+($LINENO): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' |
| 5 | +trap 'handle_error ${LINENO}' ERR |
| 6 | + |
| 7 | +# shellcheck disable=SC2317 |
| 8 | +handle_error() { |
| 9 | + local exit_code=$? |
| 10 | + local line_number=$1 |
| 11 | + echo "Error: Command failed with exit code $exit_code at line $line_number" >&2 |
| 12 | + exit 1 |
| 13 | +} |
| 14 | + |
| 15 | +error() { |
| 16 | + local reason="${1:-}" |
| 17 | + if [[ -n "$reason" ]]; then |
| 18 | + echo "Error: $reason" >&2 |
| 19 | + else |
| 20 | + echo "Error" >&2 |
| 21 | + fi |
| 22 | + exit 1 |
| 23 | +} |
| 24 | + |
| 25 | +# Default values |
| 26 | +DRY_RUN="false" |
| 27 | + |
| 28 | +show_help() { |
| 29 | + echo "Usage: $0 --project <project> --build-names <builds> --bundle-name <name> --version <version> [OPTIONS]" >&2 |
| 30 | + echo "" >&2 |
| 31 | + echo "Create JFrog release bundles from a configurable list of build names" >&2 |
| 32 | + echo "" >&2 |
| 33 | + echo "Required Arguments:" >&2 |
| 34 | + echo " --project <project> JFrog Artifactory project name" >&2 |
| 35 | + echo " --build-names <builds> Comma-separated list of build names to include" >&2 |
| 36 | + echo " --bundle-name <name> Name for the release bundle" >&2 |
| 37 | + echo " --version <version> Version of the release bundle" >&2 |
| 38 | + echo "" >&2 |
| 39 | + echo "Options:" >&2 |
| 40 | + echo " --dry-run Show what would be done without actually doing it" >&2 |
| 41 | + echo " --help, -h Show this help message" >&2 |
| 42 | + echo "" >&2 |
| 43 | + echo "Examples:" >&2 |
| 44 | + echo " $0 --project database --build-names 'db-build-1,db-build-2' --bundle-name database-release --version v1.0.0" >&2 |
| 45 | + echo " $0 --project app --build-names 'app-build' --bundle-name app-release --version v2.1.0 --dry-run" >&2 |
| 46 | +} |
| 47 | + |
| 48 | +# Parse command line arguments |
| 49 | +while [[ $# -gt 0 ]]; do |
| 50 | + case $1 in |
| 51 | + --project) |
| 52 | + PROJECT="$2" |
| 53 | + shift 2 |
| 54 | + ;; |
| 55 | + --build-names) |
| 56 | + BUILD_NAMES="$2" |
| 57 | + shift 2 |
| 58 | + ;; |
| 59 | + --bundle-name) |
| 60 | + BUNDLE_NAME="$2" |
| 61 | + shift 2 |
| 62 | + ;; |
| 63 | + --version) |
| 64 | + VERSION="$2" |
| 65 | + shift 2 |
| 66 | + ;; |
| 67 | + --dry-run) |
| 68 | + DRY_RUN="true" |
| 69 | + shift |
| 70 | + ;; |
| 71 | + --help|-h) |
| 72 | + show_help |
| 73 | + exit 0 |
| 74 | + ;; |
| 75 | + -*) |
| 76 | + echo "Unknown option: $1" >&2 |
| 77 | + show_help |
| 78 | + exit 1 |
| 79 | + ;; |
| 80 | + *) |
| 81 | + echo "Unexpected positional argument: $1" >&2 |
| 82 | + show_help |
| 83 | + exit 1 |
| 84 | + ;; |
| 85 | + esac |
| 86 | +done |
| 87 | + |
| 88 | +# Validate required arguments |
| 89 | +if [[ -z "${PROJECT:-}" ]]; then |
| 90 | + echo "--project is required." |
| 91 | + show_help |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | + |
| 95 | +if [[ -z "${BUILD_NAMES:-}" ]]; then |
| 96 | + echo "--build-names is required." |
| 97 | + show_help |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | + |
| 101 | +if [[ -z "${BUNDLE_NAME:-}" ]]; then |
| 102 | + echo "--bundle-name is required." |
| 103 | + show_help |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | + |
| 107 | +if [[ -z "${VERSION:-}" ]]; then |
| 108 | + echo "--version is required." |
| 109 | + show_help |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | + |
| 113 | +# Wrapper function that either executes or echoes commands |
| 114 | +run() { |
| 115 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 116 | + local green='\033[0;32m' |
| 117 | + local reset='\033[0m' |
| 118 | + echo -e "${green} $*${reset}" >&2 |
| 119 | + else |
| 120 | + "$@" |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +main() { |
| 125 | + echo "Command line: $0 $*" >&2 |
| 126 | + |
| 127 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 128 | + echo "Would execute create-release-bundle workflow" >&2 |
| 129 | + echo "The following JFrog commands would be executed:" >&2 |
| 130 | + echo "" >&2 |
| 131 | + else |
| 132 | + echo "Executing create-release-bundle workflow" >&2 |
| 133 | + fi |
| 134 | + |
| 135 | + echo "Project: $PROJECT" >&2 |
| 136 | + echo "Build names: $BUILD_NAMES" >&2 |
| 137 | + echo "Bundle name: $BUNDLE_NAME" >&2 |
| 138 | + echo "Version: $VERSION" >&2 |
| 139 | + echo "Dry run: $DRY_RUN" >&2 |
| 140 | + |
| 141 | + # Convert comma-separated build names to array |
| 142 | + IFS=',' read -ra BUILD_ARRAY <<< "$BUILD_NAMES" |
| 143 | + mkdir -p build-artifacts |
| 144 | + # Create the release bundle spec file |
| 145 | + cat > build-artifacts/release-bundle-spec.json <<EOF |
| 146 | +{ |
| 147 | + "name": "$BUNDLE_NAME", |
| 148 | + "version": "$VERSION", |
| 149 | + "description": "Release for build version $VERSION", |
| 150 | + "files": [ |
| 151 | +$(for ((i=0; i<${#BUILD_ARRAY[@]}; i++)); do |
| 152 | + build_name=$(echo "${BUILD_ARRAY[i]}" | xargs) |
| 153 | + if [[ -n "$build_name" ]]; then |
| 154 | + echo " {" |
| 155 | + echo " \"project\": \"$PROJECT\"," |
| 156 | + echo " \"build\": \"$build_name/$VERSION\"" |
| 157 | + if [[ $i -lt $((${#BUILD_ARRAY[@]}-1)) ]]; then |
| 158 | + echo " }," |
| 159 | + else |
| 160 | + echo " }" |
| 161 | + fi |
| 162 | + fi |
| 163 | +done) |
| 164 | + ] |
| 165 | +} |
| 166 | +EOF |
| 167 | + |
| 168 | + echo "Spec file:" >&2 |
| 169 | + cat build-artifacts/release-bundle-spec.json >&2 |
| 170 | + |
| 171 | + # Create the release bundle |
| 172 | + run jf release-bundle-create "$BUNDLE_NAME" "$VERSION" \ |
| 173 | + --spec build-artifacts/release-bundle-spec.json \ |
| 174 | + --project="$PROJECT" \ |
| 175 | + --signing-key="aerospike" |
| 176 | + |
| 177 | + echo "Create release bundle workflow completed successfully!" >&2 |
| 178 | +} |
| 179 | + |
| 180 | +main "$@" |
0 commit comments