Skip to content

Instantly share code, notes, and snippets.

@030
Last active February 25, 2023 21:55
Show Gist options
  • Save 030/666c99d8fc86e9f1cc0ad216e0190574 to your computer and use it in GitHub Desktop.
Save 030/666c99d8fc86e9f1cc0ad216e0190574 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
NEXUS_VERSION="${1:-3.21.1}"
NEXUS_API_VERSION="${2:-v1}"
NEXUS_DOCKER_NAME="${3:-nexus}"
NEXUS_DOCKER_PORT="${4:-9999}"
NEXUS_PW_ON_DISK="${5:-/tmp/nexus-tst-pass}"
NEXUS_HTTP_CONNECTOR_DOCKER_REGISTRY="${6:-8888}"
NEXUS_HTTP_CONNECTOR_DOCKER_REGISTRY_INTERNAL="${7:-8082}"
validate() {
if [ -z "${NEXUS_VERSION}" ] || [ -z "${NEXUS_API_VERSION}" ]; then
echo "NEXUS_VERSION and NEXUS_API_VERSION should be specified."
exit 1
fi
}
nexus() {
docker run --rm -d -p ${NEXUS_DOCKER_PORT}:8081 -p ${NEXUS_HTTP_CONNECTOR_DOCKER_REGISTRY}:${NEXUS_HTTP_CONNECTOR_DOCKER_REGISTRY_INTERNAL} --name ${NEXUS_DOCKER_NAME} "sonatype/nexus3:${NEXUS_VERSION}"
}
readiness() {
until docker logs ${NEXUS_DOCKER_NAME} | grep -q 'Started Sonatype Nexus OSS'; do
sleep 10
done
}
# Since nexus 3.17.0 the default 'admin123' was changed by an autogenerated
# one. This function retrieves the autogenerated password and if this file
# is unavailable, the default 'admin123' is returned.
password() {
mkdir -p "${NEXUS_PW_ON_DISK}"
pw=$(docker exec -i ${NEXUS_DOCKER_NAME} cat /nexus-data/admin.password || true)
if [[ "${pw}" =~ [0-9a-z-]{36} ]]; then
echo "admin.password file found"
else
echo "no admin.password file found. Setting password to old default admin pass..."
pw=admin123
fi
export PASSWORD=$pw
echo "${pw}" >${NEXUS_PW_ON_DISK}/${NEXUS_DOCKER_NAME}.txt
}
cleanup() {
docker stop ${NEXUS_DOCKER_NAME}
}
main() {
validate
nexus
readiness
password
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment