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..03456baa --- a/common/src/main/bash/projectType/pipeline-jvm.sh +++ b/common/src/main/bash/projectType/pipeline-jvm.sh @@ -12,16 +12,88 @@ function downloadAppBinary() { 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 artifactoryUsername="${5}" + local artifactoryPassword="${6}" local destination local changedGroupId local pathToJar destination="$(pwd)/${OUTPUT_FOLDER}/${artifactId}-${version}.${BINARY_EXTENSION}" - changedGroupId="$(echo "${groupId}" | tr . /)" + 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 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 nexusUsername="${6:-$M2_SETTINGS_REPO_USERNAME}" + local nexusPassword="${7:-$M2_SETTINGS_REPO_PASSWORD}" + local searchBaseUrl + local nexusApiSearchUrl + 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}" + 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="$(< "${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="${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 "${M2_SETTINGS_REPO_USERNAME}:${M2_SETTINGS_REPO_PASSWORD}" "${pathToJar}" -o "${destination}" --fail && success="true" + curl -u "${username}:${password}" "${pathToJar}" -o "${destination}" --fail && success="true" if [[ "${success}" == "true" ]]; then echo "File downloaded successfully!" return 0 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 +}