maasglobal_cicdtest/Jenkinsfile

171 lines
6.1 KiB
Groovy

def params
def String[] envs
def String[] folders
def rootdirlist = ''
def envlist = ''
pipeline {
options {
ansiColor('xterm')
}
agent{label 'on-prem-slaves'}
environment {
AUTH = credentials('proxy')
proxy_user = "${AUTH_USR}"
proxy_pw = "${AUTH_PSW}"
http_proxy="http://${proxy_user}:${proxy_pw}@proxy.muc:8080"
https_proxy="http://${proxy_user}:${proxy_pw}@proxy.muc:8080"
no_proxy="localhost,127.0.0.1,.muc,.bmwgroup.net"
HTTP_PROXY="${http_proxy}"
HTTPS_PROXY="${https_proxy}"
NO_PROXY="${no_proxy}"
CICD_Testing_TOKEN = credentials('dynatrace_automation_testing_token')
}
stages {
stage('Git checkout') {
steps {
git credentialsId: 'jaws_dynatrace_bitbuket_user', url: 'https://atc.bmwgroup.net/bitbucket/scm/opapm/maasglobal_cicdtest.git'
sh '''
git branch -r | sed -e 's/origin\\///g' |sed '/HEAD/d' >> branchlist.txt
'''
}
}
stage ('Branch selection') {
steps {
script {
def branchlist = readFile 'branchlist.txt'
echo "please click on the link here to chose the branch to build"
env.BRANCH_SCOPE = input (message: 'Please choose the branch to build ',
parameters: [choice(name: 'BRANCH_NAME', choices: branchlist, description: 'Branch to build?')])
}
}
}
stage ('Environment and Application selection') {
steps {
sh '''
git checkout ${BRANCH_SCOPE}
sed '/^ /d' environment.yaml| sed 's/://g' >> envlist.txt
#echo "none" > rootdir.txt
ls -ld */ | cut -d' ' -f 9| sed 's/\\///g' >> rootdir.txt
cat rootdir.txt
'''
script {
def lines = readFile("${workspace}/rootdir.txt").readLines()
def envlines = readFile("${workspace}/envlist.txt").readLines()
lines.each { def line ->
rootdirlist = rootdirlist+','
rootdirlist = rootdirlist+line
}
envlines.each { def line ->
envlist = envlist+','
envlist = envlist+line
}
rootdirlist = rootdirlist.substring(1)
envlist = envlist.substring(1)
println(rootdirlist)
echo "please click on the link here to choose environment to deploy"
params = input(
id: 'user_Input',
message: 'Promote:',
parameters: [
[
name: 'ENVIRONMENT',
$class: 'ExtendedChoiceParameterDefinition',
type: 'PT_MULTI_SELECT',
value: "${envlist}",
description: 'Select Environment(s)'
],
[
name: 'APPLICATION',
$class: 'ExtendedChoiceParameterDefinition',
type: 'PT_MULTI_SELECT',
value: "${rootdirlist}",
description: 'Select Application(s)'
],
[
name: 'DRY_RUN_ONLY',
$class: 'BooleanParameterDefinition',
defaultValue: true,
description: 'Do you want to execute a dry run only?'
]
]
)
envs = params['ENVIRONMENT'].split(',');
folders = params['APPLICATION'].split(',');
}
}
}
stage('Monaco CLI setup') {
steps {
sh '''
wget "https://github.com/dynatrace-oss/dynatrace-monitoring-as-code/releases/download/v1.5.0/monaco-linux-amd64"
sudo cp monaco-linux-amd64 /usr/local/bin/monaco
sudo chmod +x /usr/local/bin/monaco
'''
}
}
stage('Monaco dry run') {
steps {
script {
println("value of env: "+ params['ENVIRONMENT'])
println("value of root folder: "+ params['APPLICATION'])
if(envs != null){
envs.each { env ->
if(params['APPLICATION'] != null){
folders.each { folder ->
sh "monaco -v -e $WORKSPACE/environment.yaml -se ${env} --project ${folder} --dry-run"
}
} else{
println "Application not selected"
}
}
} else {
println "Environment not selected"
}
}
}
}
stage('Dynatrace config deployment') {
when {
expression { params.DRY_RUN_ONLY == false }
}
steps {
script {
if(envs != null){
envs.each { env ->
if(params['APPLICATION'] != null){
folders.each { folder ->
sh "set +x && echo ------------ Starting Deployment on environment $env for app $folder -----------"
sh "monaco -v -e $WORKSPACE/environment.yaml -se ${env} --project ${folder}"
}
} else{
println "Application not selected"
}
}
} else {
println "Environment not selected"
}
}
}
}
}
post {
always {
cleanWs()
}
}
}