Created
June 16, 2022 09:41
-
-
Save WoozyMasta/69146c07b324db70d9335925b8a11c7d to your computer and use it in GitHub Desktop.
Script for rebuild indexes and invalidate cache in Sonatype nexus 3 npm|nuget|pypi|maven proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Script for rebuild indexes and invalidate cache in NPM proxy repositories. | |
# Configuration can load from hide file with same name as script | |
# Example: this-srcipt.sh load config from ./.this-srcipt | |
# | |
# Copyright 2020 WoozyMasta <[email protected]> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
set -euo pipefail | |
readonly binaries=(jq curl) | |
manage-nexus() { | |
local action; action=${1:-invalidate-cache} | |
_rc=$( | |
curl -X POST -sLku "$NEXUS_USER:$NEXUS_PASS" \ | |
-H "accept: application/json" \ | |
-w '%{response_code}' \ | |
"$NEXUS_URL/service/rest/v1/repositories/$repo/$action" | |
) | |
if [ "$_rc" -eq 204 ]; then | |
printf '%s\n' "Task ${action//-/ } for repository \"$repo\" completed." | |
else | |
>&2 printf '%s\n' \ | |
"Task ${action//-/ } for repository \"$repo\" failed with code $_rc " | |
fi | |
} | |
binary-exist () { | |
command -v "${1:-}" &>/dev/null || { | |
>&2 printf '%s\n' "Binary ${1:-nonne} not found. Terminated." | |
exit 1 | |
} | |
} | |
for x in "${binaries[@]}"; do binary-exist "$x"; done | |
config="./.$(basename -s .sh "$0")" | |
if [ -f "$config" ]; then | |
# shellcheck disable=SC1090 | |
. "$config" | |
fi | |
: "${NEXUS_URL:?Nexus registry URL not defined}" | |
: "${NEXUS_USER:?Nexus user login not defined}" | |
: "${NEXUS_PASS:?Nexus user passwordL not defined}" | |
mapfile -t repos < <( | |
curl -sfLku "$NEXUS_USER:$NEXUS_PASS" \ | |
"$NEXUS_URL/service/rest/v1/repositories" |\ | |
jq -er ' | |
.[] | | |
select(.format | test("npm|nuget|pypi|maven")) | | |
select(.type=="proxy") | | |
.name | |
' | |
) | |
for repo in "${repos[@]}"; do | |
manage-nexus rebuild-index | |
manage-nexus invalidate-cache | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment