|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright OpenSearch Contributors |
| 4 | +###### Information ############################################################################ |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +# |
| 7 | +# The OpenSearch Contributors require contributions made to |
| 8 | +# this file be licensed under the Apache-2.0 license or a |
| 9 | +# compatible open source license. |
| 10 | +# |
| 11 | +# Name: publish-snapshot.sh |
| 12 | +# Language: Shell |
| 13 | +# |
| 14 | +# About: Deploy opensearch artifacts to a sonatype snapshot repository. |
| 15 | +# This script will search POM files under the passed in directory and publish artifacts to |
| 16 | +# a snapshot repository using the mvn deploy plugin. |
| 17 | +# |
| 18 | +# Usage: ./publish-snapshot.sh <directory> |
| 19 | +# |
| 20 | +############################################################################################### |
| 21 | +set -e |
| 22 | + |
| 23 | +[ -z "${1:-}" ] && { |
| 24 | + usage |
| 25 | +} |
| 26 | + |
| 27 | +usage() { |
| 28 | + echo "usage: $0 [-h] [dir]" |
| 29 | + echo " dir parent directory of artifacts to be published to org/opensearch namespace." |
| 30 | + echo " example: dir = ~/.m2/repository/org/opensearch where dir contains artifacts of a path:" |
| 31 | + echo " /maven/reindex-client/1.0.0-SNAPSHOT/reindex-client-1.0.0-SNAPSHOT.jar" |
| 32 | + echo " -h display help" |
| 33 | + echo "Required environment variables:" |
| 34 | + echo "SONATYPE_USERNAME - username with publish rights to a sonatype repository" |
| 35 | + echo "SONATYPE_PASSWORD - password for sonatype" |
| 36 | + echo "SNAPSHOT_REPO_URL - repository URL ex. http://localhost:8081/nexus/content/repositories/snapshots/" |
| 37 | + exit 1 |
| 38 | +} |
| 39 | + |
| 40 | +while getopts ":h" option; do |
| 41 | + case $option in |
| 42 | + h) |
| 43 | + usage |
| 44 | + ;; |
| 45 | + \?) |
| 46 | + echo "Invalid option -$OPTARG" >&2 |
| 47 | + usage |
| 48 | + ;; |
| 49 | + esac |
| 50 | +done |
| 51 | + |
| 52 | +[ -z "${SONATYPE_USERNAME}" ] && { |
| 53 | + echo "SONATYPE_USERNAME is required" |
| 54 | + exit 1 |
| 55 | +} |
| 56 | + |
| 57 | +[ -z "${SONATYPE_PASSWORD}" ] && { |
| 58 | + echo "SONATYPE_PASSWORD is required" |
| 59 | + exit 1 |
| 60 | +} |
| 61 | + |
| 62 | +[ -z "${SNAPSHOT_REPO_URL}" ] && { |
| 63 | + echo "REPO_URL is required" |
| 64 | + exit 1 |
| 65 | +} |
| 66 | + |
| 67 | +if [ ! -d "$1" ]; then |
| 68 | + echo "Invalid directory $1 does not exist" |
| 69 | + usage |
| 70 | +fi |
| 71 | + |
| 72 | +create_maven_settings() { |
| 73 | + # Create a settings.xml file with the user+password for maven |
| 74 | + mvn_settings="${workdir}/mvn-settings.xml" |
| 75 | + cat >${mvn_settings} <<-EOF |
| 76 | +<?xml version="1.0" encoding="UTF-8" ?> |
| 77 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 78 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 79 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 |
| 80 | + http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 81 | + <servers> |
| 82 | + <server> |
| 83 | + <id>nexus</id> |
| 84 | + <username>${SONATYPE_USERNAME}</username> |
| 85 | + <password>${SONATYPE_PASSWORD}</password> |
| 86 | + </server> |
| 87 | + </servers> |
| 88 | +</settings> |
| 89 | +EOF |
| 90 | +} |
| 91 | + |
| 92 | +url="${SNAPSHOT_REPO_URL}" |
| 93 | +workdir=$(mktemp -d) |
| 94 | + |
| 95 | +function cleanup() { |
| 96 | + rm -rf "${workdir}" |
| 97 | +} |
| 98 | + |
| 99 | +trap cleanup TERM INT EXIT |
| 100 | + |
| 101 | +create_maven_settings |
| 102 | + |
| 103 | +cd "$1" |
| 104 | + |
| 105 | +echo "searching for poms under $PWD" |
| 106 | + |
| 107 | +pomFiles="$(find "." -name '*.pom')" |
| 108 | +if [ -z "${pomFiles}" ]; then |
| 109 | + echo "No artifacts found under $PWD" |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | + |
| 113 | +for pom in ${pomFiles}; do |
| 114 | + pom_dir="$(dirname "${pom}")" |
| 115 | + for FILE in "${pom_dir}"/*; do |
| 116 | + # The POM is deployed with the artifact in a single deploy-file command, we can skip over it |
| 117 | + if [[ $FILE != $pom ]] && |
| 118 | + [[ $FILE != *"test-fixtures"* ]] && # This is a hack to ensure the OpenSearch build-tools test fixture jar is not uploaded instead of the actual build-tools jar. |
| 119 | + [[ $FILE != *"javadoc"* ]] && |
| 120 | + [[ $FILE != *"sources"* ]]; then |
| 121 | + extension="${FILE##*.}" |
| 122 | + case $extension in jar | war | zip) |
| 123 | + echo "Uploading: ${FILE} with ${pom} to ${url}" |
| 124 | + mvn --settings="${mvn_settings}" deploy:deploy-file \ |
| 125 | + -DgeneratePom=false \ |
| 126 | + -DrepositoryId=nexus \ |
| 127 | + -Durl="${SNAPSHOT_REPO_URL}" \ |
| 128 | + -DpomFile="${pom}" \ |
| 129 | + -Dfile="${FILE}" || echo "Failed to upload ${FILE}" |
| 130 | + ;; |
| 131 | + *) echo "Skipping upload for ${FILE}" ;; |
| 132 | + esac |
| 133 | + fi |
| 134 | + done |
| 135 | + |
| 136 | + echo "Finished uploading ${pom_dir}" |
| 137 | +done |
| 138 | + |
| 139 | +echo "===========================================" |
| 140 | +echo "Done." |
| 141 | +echo "===========================================" |
0 commit comments