Skip to content

Instantly share code, notes, and snippets.

@slide
Created October 19, 2020 15:27
Show Gist options
  • Save slide/a8fc03e97cf8f19d31114dda3b9f6212 to your computer and use it in GitHub Desktop.
Save slide/a8fc03e97cf8f19d31114dda3b9f6212 to your computer and use it in GitHub Desktop.
def buildmap = [:]
pipeline {
agent {
label 'docker'
}
options {
timestamps()
buildDiscarder(logRotator(daysToKeepStr: '30'))
timeout(time: 1, unit: 'HOURS')
}
environment {
//Sets the codedir that have been changed under services sub-directories
CODEDIR = """${sh(
returnStdout: true,
script: '''#!/bin/bash
TEST=()
for i in 1 2 3; do
if [ -z "$i" ]; then TEST+=($i);
else TEST+=($i); fi
done
echo -n ${TEST[*]}
'''
)}"""
//Grabs service name from app.yaml, this is not used in this job but needed in the sub deploy job it is passed to
SOURCE_BRANCH_NAME = """${sh(
returnStdout: true,
script: """#!/bin/bash
if [[ ${BRANCH_NAME} =~ "PR-" ]]; then echo -n "${CHANGE_BRANCH}";
elif [ "${RELEASE_TAG}" = "true" ]; then echo -n "refs/tags/${BRANCH_NAME}";
else echo -n "${BRANCH_NAME}"; fi
"""
)}"""
DOCKER_BUILDKIT = 1
} // end environment
stages {
stage('Display Details') {
when {
beforeAgent true
anyOf {
branch "dev";
} //end anyOf
} //end when
steps {
script {
if(env.CODEDIR == null) {
echo "No changes found"
env.DONTBUILD = "true"
return;
}
env.PREVBUILD = currentBuild.previousBuild?.result
if (env.RELEASE_TAG == "true") {
if (env.BUILD_NUMBER != '1' ) {
if (env.PREVBUILD == 'SUCCESS') {
env.RELEASE = "false"
} else {
env.RELEASE = "true"
}
} else {
env.RELEASE = "true"
}
} else {
env.RELEASE = "false"
}
//The following sets the PRD or INT deploy targets based on if a tag versioned release
if (env.RELEASE == "true") {
env.ENV = "PRD"
} else {
env.ENV = "INT"
}
if (env.BRANCH_NAME == "master" && env.RELEASE == "false") {
env.INTEGRATION_TEST = "true"
} else {
env.INTEGRATION_TEST = "false"
}
if (env.BRANCH_NAME == "master" && env.RELEASE == "false") {
env.SECURITY_SCAN = "true"
} else {
env.SECURITY_SCAN = "false"
}
}//end script
} // End Steps
} // End Stage
stage ('Steps') {
when {
beforeAgent true
expression { env.DONTBUILD != "true" }
anyOf {
branch "dev";
} //end anyOf
} //end when
steps {
script {
codedirarray = CODEDIR.split(" ")
for (int i = 0; i < codedirarray.size(); i++) {
def codevar = codedirarray[i]
buildmap[codevar] = {
stage("Build and Unit Tests: " + codevar) {
script {
env.code = codevar
echo "code "+ code
echo "codevar " + codevar
echo "variable codevar ${codevar}"
echo "varaiable code ${code}"
sh '''
echo code ${code}
touch ${code}-build-code
#codevar is bad in sh
'''
echo "code "+ code
echo "codevar " + codevar
echo "2 ${codevar}"
echo "3 ${code}"
} //end script
echo "code "+ code
echo "codevar " + codevar
echo "variable codevar ${codevar}"
echo "varaiable code ${code}"
}//end build and unit test stage
stage("Functional Tests: " + codevar) {
script {
echo "code "+ code
echo "codevar " + codevar
echo "variable codevar ${codevar}"
echo "varaiable code ${code}"
sh '''
echo code ${code}
touch ${code}-functional-code
#codevar is bad in sh
'''
echo "code "+ code
echo "codevar " + codevar
echo "variable codevar ${codevar}"
echo "varaiable code ${code}"
} //end script
}//end functional tests
}//end buildmap
}//end codearray for itterarion
}//end script
}// end steps
} // End stage
stage ('parallel test') {
steps {
parallel buildmap
}
}
}//end stages
post {
always {
cleanWs()
}
} // end post
} // end pipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment