diff --git a/Jenkinsfile b/Jenkinsfile index 87e4834..50cde63 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,17 +1,41 @@ +def gv + pipeline{ agent any + stages{ - stage("Sonarqube analysis"){ - steps{ - script{ - withSonarQubeEnv(credentialsId: 'new_sonar') { - sh 'mvn sonar:sonar' + stage('code quality check via sonarQube') { + steps { + script { + if (env.BRANCH_NAME == 'dev' || env.BRANCH_NAME == 'QA' ) { + echo 'I execute on the DEV and QA branch' + gv = load "pipeline_config.groovy" + echo "sonarQube code quality check" + gv.qualityanalysis() + } else { + echo 'I execute elsewhere' } } } } + stage("Build jar") { + steps { + script { + echo "building jar" + gv.buildJar() + } + } + } + stage("Roll Back") { + steps { + script { + echo "roll back to previous version" + gv.rollback() + } + } + } } - post{ + post{ always{ echo "========always========" } @@ -22,4 +46,6 @@ pipeline{ echo "========pipeline execution failed========" } } + + } diff --git a/pipeline_config.groovy b/pipeline_config.groovy new file mode 100644 index 0000000..89699cf --- /dev/null +++ b/pipeline_config.groovy @@ -0,0 +1,42 @@ +def buildJar() { + echo "building the application..." + sh 'mvn package' +} + +def qualityanalysis() { + echo "sonarQube code quality check" + withSonarQubeEnv(credentialsId: 'mysorarqube', installationName: 'sample_java') { + sh 'mvn sonar:sonar' + } + } + +def testReport(){ + echo 'Generated Test report...' + sh 'mvn test' +} + + +def rollback() { + echo "roll back to previous version" + if (currentBuild?.getPreviousBuild()?.result == 'FAILURE') { + if (currentBuild.resultIsBetterOrEqualTo( + currentBuild.getPreviousBuild().result)) { + echo 'build has been fixed' + } + } +} + +def buildImage() { + echo "building the docker image..." + withCredentials([usernamePassword(credentialsId: 'ACR', passwordVariable: 'PASS', usernameVariable: 'USER')]) { + sh 'docker build -t maven--java/demo-app:jma-2.0 .' + sh "echo $PASS | docker login -u $USER --password-stdin" + sh 'docker push maven--java/demo-app:jma-2.0' + } +} + +def deployApp() { + echo 'deploying the application...' +} + +return this diff --git a/pom.xml b/pom.xml index 55ff3b2..89fdfcb 100644 --- a/pom.xml +++ b/pom.xml @@ -54,11 +54,11 @@ nexusdeploymentrepo - http://34.122.147.224:8081/repository/maven-snapshots/ + http://52.140.6.198:8081/repository/maven-snapshots/ nexusdeploymentrepo - http://34.122.147.224:8081/repository/maven-releases/ + http://52.140.6.198:8081/repository/advisingbank-release/ diff --git a/template.yaml b/template.yaml new file mode 100644 index 0000000..1362899 --- /dev/null +++ b/template.yaml @@ -0,0 +1,62 @@ +version: 1 + +name: Container Build +description: "This will create a multibranch pipeline job for container builds" + +type: pipeline-template +templateType: MULTIBRANCH + +parameters: + - name: github_organization + type: string + displayName: Specify the GitHub Organisation e.g. lot2learn (mandatory) + + - name: github_repo + type: string + displayName: Specify the GitHub Repository e.g. nodejs-project (mandatory) + + - name: headWildcardFilterIncludes + type: string + displayName: "Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*" + + - name: headWildcardFilterExcludes + type: string + displayName: "Space-separated list of name patterns to ignore even if matched by the includes list. For example: release alpha-* beta-*" + + - name: github_creds + type: credentials + displayName: Specify the GitHub Credentials ID name for the source code repository (mandatory) + + - name: dockerfiles + type: string + displayName: Specify the path to the Dockerfile(s) as semicolon delimited string e.g. app01/Dockerfile (optional) + + - name: build_arguments + type: string + displayName: Specify the container build arguments as semicolon delimited string e.g. VAR1=FOO;VAR2=BAR (optional) + + - name: docker_tag + type: string + displayName: Specify the container tagname e.g. arnabdnany1706/smartdb:latest (mandatory) + +multibranch: + branchSource: + github: + repoOwner: ${github_organization} + repository: ${github_repo} + credentialsId: ${github_creds} + traits: + - gitHubBranchDiscovery: + strategyId: 3 + - headWildcardFilter: + includes: ${headWildcardFilterIncludes} + excludes: ${headWildcardFilterExcludes} + + strategy: + $class: DefaultBranchPropertyStrategy # All branches get the same properties + props: + - $class: NoTriggerBranchProperty # Suppress automatic SCM triggering + + orphanedItemStrategy: + daysToKeep: 60 + scanRepositoryInterval: 15 minutes diff --git a/vars/config.groovy b/vars/config.groovy new file mode 100644 index 0000000..ce5f7bd --- /dev/null +++ b/vars/config.groovy @@ -0,0 +1,23 @@ +def buildJar() { + echo "building the application..." + sh 'mvn package' +} + +def rollback() { + echo "roll back to previous version" + hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) == true +} +def buildImage() { + echo "building the docker image..." + withCredentials([usernamePassword(credentialsId: 'ACR', passwordVariable: 'PASS', usernameVariable: 'USER')]) { + sh 'docker build -t maven--java/demo-app:jma-2.0 .' + sh "echo $PASS | docker login -u $USER --password-stdin" + sh 'docker push maven--java/demo-app:jma-2.0' + } +} + +def deployApp() { + echo 'deploying the application...' +} + +return this diff --git a/vars/dockerBuild.groovy b/vars/dockerBuild.groovy new file mode 100644 index 0000000..9804d4e --- /dev/null +++ b/vars/dockerBuild.groovy @@ -0,0 +1,19 @@ +def login() { + withCredentials([usernamePassword(credentialsId: '******', usernameVariable: '*****', passwordVariable: '*****')]) { + sh """ + docker login --username="${hubUsername}" --password="${hubPassword}" + """ + } +} + +def build(String tag) { + sh """ + docker build -t "${tag}" . + """ +} + +def push(String tag) { + sh """ + docker push "${tag}" + """ +} diff --git a/vars/params.yaml b/vars/params.yaml new file mode 100644 index 0000000..1362899 --- /dev/null +++ b/vars/params.yaml @@ -0,0 +1,62 @@ +version: 1 + +name: Container Build +description: "This will create a multibranch pipeline job for container builds" + +type: pipeline-template +templateType: MULTIBRANCH + +parameters: + - name: github_organization + type: string + displayName: Specify the GitHub Organisation e.g. lot2learn (mandatory) + + - name: github_repo + type: string + displayName: Specify the GitHub Repository e.g. nodejs-project (mandatory) + + - name: headWildcardFilterIncludes + type: string + displayName: "Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*" + + - name: headWildcardFilterExcludes + type: string + displayName: "Space-separated list of name patterns to ignore even if matched by the includes list. For example: release alpha-* beta-*" + + - name: github_creds + type: credentials + displayName: Specify the GitHub Credentials ID name for the source code repository (mandatory) + + - name: dockerfiles + type: string + displayName: Specify the path to the Dockerfile(s) as semicolon delimited string e.g. app01/Dockerfile (optional) + + - name: build_arguments + type: string + displayName: Specify the container build arguments as semicolon delimited string e.g. VAR1=FOO;VAR2=BAR (optional) + + - name: docker_tag + type: string + displayName: Specify the container tagname e.g. arnabdnany1706/smartdb:latest (mandatory) + +multibranch: + branchSource: + github: + repoOwner: ${github_organization} + repository: ${github_repo} + credentialsId: ${github_creds} + traits: + - gitHubBranchDiscovery: + strategyId: 3 + - headWildcardFilter: + includes: ${headWildcardFilterIncludes} + excludes: ${headWildcardFilterExcludes} + + strategy: + $class: DefaultBranchPropertyStrategy # All branches get the same properties + props: + - $class: NoTriggerBranchProperty # Suppress automatic SCM triggering + + orphanedItemStrategy: + daysToKeep: 60 + scanRepositoryInterval: 15 minutes