From e5708f30b0876feb75bb82e905267c5c27c55367 Mon Sep 17 00:00:00 2001 From: Mark Alston Date: Fri, 2 Mar 2018 09:01:09 -0700 Subject: [PATCH 1/3] Add functions for download artifact from nexus --- .../src/main/bash/projectType/pipeline-jvm.sh | 95 +++++++++++++++---- 1 file changed, 77 insertions(+), 18 deletions(-) mode change 100755 => 100644 common/src/main/bash/projectType/pipeline-jvm.sh diff --git a/common/src/main/bash/projectType/pipeline-jvm.sh b/common/src/main/bash/projectType/pipeline-jvm.sh old mode 100755 new mode 100644 index d42b0cd6..c68dcfa5 --- a/common/src/main/bash/projectType/pipeline-jvm.sh +++ b/common/src/main/bash/projectType/pipeline-jvm.sh @@ -8,27 +8,86 @@ export MAVEN_OPTS="${MAVEN_OPTS} -Djava.security.egd=file:///dev/urandom -Dorg.s export BINARY_EXTENSION="${BINARY_EXTENSION:-jar}" function downloadAppBinary() { - local repoWithJars="${1}" - local groupId="${2}" - local artifactId="${3}" - local version="${4}" + local repoWithJars="${1}" + local groupId="${2}" + local artifactId="${3}" + local version="${4}" + local artifactRepo="${5}" + local nexusApiBaseUrl="${6}" + local repoId="${7}" + if [[ "${artifactRepo}" == "nexus" ]]; then + downloadAppBinaryFromNexus "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" + elif [[ "${artifactRepo}" == "nexus-3" ]]; then + downloadAppBinaryFromNexus3 "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" + else + downloadAppBinaryFromArtifactory "${repoWithJars}" "${groupId}" "${artifactId}" "${version}" + fi +} + +function downloadAppBinaryFromArtifactory() { + local repoWithJars="${1}" + local groupId="${2}" + local artifactId="${3}" + local version="${4}" local destination local changedGroupId local pathToJar - destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" - changedGroupId="$(echo "${groupId}" | tr . /)" - pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.${BINARY_EXTENSION}" - mkdir -p "${OUTPUT_FOLDER}" - echo "Current folder is [$(pwd)]; Downloading binary to [${destination}]" - local success="false" - curl -u "${M2_SETTINGS_REPO_USERNAME}:${M2_SETTINGS_REPO_PASSWORD}" "${pathToJar}" -o "${destination}" --fail && success="true" - if [[ "${success}" == "true" ]]; then - echo "File downloaded successfully!" - return 0 - else - echo "Failed to download file!" - return 1 - fi + destination="`pwd`/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + changedGroupId="$( echo "${groupId}" | tr . / )" + pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.${BINARY_EXTENSION}" + downloadArtifact "${destination}" "${pathToJar}" +} + +function downloadAppBinaryFromNexus() { + local nexusApiBaseUrl="${1}" + local repoId="${2}" + local groupId="${3}" + local artifactId="${4}" + local version="${5}" + local destination + local changedGroupId + local pathToJar + destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + changedGroupId="$(echo "${groupId}" | tr . /)" + pathToJar="${nexusApiBaseUrl}/service/local/artifact/maven/redirect?r=${repoId}&g=${changedGroupId}&a=${artifactId}&v=${version}" + downloadArtifact "${destination}" "${pathToJar}" +} + +function downloadAppBinaryFromNexus3() { + local nexusApiBaseUrl="${1}" + local repoId="${2}" + local groupId="${3}" + local artifactId="${4}" + local version="${5}" + local searchBaseUrl + local nexusApiSearchUrl + local nexusUsername + local nexusPassword + local destination + local pathToJar + searchBaseUrl="${nexusApiBaseUrl}/service/siesta/rest/beta/search" + nexusApiSearchUrl="${searchBaseUrl}?repository=${repoId}&maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}&maven.extension=${BINARY_EXTENSION}" + nexusUsername=${M2_SETTINGS_SNAPSHOTS_REPO_USERNAME} + nexusPassword=${M2_SETTINGS_SNAPSHOTS_REPO_PASSWORD} + destination="`pwd`/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + pathToJar=$(curl -u ${nexusUsername}:${nexusPassword} -X GET --header 'Accept: application/json' ${nexusApiSearchUrl} | jq --raw-output '.items | reverse[0].assets[0].downloadUrl') + downloadArtifact "${destination}" "${pathToJar}" +} + +function downloadArtifact() { + local destination="${1}" + local pathToJar="${2}" + local username + local password + username=${M2_SETTINGS_SNAPSHOTS_REPO_USERNAME} + password=${M2_SETTINGS_SNAPSHOTS_REPO_PASSWORD} + if [[ ! -e ${destination} ]]; then + mkdir -p "${OUTPUT_FOLDER}" + echo "Current folder is [$(pwd)]; Downloading [${pathToJar}] to [${destination}]" + (curl -u "${username}:${password}" "${pathToJar}" -o "${destination}" --fail && echo "File downloaded successfully!") || (echo "Failed to download file!" && return 1) + else + echo "File [${destination}] exists. Will not download it again" + fi } function isMavenProject() { From 1876a1bac2670437a3bcb482dba2ed7bdb9e51ca Mon Sep 17 00:00:00 2001 From: Mark Alston Date: Mon, 5 Mar 2018 22:08:15 -0700 Subject: [PATCH 2/3] Add tests for downloading artifacts from nexus --- .../src/main/bash/projectType/pipeline-jvm.sh | 144 ++++++++++-------- common/src/test/bats/pipeline-jvm.bats | 76 ++++++++- 2 files changed, 152 insertions(+), 68 deletions(-) diff --git a/common/src/main/bash/projectType/pipeline-jvm.sh b/common/src/main/bash/projectType/pipeline-jvm.sh index c68dcfa5..12bf224f 100644 --- a/common/src/main/bash/projectType/pipeline-jvm.sh +++ b/common/src/main/bash/projectType/pipeline-jvm.sh @@ -8,86 +8,100 @@ export MAVEN_OPTS="${MAVEN_OPTS} -Djava.security.egd=file:///dev/urandom -Dorg.s export BINARY_EXTENSION="${BINARY_EXTENSION:-jar}" function downloadAppBinary() { - local repoWithJars="${1}" - local groupId="${2}" - local artifactId="${3}" - local version="${4}" - local artifactRepo="${5}" - local nexusApiBaseUrl="${6}" - local repoId="${7}" - if [[ "${artifactRepo}" == "nexus" ]]; then - downloadAppBinaryFromNexus "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" - elif [[ "${artifactRepo}" == "nexus-3" ]]; then - downloadAppBinaryFromNexus3 "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" - else - downloadAppBinaryFromArtifactory "${repoWithJars}" "${groupId}" "${artifactId}" "${version}" - fi + local repoWithJars="${1}" + local groupId="${2}" + local artifactId="${3}" + local version="${4}" + local artifactRepo="${5}" + local nexusApiBaseUrl="${6}" + local repoId="${7}" + local username="${8}" + local password="${9}" + if [[ "${artifactRepo}" == "nexus" ]]; then + downloadAppBinaryFromNexus "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" "${username}" "${password}" + elif [[ "${artifactRepo}" == "nexus-3" ]]; then + downloadAppBinaryFromNexus3 "${nexusApiBaseUrl}" "${repoId}" "${groupId}" "${artifactId}" "${version}" "${username}" "${password}" + else + downloadAppBinaryFromArtifactory "${repoWithJars}" "${groupId}" "${artifactId}" "${version}" "${username}" "${password}" + fi } function downloadAppBinaryFromArtifactory() { - local repoWithJars="${1}" - local groupId="${2}" - local artifactId="${3}" - local version="${4}" + local repoWithJars="${1}" + local groupId="${2}" + local artifactId="${3}" + local version="${4}" + local artifactoryUsername="${5}" + local artifactoryPassword="${6}" local destination local changedGroupId local pathToJar - destination="`pwd`/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" - changedGroupId="$( echo "${groupId}" | tr . / )" - pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.${BINARY_EXTENSION}" - downloadArtifact "${destination}" "${pathToJar}" + destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + changedGroupId="$( echo "${groupId}" | tr . / )" + pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.${BINARY_EXTENSION}" + downloadArtifact "${destination}" "${pathToJar}" "${artifactoryUsername}" "${artifactoryPassword}" } function downloadAppBinaryFromNexus() { - local nexusApiBaseUrl="${1}" - local repoId="${2}" - local groupId="${3}" - local artifactId="${4}" - local version="${5}" - local destination - local changedGroupId - local pathToJar - destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" - changedGroupId="$(echo "${groupId}" | tr . /)" - pathToJar="${nexusApiBaseUrl}/service/local/artifact/maven/redirect?r=${repoId}&g=${changedGroupId}&a=${artifactId}&v=${version}" - downloadArtifact "${destination}" "${pathToJar}" + local nexusApiBaseUrl="${1}" + local repoId="${2}" + local groupId="${3}" + local artifactId="${4}" + local version="${5}" + local nexusUsername="${6}" + local nexusPassword="${7}" + local destination + local changedGroupId + local pathToJar + destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + changedGroupId="$(echo "${groupId}" | tr . /)" + pathToJar="${nexusApiBaseUrl}/service/local/artifact/maven/redirect?r=${repoId}&g=${changedGroupId}&a=${artifactId}&v=${version}" + downloadArtifact "${destination}" "${pathToJar}" "${nexusUsername}" "${nexusPassword}" } function downloadAppBinaryFromNexus3() { - local nexusApiBaseUrl="${1}" - local repoId="${2}" - local groupId="${3}" - local artifactId="${4}" - local version="${5}" - local searchBaseUrl - local nexusApiSearchUrl - local nexusUsername - local nexusPassword - local destination - local pathToJar - searchBaseUrl="${nexusApiBaseUrl}/service/siesta/rest/beta/search" - nexusApiSearchUrl="${searchBaseUrl}?repository=${repoId}&maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}&maven.extension=${BINARY_EXTENSION}" - nexusUsername=${M2_SETTINGS_SNAPSHOTS_REPO_USERNAME} - nexusPassword=${M2_SETTINGS_SNAPSHOTS_REPO_PASSWORD} - destination="`pwd`/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" - pathToJar=$(curl -u ${nexusUsername}:${nexusPassword} -X GET --header 'Accept: application/json' ${nexusApiSearchUrl} | jq --raw-output '.items | reverse[0].assets[0].downloadUrl') - downloadArtifact "${destination}" "${pathToJar}" + local nexusApiBaseUrl="${1}" + local repoId="${2}" + local groupId="${3}" + local artifactId="${4}" + local version="${5}" + local nexusUsername="${6:-$M2_SETTINGS_REPO_USERNAME}" + local nexusPassword="${7:-$M2_SETTINGS_REPO_PASSWORD}" + local searchBaseUrl + local nexusApiSearchUrl + local destination + local pathToJar + local downloadUrl + searchBaseUrl="${nexusApiBaseUrl}/service/siesta/rest/beta/search" + nexusApiSearchUrl="${searchBaseUrl}?repository=${repoId}&maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}&maven.extension=${BINARY_EXTENSION}" + destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" + mkdir -p "${OUTPUT_FOLDER}" + curl -u "${nexusUsername}:${nexusPassword}" -X GET --header "Accept: application/json" "${nexusApiSearchUrl}" -o "${OUTPUT_FOLDER}/artifacts.json" --fail && success="true" + if [[ "${success}" == "true" ]]; then + pathToJar="$(cat ${OUTPUT_FOLDER}/artifacts.json | jq --raw-output '.items | reverse[0].assets[0].downloadUrl')" + downloadArtifact "${destination}" "${pathToJar}" "${nexusUsername}" "${nexusPassword}" + else + echo "Failed to find path to jar!" + return 1 + fi } function downloadArtifact() { - local destination="${1}" - local pathToJar="${2}" - local username - local password - username=${M2_SETTINGS_SNAPSHOTS_REPO_USERNAME} - password=${M2_SETTINGS_SNAPSHOTS_REPO_PASSWORD} - if [[ ! -e ${destination} ]]; then - mkdir -p "${OUTPUT_FOLDER}" - echo "Current folder is [$(pwd)]; Downloading [${pathToJar}] to [${destination}]" - (curl -u "${username}:${password}" "${pathToJar}" -o "${destination}" --fail && echo "File downloaded successfully!") || (echo "Failed to download file!" && return 1) - else - echo "File [${destination}] exists. Will not download it again" - fi + local destination="${1}" + local pathToJar="${2}" + local username="${3:-$M2_SETTINGS_REPO_USERNAME}" + local password="${4:-$M2_SETTINGS_REPO_PASSWORD}" + mkdir -p "${OUTPUT_FOLDER}" + echo "Current folder is [$(pwd)]; Downloading binary to [${destination}]" + local success="false" + curl -u "${username}:${password}" "${pathToJar}" -o "${destination}" --fail && success="true" + if [[ "${success}" == "true" ]]; then + echo "File downloaded successfully!" + return 0 + else + echo "Failed to download file!" + return 1 + fi } function isMavenProject() { diff --git a/common/src/test/bats/pipeline-jvm.bats b/common/src/test/bats/pipeline-jvm.bats index 5858692c..3be0025a 100644 --- a/common/src/test/bats/pipeline-jvm.bats +++ b/common/src/test/bats/pipeline-jvm.bats @@ -64,7 +64,7 @@ export -f curl assert_equal "${PROJECT_TYPE}" "GRADLE" } -@test "should download an artifact if file hasn't been downloaded" { +@test "should download an artifact from artifactory if file hasn't been downloaded" { export OUTPUT_FOLDER="${TEMP_DIR}/output" source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" @@ -75,7 +75,7 @@ export -f curl assert_success } -@test "should download a WAR artifact if file hasn't been downloaded" { +@test "should download a WAR artifact from artifactory if file hasn't been downloaded" { export OUTPUT_FOLDER="${TEMP_DIR}/output" export BINARY_EXTENSION="war" @@ -88,7 +88,7 @@ export -f curl assert_success } -@test "should exit 1 when failed to download the artifact" { +@test "should exit 1 when failed to download the artifact from artifactory" { export OUTPUT_FOLDER="${TEMP_DIR}/output" source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" @@ -98,3 +98,73 @@ export -f curl assert_output --partial 'Failed to download file!' assert_failure } + +@test "should download an artifact from nexus v2 if file hasn't been downloaded" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus" "http://artifactrepo.com" "maven-releases" + + assert_output --partial 'File downloaded successfully' + assert_success +} + +@test "should download a WAR artifact from nexus v2 if file hasn't been downloaded" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + export BINARY_EXTENSION="war" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus" "http://artifactrepo.com" "maven-releases" + + assert_output --partial 'artifactId-version.war' + assert_output --partial 'File downloaded successfully' + assert_success +} + +@test "should exit 1 when failed to download the artifact from nexus v2" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus" "failed" "maven-releases" + + assert_output --partial 'Failed to download file!' + assert_failure +} + +@test "should download an artifact from nexus v3 if file hasn't been downloaded" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus-3" "http://artifactrepo.com" "maven-releases" + + assert_output --partial 'File downloaded successfully' + assert_success +} + +@test "should download a WAR artifact from nexus v3 if file hasn't been downloaded" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + export BINARY_EXTENSION="war" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus-3" "http://artifactrepo.com" "maven-releases" + + assert_output --partial 'artifactId-version.war' + assert_output --partial 'File downloaded successfully' + assert_success +} + +@test "should exit 1 when failed to download the artifact from nexus v3" { + export OUTPUT_FOLDER="${TEMP_DIR}/output" + + source "${SOURCE_DIR}/projectType/pipeline-jvm.sh" + + run downloadAppBinary "repoWithJars" "group.id" "artifactId" "version" "nexus-3" "failed" "maven-releases" + + assert_output --partial 'Failed to find path to jar!' + assert_failure +} From 8e628168612f4c3690d48782ed68a6dbaa2a598d Mon Sep 17 00:00:00 2001 From: Mark Alston Date: Mon, 5 Mar 2018 22:48:57 -0700 Subject: [PATCH 3/3] Fix shellcheck errors --- common/src/main/bash/projectType/pipeline-jvm.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/src/main/bash/projectType/pipeline-jvm.sh b/common/src/main/bash/projectType/pipeline-jvm.sh index 12bf224f..03456baa 100644 --- a/common/src/main/bash/projectType/pipeline-jvm.sh +++ b/common/src/main/bash/projectType/pipeline-jvm.sh @@ -71,14 +71,13 @@ function downloadAppBinaryFromNexus3() { local nexusApiSearchUrl local destination local pathToJar - local downloadUrl searchBaseUrl="${nexusApiBaseUrl}/service/siesta/rest/beta/search" nexusApiSearchUrl="${searchBaseUrl}?repository=${repoId}&maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}&maven.extension=${BINARY_EXTENSION}" destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" mkdir -p "${OUTPUT_FOLDER}" curl -u "${nexusUsername}:${nexusPassword}" -X GET --header "Accept: application/json" "${nexusApiSearchUrl}" -o "${OUTPUT_FOLDER}/artifacts.json" --fail && success="true" if [[ "${success}" == "true" ]]; then - pathToJar="$(cat ${OUTPUT_FOLDER}/artifacts.json | jq --raw-output '.items | reverse[0].assets[0].downloadUrl')" + pathToJar="$(< "${OUTPUT_FOLDER}/artifacts.json" jq --raw-output '.items | reverse[0].assets[0].downloadUrl')" downloadArtifact "${destination}" "${pathToJar}" "${nexusUsername}" "${nexusPassword}" else echo "Failed to find path to jar!"