Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 92 additions & 54 deletions release.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/bin/bash -i

#!/usr/bin/env bash

#
# Copyright The WildFly Authors
# SPDX-License-Identifier: Apache-2.0
#

# Strict mode: fail on error, fail on unset vars, fail on pipe failure
set -o errexit
set -o nounset
set -o pipefail

# Formatting functions

fail() {
printf "${RED}%s${CLEAR}\n\n" "${1}"
printf "%s%s%s\n\n" "${RED}" "${1}" "${CLEAR}"
printHelp
exit 1
}

failNoHelp() {
printf "${RED}%s${CLEAR}\n" "${1}"
printf "%s%s%s\n" "${RED}" "${1}" "${CLEAR}"
exit 1
}

Expand Down Expand Up @@ -45,10 +51,13 @@ CLEAR=""
RED=""
YELLOW=""

if [[ -t 1 ]] && [[ -z "${NO_COLOR-}" ]] && [ "$(tput colors)" -ge 8 ]; then
CLEAR="\033[0m"
RED="\033[0;31m"
YELLOW="\033[0;33m"
# check if stdout is a terminal and NO_COLOR is not set
if [[ -t 1 ]] && [[ -z "${NO_COLOR-}" ]]; then
if command -v tput >/dev/null 2>&1; then
CLEAR=$(tput sgr0)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
fi
fi

DRY_RUN=false
Expand All @@ -58,18 +67,14 @@ RELEASE_VERSION=""
SCRIPT_PATH=$(realpath "${0}")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
LOCAL_REPO="/tmp/m2/repository/$(basename "${SCRIPT_DIR}")"
VERBOSE=""
VERBOSE=false
GH_RELEASE_TYPE="--latest"
START_TAG=()

MAVEN_ARGS=()
DAYS="${DAYS:-5}" # Default to 5 if not set

if [ -z "${DAYS}" ]; then
DAYS="5"
fi

while [ "$#" -gt 0 ]
do
# Parse arguments
while [ "$#" -gt 0 ]; do
case "${1}" in
-d|--development)
DEVEL_VERSION="${2}"
Expand Down Expand Up @@ -97,7 +102,7 @@ do
shift
;;
-v|--verbose)
VERBOSE="-v"
VERBOSE=true
;;
*)
MAVEN_ARGS+=("${1}")
Expand All @@ -106,6 +111,7 @@ do
shift
done

# Validation
if [ -z "${DEVEL_VERSION}" ]; then
fail "The development version is required."
fi
Expand All @@ -114,17 +120,27 @@ if [ -z "${RELEASE_VERSION}" ]; then
fail "The release version is required."
fi

if [ ${FORCE} == false ]; then
if echo "${RELEASE_VERSION}" | grep -q "SNAPSHOT" ; then
failNoHelp "The release version appears to be a SNAPSHOT (${RELEASE_VERSION}). This is likely no valid and -f should be used if it is."
if [ "${FORCE}" == "false" ]; then
# Native bash string matching instead of grep
if [[ "${RELEASE_VERSION}" == *"SNAPSHOT"* ]]; then
failNoHelp "The release version appears to be a SNAPSHOT (${RELEASE_VERSION}). This is likely invalid. Use -f to force."
fi
if echo "${DEVEL_VERSION}" | grep -q -v "SNAPSHOT" ; then
failNoHelp "The development version does not appear to be a SNAPSHOT (${DEVEL_VERSION}). This is likely no valid and -f should be used if it is."
if [[ "${DEVEL_VERSION}" != *"SNAPSHOT"* ]]; then
failNoHelp "The development version does not appear to be a SNAPSHOT (${DEVEL_VERSION}). This is likely invalid. Use -f to force."
fi
fi

# Find the expected
SERVER_ID=$(mvn help:evaluate -Dexpression=sonatype.server.id -q -DforceStdout "${MAVEN_ARGS[@]}")
# Find the expected Server ID
# We temporarily disable set -e here because mvn might fail if args are bad, and we want to capture that
set +e
SERVER_ID=$(mvn help:evaluate -Dexpression=sonatype.server.id -q -DforceStdout "${MAVEN_ARGS[@]}" | sed 's/^\[INFO\] \[stdout\] //')
RET_CODE=$?
set -e

if [ $RET_CODE -ne 0 ]; then
failNoHelp "Failed to evaluate Maven expression. Please check your Maven arguments."
fi

# Check the settings to ensure a server defined with that value
if ! mvn help:effective-settings | grep -q "<id>${SERVER_ID}</id>"; then
failNoHelp "A server with the id of \"${SERVER_ID}\" was not found in your settings.xml file."
Expand All @@ -133,54 +149,76 @@ fi
printf "Performing release for version %s with the next version of %s\n" "${RELEASE_VERSION}" "${DEVEL_VERSION}"

TAG_NAME="v${RELEASE_VERSION}"
MVN_FLAGS=()

if ${DRY_RUN}; then
echo "This will be a dry run and nothing will be updated or pushed."
MAVEN_ARGS+=("-DdryRun" "-DpushChanges=false")
MVN_FLAGS+=("-DdryRun" "-DpushChanges=false")
fi

# Clean up local repo
if [ -d "${LOCAL_REPO}" ]; then
# Verbose flag logic for rm
RM_FLAGS="-rf"
if ${VERBOSE}; then RM_FLAGS="-rfv"; fi

# Delete any directories over a day old
find "${LOCAL_REPO}" -type d -mtime +"${DAYS}" -print0 | xargs -0 -I {} rm -rf ${VERBOSE} "{}"
# Delete any SNAPSHOT's
find "${LOCAL_REPO}" -type d -name "*SNAPSHOT" -print0 | xargs -0 -I {} rm -rf ${VERBOSE} "{}"
find "${LOCAL_REPO}" -type d -mtime +"${DAYS}" -print0 | xargs -0 -I {} rm "${RM_FLAGS}" "{}"
# Delete any SNAPSHOTs
find "${LOCAL_REPO}" -type d -name "*SNAPSHOT" -print0 | xargs -0 -I {} rm "${RM_FLAGS}" "{}"

# Delete directories associated with this project
PROJECT_PATH="$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout "${MAVEN_ARGS[@]}")"
# Safe replacement of dots with slashes
PROJECT_PATH="${LOCAL_REPO}/${PROJECT_PATH//./\/}"
rm -rf ${VERBOSE} "${PROJECT_PATH}"

if [ -d "${PROJECT_PATH}" ]; then
rm "${RM_FLAGS}" "${PROJECT_PATH}"
fi
fi

command="mvn clean release:clean release:prepare release:perform -Dmaven.repo.local=\"${LOCAL_REPO}\" -DdevelopmentVersion=\"${DEVEL_VERSION}\" -DreleaseVersion=\"${RELEASE_VERSION}\" -Dtag=\"${TAG_NAME}\" \"${MAVEN_ARGS[*]}\""
# Create the command
CMD=(mvn clean release:clean release:prepare release:perform)
CMD+=("-Dmaven.repo.local=${LOCAL_REPO}")
CMD+=("-DdevelopmentVersion=${DEVEL_VERSION}")
CMD+=("-DreleaseVersion=${RELEASE_VERSION}")
CMD+=("-Dtag=${TAG_NAME}")

# Append extra maven flags calculated earlier
if [ ${#MVN_FLAGS[@]} -gt 0 ]; then
CMD+=("${MVN_FLAGS[@]}")
fi

if [ "-v" = "${VERBOSE}" ]; then
printf "\n\nExecuting:\n "
echo "${command}"
# Append any pass-through arguments
if [ ${#MAVEN_ARGS[@]} -gt 0 ]; then
CMD+=("${MAVEN_ARGS[@]}")
fi

if ${VERBOSE}; then
printf "\n\nExecuting:\n %s\n" "${CMD[*]}"
fi

# Execute the command
eval "${command}"
status=$?

if [ ${status} = 0 ]; then
if command -v gh &>/dev/null; then
result="$(gh repo set-default --view 2>&1)"
if [[ "${result}" =~ "gh repo set-default" ]]; then
echo ""
echo -e "${RED}The no default repository has been set. You must use gh repo set-default to set a default repository before executing the following commands.${CLEAR}"
echo ""
echo "gh release create --generate-notes --latest --verify-tag ${TAG_NAME}"
# "${CMD[@]}" expands the array respecting spaces within arguments
"${CMD[@]}"

# GitHub Release Handling

if command -v gh &>/dev/null; then
# Check for default repo quietly
if ! gh repo set-default --view &>/dev/null; then
echo ""
echo -e "${RED}No default repository has been set. You must use 'gh repo set-default' to set a default repository before executing the following commands.${CLEAR}"
echo ""
echo "gh release create --generate-notes ${START_TAG[*]} ${GH_RELEASE_TYPE} --verify-tag ${TAG_NAME}"
else
if ${DRY_RUN}; then
printf "${YELLOW}Dry run would execute:${CLEAR}\ngh release create --generate-notes %s %s --verify-tag %s\n" "${START_TAG[*]}" "${GH_RELEASE_TYPE}" "${TAG_NAME}"
else
if ${DRY_RUN}; then
printf "${YELLOW}Dry run would execute:${CLEAR}\ngh release create --generate-notes ${START_TAG[*]} ${GH_RELEASE_TYPE} --verify-tag %s\n" "${TAG_NAME}"
else
gh release create --generate-notes "${START_TAG[@]}" ${GH_RELEASE_TYPE} --verify-tag "${TAG_NAME}"
fi
gh release create --generate-notes "${START_TAG[@]}" "${GH_RELEASE_TYPE}" --verify-tag "${TAG_NAME}"
fi
else
echo ""
echo "The gh commands are not available. You must manually create a release for the GitHub tag ${TAG_NAME}."
fi
else
failNoHelp "\nThe release has failed. See the previous errors and try again. The command executed was:\n%s\n" "${command}"
echo ""
echo "The gh commands are not available. You must manually create a release for the GitHub tag ${TAG_NAME}."
fi
exit ${status}