Skip to content

Instantly share code, notes, and snippets.

@rkalit
Created November 15, 2021 09:02
Show Gist options
  • Select an option

  • Save rkalit/bbf1324b809dc59d8b2b8c34732d5008 to your computer and use it in GitHub Desktop.

Select an option

Save rkalit/bbf1324b809dc59d8b2b8c34732d5008 to your computer and use it in GitHub Desktop.
def build_status = false
def test_status = false
pipeline {
agent {
node {
label 'jslave'
}
}
environment {
service_name = "${JOB_NAME}".split('/').first()
uri_blueocean= "{your_blueocean_uri}"
slack_channel = '{channel_name}'
slack_user = '{slack_user}'
oss_bucket = '{bucket_name}'
build_tool = sh (
script: ''' if [[ -f pom.xml ]]; then
echo 'mvnw'
elif [[ -f build.gradle ]]; then
echo 'gradlew'
fi ''',
returnStdout: true
).trim()
env_name = sh (
script: ''' if [[ ${GIT_BRANCH} == *'feature'* ]] || [[ ${GIT_BRANCH} == *'hotfix'* ]] || [[ ${GIT_BRANCH} == *'bugfix'* ]]; then
echo 'feature'
elif [[ ${GIT_BRANCH} == *'master' ]]; then
echo 'alpha'
else
echo 'build not allowed in this branch'
fi ''',
returnStdout: true
).trim()
}
stages {
stage ('Build & Test') {
parallel {
stage ('Build') {
steps {
script {
try {
sh '''
/path/to/script/JenkinsFunction.sh build
'''
build_status = true
} catch (Exception err) {
build_status = false
sh '''
export color=\"danger\"
export pretext=\"*Status of ${service_name} service*\"
export text=\"The Operation on *Build Stage* is failed, see the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
}
}
}
stage ('Test') {
steps {
script {
if (env.build_tool == "mvnw") {
try {
sh ''' ./mvnw test '''
test_status = true
} catch (Exception err) {
test_status = false
}
} else if (env.build_tool == 'gradlew') {
try {
sh ''' ./gradlew test '''
test_status = true
} catch (Exception err) {
test_status = false
}
} else {
echo "Can't specify the build tool, skip stage Test."
test_status = false
}
if (!test_status) {
sh '''
export color=\"danger\"
export pretext=\"*Status of ${service_name} service*\"
export text=\"The Operation on *Test Stage* is failed, see the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
}
}
}
}
}
stage ('Packaging and Dockerize Image') {
when {
expression {
build_status && test_status
}
}
parallel {
stage ('Build and push docker image') {
steps {
script {
sh '''
if [[ \${service_name} == "heimdall" ]]; then
jarDirectory="server/target"
else
jarDirectory="target"
fi
./dbuild.sh \${jarDirectory}/*.jar \${service_name} spring
/path/to/script/JenkinsFunction.sh push
'''
}
}
}
stage ('Packaging Jar and store to alibaba OSS') {
steps {
script {
sh '''
/path/to/script/JenkinsFunction.sh pack
'''
}
}
}
}
}
stage ('Deploy') {
when {
expression {
build_status && test_status
}
}
steps {
script {
try {
sh '''
/path/to/script/JenkinsFunction.sh deploy
if [ \$env_name == 'feature' ]; then
export text=\"The Deploy Operation of *${service_name}* for the environment *${env_name}* is success, see detail on ${uri_blueocean}\"
elif [ \$env_name == 'alpha' ]; then
export text=\"The Deploy Operation of *${service_name}* for the environment *${env_name}* is success, confirm whether to *release* or *not* on ${uri_blueocean}\"
fi
export color=\"good\"
export pretext=\"*Status of ${service_name} service*\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
// ask for Version input from user
if (env.env_name == 'alpha') {
try {
timeout(time: 1, unit: 'DAYS') {
env.userChoice = input message: 'Do you want to Release this build?',
parameters: [choice(name: 'Versioning Service', choices: 'no\nyes', description: 'Choose "yes" if you want to release this build')]
}
if (userChoice == 'no') {
echo "User refuse to release this build, stopping...."
}
} catch (Exception err) {
sh '''
export color=\"gray\"
export pretext=\"*Status of ${service_name} service*\"
export text=\"The Operation of *${service_name}* is success aborted, see the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
}
} catch (Exception err) {
sh '''
export color=\"danger\"
export pretext=\"*Status of ${service_name} service on build number: ${BUILD_NUMBER}*\"
export text=\"The Deploy Operation of *${service_name}* for the environment *${env_name}* is failed, see the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
}
}
}
stage ('Release Build') {
when {
environment name: 'userChoice', value: 'yes'
}
steps {
script {
try {
timeout(time: 1, unit: 'DAYS') {
env.version_name = input (
id: 'version', message: 'Input version name', parameters: [
[$class: 'TextParameterDefinition', description: 'Whatever you type here will be your version', name: 'Version']
]
)
}
try {
sh '''
if [[ \${service_name} == "heimdall" ]]; then
jarDirectory="server/target"
else
jarDirectory="target"
fi
./dbuild.sh \${jarDirectory}/*.jar \${service_name} spring \${version_name}
/path/to/script/JenkinsFunction.sh release \${version_name}
export text=\"The *Release* Operation of *${service_name}* is success, see detail log on ${uri_blueocean}\"
export color=\"good\"
export pretext=\"*Status of ${service_name} service*\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
} catch (Exception err) {
sh '''
export color=\"danger\"
export pretext=\"*Status of ${service_name} servic*\"
export text=\"The Release Stage of *${service_name}* is failed, check the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
} catch (Exception err) {
sh '''
export color=\"gray\"
export pretext=\"*Status of ${service_name} service*\"
export text=\"The Operation of *${service_name}* is aborted, see the detail on ${uri_blueocean}\"
bash -x /path/to/script/JenkinsFunction.sh notify
'''
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment