Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Commit

Permalink
Fix shellcheck warnings in pipeline scripts (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
wybczu authored and marcingrzejszczak committed Sep 11, 2017
1 parent af4ad9b commit 33cf37a
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 93 deletions.
6 changes: 4 additions & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ sourceSets {
}
}

task shellcheck(type: Exec) {
task shellcheck {
sourceSets*.resources.srcDirs*.each { srcDir ->
fileTree(relativePath(srcDir)).include('**/*.sh').each { File script ->
commandLine "shellcheck", script
exec {
commandLine "shellcheck", script
}
}
}
}
1 change: 1 addition & 0 deletions common/src/main/bash/build_and_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

export ENVIRONMENT=BUILD

# shellcheck source=/dev/null
[[ -f "${__DIR}/pipeline.sh" ]] && source "${__DIR}/pipeline.sh" || \
echo "No pipeline.sh found"

Expand Down
3 changes: 2 additions & 1 deletion common/src/main/bash/build_api_compatibility_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

export ENVIRONMENT=BUILD

# shellcheck source=/dev/null
[[ -f "${__DIR}/pipeline.sh" ]] && source "${__DIR}/pipeline.sh" || \
echo "No pipeline.sh found"

apiCompatibilityCheck
apiCompatibilityCheck
14 changes: 4 additions & 10 deletions common/src/main/bash/pipeline-cf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ function deployService() {
deployEureka "${EUREKA_ARTIFACT_ID}-${EUREKA_VERSION}" "${serviceName}" "${ENVIRONMENT}"
;;
stubrunner)
UNIQUE_EUREKA_NAME="$( echo ${PARSED_YAML} | jq --arg x ${LOWER_CASE_ENV} '.[$x].services[] | select(.type == "eureka") | .name' | sed 's/^"\(.*\)"$/\1/' )"
UNIQUE_RABBIT_NAME="$( echo ${PARSED_YAML} | jq --arg x ${LOWER_CASE_ENV} '.[$x].services[] | select(.type == "rabbitmq") | .name' | sed 's/^"\(.*\)"$/\1/' )"
UNIQUE_EUREKA_NAME="$( echo ${PARSED_YAML} | jq --arg x ${ENVIRONMENT} '.[$x | ascii_downcase].services[] | select(.type == "eureka") | .name' | sed 's/^"\(.*\)"$/\1/' )"
UNIQUE_RABBIT_NAME="$( echo ${PARSED_YAML} | jq --arg x ${ENVIRONMENT} '.[$x | ascii_downcase].services[] | select(.type == "rabbitmq") | .name' | sed 's/^"\(.*\)"$/\1/' )"
PREVIOUS_IFS="${IFS}"
IFS=: read -r STUBRUNNER_GROUP_ID STUBRUNNER_ARTIFACT_ID STUBRUNNER_VERSION <<< "${serviceCoordinates}"
IFS="${PREVIOUS_IFS}"
PARSED_STUBRUNNER_USE_CLASSPATH="$( echo ${PARSED_YAML} | jq --arg x ${LOWER_CASE_ENV} '.[$x].services[] | select(.type == "stubrunner") | .useClasspath' | sed 's/^"\(.*\)"$/\1/' )"
PARSED_STUBRUNNER_USE_CLASSPATH="$( echo ${PARSED_YAML} | jq --arg x ${ENVIRONMENT} '.[$x | ascii_downcase].services[] | select(.type == "stubrunner") | .useClasspath' | sed 's/^"\(.*\)"$/\1/' )"
STUBRUNNER_USE_CLASSPATH=$( if [[ "${PARSED_STUBRUNNER_USE_CLASSPATH}" == "null" ]] ; then echo "false"; else echo "${PARSED_STUBRUNNER_USE_CLASSPATH}" ; fi )
downloadAppBinary ${REPO_WITH_BINARIES} ${STUBRUNNER_GROUP_ID} ${STUBRUNNER_ARTIFACT_ID} ${STUBRUNNER_VERSION}
deployStubRunnerBoot "${STUBRUNNER_ARTIFACT_ID}-${STUBRUNNER_VERSION}" "${REPO_WITH_BINARIES}" "${UNIQUE_RABBIT_NAME}" "${UNIQUE_EUREKA_NAME}" "${ENVIRONMENT}" "${serviceName}"
Expand Down Expand Up @@ -451,14 +451,8 @@ function propagatePropertiesForTests() {
cat ${fileLocation}
}

function toLowerCase() {
local string=${1}
local result=$( echo "${string}" | tr '[:upper:]' '[:lower:]' )
echo "${result}"
}

__DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# CURRENTLY WE ONLY SUPPORT JVM BASED PROJECTS OUT OF THE BOX
[[ -f "${__DIR}/projectType/pipeline-jvm.sh" ]] && source "${__DIR}/projectType/pipeline-jvm.sh" || \
echo "No projectType/pipeline-jvm.sh found"
echo "No projectType/pipeline-jvm.sh found"
82 changes: 44 additions & 38 deletions common/src/main/bash/pipeline.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/bash
set -e

set -o errexit
set -o errtrace
set -o pipefail

IFS=$' \n\t'

__ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# ---- BUILD PHASE ----
function build() {
Expand Down Expand Up @@ -82,16 +89,15 @@ function testResultsAntPattern() {

# Finds the latest prod tag from git
function findLatestProdTag() {
local LAST_PROD_TAG=$(git for-each-ref --sort=taggerdate --format '%(refname)' refs/tags/prod | head -n 1)
LAST_PROD_TAG=${LAST_PROD_TAG#refs/tags/}
echo "${LAST_PROD_TAG}"
local LAST_PROD_TAG
LAST_PROD_TAG=$( git for-each-ref --sort=taggerdate --format '%(refname)' refs/tags/prod | head -n 1 )
echo "${LAST_PROD_TAG#refs/tags/}"
}

# Extracts the version from the production tag
function extractVersionFromProdTag() {
local tag="${1}"
LAST_PROD_VERSION=${tag#prod/}
echo "${LAST_PROD_VERSION}"
echo "${tag#prod/}"
}

# Checks for existence of pipeline.yaml file that contains types and names of the
Expand Down Expand Up @@ -136,55 +142,55 @@ function serviceExists() {
# For other environments only deploys a service if it wasn't there.
# Uses ruby and jq
function deployServices() {
if [[ "$( pipelineDescriptorExists )" != "true" ]]; then
echo "No pipeline descriptor found - will not deploy any services"
return
fi

PARSED_YAML=$( yaml2json "pipeline.yml" )
export PARSED_YAML

while read -r serviceType serviceName serviceCoordinates; do
if [[ "${ENVIRONMENT}" == "TEST" ]]; then
deleteService "${serviceType}" "${serviceName}"
deployService "${serviceType}" "${serviceName}" "${serviceCoordinates}"
else
if [[ "$( serviceExists "${serviceName}" )" == "true" ]]; then
echo "Skipping deployment since service is already deployed"
else
deployService "${serviceType}" "${serviceName}" "${serviceCoordinates}"
fi
if [[ "$( pipelineDescriptorExists )" != "true" ]]; then
echo "No pipeline descriptor found - will not deploy any services"
return
fi
# retrieve the space separated type, name and coordinates
done <<< "$( echo "${PARSED_YAML}" | \
jq -r --arg x "${LOWER_CASE_ENV}" '.[$x].services[] | "\(.type) \(.name) \(.coordinates)"' )"

PARSED_YAML=$( yaml2json "pipeline.yml" )
export PARSED_YAML

while read -r serviceType serviceName serviceCoordinates; do
if [[ "${ENVIRONMENT}" == "TEST" ]]; then
deleteService "${serviceType}" "${serviceName}"
deployService "${serviceType}" "${serviceName}" "${serviceCoordinates}"
else
if [[ "$( serviceExists "${serviceName}" )" == "true" ]]; then
echo "Skipping deployment since service is already deployed"
else
deployService "${serviceType}" "${serviceName}" "${serviceCoordinates}"
fi
fi
# retrieve the space separated type, name and coordinates
done <<< "$( echo "${PARSED_YAML}" | \
jq -r --arg x "${ENVIRONMENT}" '.[$x | ascii_downcase].services[] | "\(.type) \(.name) \(.coordinates)"' )"
}

# Converts YAML to JSON - uses ruby
function yaml2json() {
ruby -ryaml -rjson -e \
'puts JSON.pretty_generate(YAML.load(ARGF))' $*
ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))' "$@"
}

function lowerCaseEnv() {
local string=${1}
echo "${ENVIRONMENT}" | tr '[:upper:]' '[:lower:]'
# Converts a string to lower case
function toLowerCase() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}

__ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# CURRENTLY WE ONLY SUPPORT CF AS PAAS OUT OF THE BOX
export PAAS_TYPE="${PAAS_TYPE:-cf}"
PAAS_TYPE="${PAAS_TYPE:-cf}"
export PAAS_TYPE

echo "Picked PAAS is [${PAAS_TYPE}]"
echo "Current environment is [${ENVIRONMENT}]"
export LOWER_CASE_ENV=$( lowerCaseEnv )

# shellcheck source=/dev/null
[[ -f "${__ROOT}/pipeline-${PAAS_TYPE}.sh" ]] && source "${__ROOT}/pipeline-${PAAS_TYPE}.sh" || \
echo "No pipeline-${PAAS_TYPE}.sh found"

export OUTPUT_FOLDER=$( outputFolder )
export TEST_REPORTS_FOLDER=$( testResultsAntPattern )
OUTPUT_FOLDER="$( outputFolder )"
TEST_REPORTS_FOLDER="$( testResultsAntPattern )"

export OUTPUT_FOLDER TEST_REPORTS_FOLDER

echo "Output folder [${OUTPUT_FOLDER}]"
echo "Test reports folder [${TEST_REPORTS_FOLDER}]"
1 change: 1 addition & 0 deletions common/src/main/bash/prod_complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

export ENVIRONMENT=PROD

# shellcheck source=/dev/null
[[ -f "${__DIR}/pipeline.sh" ]] && source "${__DIR}/pipeline.sh" || \
echo "No pipeline.sh found"

Expand Down
1 change: 1 addition & 0 deletions common/src/main/bash/prod_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

export ENVIRONMENT=PROD

# shellcheck source=/dev/null
[[ -f "${__DIR}/pipeline.sh" ]] && source "${__DIR}/pipeline.sh" || \
echo "No pipeline.sh found"

Expand Down
28 changes: 11 additions & 17 deletions common/src/main/bash/projectType/pipeline-gradle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ function build() {
echo "Additional Build Options [${BUILD_OPTIONS}]"

if [[ "${CI}" == "CONCOURSE" ]]; then
./gradlew clean build deploy -PnewVersion=${PIPELINE_VERSION} -DREPO_WITH_BINARIES=${REPO_WITH_BINARIES} --stacktrace ${BUILD_OPTIONS} || ( $( printTestResults ) && return 1)
./gradlew clean build deploy -PnewVersion="${PIPELINE_VERSION}" -DREPO_WITH_BINARIES="${REPO_WITH_BINARIES}" --stacktrace ${BUILD_OPTIONS} || ( printTestResults && return 1)
else
./gradlew clean build deploy -PnewVersion=${PIPELINE_VERSION} -DREPO_WITH_BINARIES=${REPO_WITH_BINARIES} --stacktrace ${BUILD_OPTIONS}
./gradlew clean build deploy -PnewVersion="${PIPELINE_VERSION}" -DREPO_WITH_BINARIES="${REPO_WITH_BINARIES}" --stacktrace ${BUILD_OPTIONS}
fi
}

function apiCompatibilityCheck() {
echo "Running retrieval of group and artifactid to download all dependencies. It might take a while..."
projectGroupId=$( retrieveGroupId )
appName=$( retrieveAppName )

# Find latest prod version
LATEST_PROD_TAG=$( findLatestProdTag )
Expand All @@ -27,31 +25,27 @@ function apiCompatibilityCheck() {
echo "Last prod version equals ${LATEST_PROD_VERSION}"
echo "Additional Build Options [${BUILD_OPTIONS}]"
if [[ "${CI}" == "CONCOURSE" ]]; then
./gradlew clean apiCompatibility -DlatestProductionVersion=${LATEST_PROD_VERSION} -DREPO_WITH_BINARIES=${REPO_WITH_BINARIES} --stacktrace ${BUILD_OPTIONS} || ( $( printTestResults ) && return 1)
./gradlew clean apiCompatibility -DlatestProductionVersion="${LATEST_PROD_VERSION}" -DREPO_WITH_BINARIES="${REPO_WITH_BINARIES}" --stacktrace ${BUILD_OPTIONS} || ( printTestResults && return 1)
else
./gradlew clean apiCompatibility -DlatestProductionVersion=${LATEST_PROD_VERSION} -DREPO_WITH_BINARIES=${REPO_WITH_BINARIES} --stacktrace ${BUILD_OPTIONS}
./gradlew clean apiCompatibility -DlatestProductionVersion="${LATEST_PROD_VERSION}" -DREPO_WITH_BINARIES="${REPO_WITH_BINARIES}" --stacktrace ${BUILD_OPTIONS}
fi
fi
}

function retrieveGroupId() {
local result=$( ./gradlew groupId -q )
result=$( echo "${result}" | tail -1 )
echo "${result}"
./gradlew groupId -q | tail -1
}

function retrieveAppName() {
local result=$( ./gradlew artifactId -q )
result=$( echo "${result}" | tail -1 )
echo "${result}"
./gradlew artifactId -q | tail -1
}

function printTestResults() {
echo -e "\n\nBuild failed!!! - will print all test results to the console (it's the easiest way to debug anything later)\n\n" && tail -n +1 "$( testResultsAntPattern )"
}

function retrieveStubRunnerIds() {
echo "$( ./gradlew stubIds -q | tail -1 )"
./gradlew stubIds -q | tail -1
}

function runSmokeTests() {
Expand All @@ -60,9 +54,9 @@ function runSmokeTests() {
echo "Running smoke tests"

if [[ "${CI}" == "CONCOURSE" ]]; then
./gradlew smoke -PnewVersion=${PIPELINE_VERSION} -Dapplication.url="${applicationHost}" -Dstubrunner.url="${stubrunnerHost}" || ( echo "$( printTestResults )" && return 1)
./gradlew smoke -PnewVersion="${PIPELINE_VERSION}" -Dapplication.url="${applicationHost}" -Dstubrunner.url="${stubrunnerHost}" || ( printTestResults && return 1)
else
./gradlew smoke -PnewVersion=${PIPELINE_VERSION} -Dapplication.url="${applicationHost}" -Dstubrunner.url="${stubrunnerHost}"
./gradlew smoke -PnewVersion="${PIPELINE_VERSION}" -Dapplication.url="${applicationHost}" -Dstubrunner.url="${stubrunnerHost}"
fi
}

Expand All @@ -73,9 +67,9 @@ function runE2eTests() {
echo "Running e2e tests"

if [[ "${CI}" == "CONCOURSE" ]]; then
./gradlew e2e -PnewVersion=${PIPELINE_VERSION} -Dapplication.url="${applicationHost}" ${BUILD_OPTIONS} || ( $( printTestResults ) && return 1)
./gradlew e2e -PnewVersion="${PIPELINE_VERSION}" -Dapplication.url="${applicationHost}" ${BUILD_OPTIONS} || ( printTestResults && return 1)
else
./gradlew e2e -PnewVersion=${PIPELINE_VERSION} -Dapplication.url="${applicationHost}" ${BUILD_OPTIONS}
./gradlew e2e -PnewVersion="${PIPELINE_VERSION}" -Dapplication.url="${applicationHost}" ${BUILD_OPTIONS}
fi
}

Expand Down
24 changes: 17 additions & 7 deletions common/src/main/bash/projectType/pipeline-jvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ function downloadAppBinary() {
local groupId="${2}"
local artifactId="${3}"
local version="${4}"
local destination="`pwd`/${OUTPUT_FOLDER}/${artifactId}-${version}.jar"
local changedGroupId="$( echo "${groupId}" | tr . / )"
local pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.jar"
local destination
local changedGroupId
local pathToJar

destination="$( pwd )/${OUTPUT_FOLDER}/${artifactId}-${version}.jar"
changedGroupId="$( echo "${groupId}" | tr . / )"
pathToJar="${repoWithJars}/${changedGroupId}/${artifactId}/${version}/${artifactId}-${version}.jar"
if [[ ! -e ${destination} ]]; then
mkdir -p "${OUTPUT_FOLDER}"
echo "Current folder is [`pwd`]; Downloading [${pathToJar}] to [${destination}]"
echo "Current folder is [$( pwd )]; Downloading [${pathToJar}] to [${destination}]"
(curl "${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"
Expand All @@ -41,11 +45,17 @@ function projectType() {
fi
}

PROJECT_TYPE=$( projectType )

export -f projectType
export PROJECT_TYPE=$( projectType )
export PROJECT_TYPE

echo "Project type [${PROJECT_TYPE}]"

lowerCaseProjectType=$( echo "${PROJECT_TYPE}" | tr '[:upper:]' '[:lower:]' )
__DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
[[ -f "${__DIR}/pipeline-${lowerCaseProjectType}.sh" ]] && source "${__DIR}/pipeline-${lowerCaseProjectType}.sh" || \
echo "No pipeline-${lowerCaseProjectType}.sh found"

# shellcheck source=/dev/null
[[ -f "${__DIR}/pipeline-${lowerCaseProjectType}.sh" ]] && \
source "${__DIR}/pipeline-${lowerCaseProjectType}.sh" || \
echo "No pipeline-${lowerCaseProjectType}.sh found"
Loading

0 comments on commit 33cf37a

Please sign in to comment.