-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from opsta/stage-maven-unit
Initial first version with Maven CI/CD
- Loading branch information
Showing
10 changed files
with
438 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Build and push Docker Image | ||
* | ||
* @param imgNames List of Docker Images name | ||
* eg. ['module-1', 'module-2'] | ||
* @param imgTag String of Docker Image Tag | ||
* eg. dev | ||
* @param imgRepoServerUrl String of Private Docker Registry Server Url | ||
* that you want to authen | ||
* eg. https://registry.hub.docker.com | ||
* @param imgRepoJenkinsCred String of Jenkins Credentials name | ||
* @param imgNamePrefix String of Docker Image Prefix | ||
* eg. private.registry.com/username | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* mavenSettingsFilePath: String Custom Maven settings file path | ||
* eg. ./.m2/maven-mirror-settings.yml | ||
* ] | ||
*/ | ||
def call( | ||
imgNames, | ||
imgTag, | ||
imgRepoServerUrl, | ||
imgRepoJenkinsCred, | ||
imgNamePrefix, | ||
Map paramArgs = [:] | ||
) { | ||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
containerName: 'docker', | ||
mavenSettingsFilePath: '' | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
stage('Build and Push Docker Image') { | ||
container(args.containerName) { | ||
|
||
// Enable Docker Buildkit to improve build speed and enable new features | ||
// Require Docker > 18.09 | ||
withEnv(['DOCKER_BUILDKIT=1']) { | ||
|
||
// Authen with Private Registry | ||
docker.withRegistry("${imgRepoServerUrl}", "${imgRepoJenkinsCred}") { | ||
|
||
// In case of we build multiple images per git repository | ||
imgNames.each { item -> | ||
|
||
// Build variables | ||
imgFullName = "${imgNamePrefix}/${item}" | ||
if (imgNames.size() > 1) { | ||
dockerfile = "Dockerfile.${item}" | ||
} else { | ||
dockerfile = "Dockerfile" | ||
} | ||
echo "Start building ${item} image [${imgFullName}:${imgTag}]" | ||
|
||
// Search and replace Maven custom settings file parameter | ||
if(!args.mavenSettingsFilePath.isEmpty()) { | ||
sh "sed -i 's!\\(RUN .*\\)-s .* \\(.*\\)!\\1-s ${args.mavenSettingsFilePath} \\2!g' ${dockerfile}" | ||
} | ||
|
||
// Build and Push Docker Image | ||
docker.build("${imgFullName}:${imgTag}", "-f ${dockerfile} --pull .").push() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Git Clone Repository and Checkout | ||
* | ||
* @return scmVars Object | ||
*/ | ||
def call() { | ||
stage('Clone repository') { | ||
scmVars = checkout scm | ||
} | ||
return scmVars | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Run OWASP Dependency Check with Maven | ||
* | ||
* @param projectName String of Project Name | ||
* @param appName String of Application Name | ||
* @param envName String of Environment Name | ||
* eg. dev | ||
* @param kubeConfigJenkinsCred String of kubeconfig Jenkins Credentials name | ||
* @param scmVars Object from checkout scm in Jenkinsfile | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* helmValueFile: String Helm value file path | ||
* helmChartPath: String Helm Chart directory path | ||
* ] | ||
*/ | ||
def call( | ||
projectName, | ||
appName, | ||
envName, | ||
kubeConfigJenkinsCred, | ||
scmVars, | ||
Map paramArgs = [:] | ||
) { | ||
|
||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
containerName: 'helm', | ||
helmValueFile: "k8s/helm-values/${envName}/values-${projectName}-${envName}-${appName}.yaml", | ||
helmChartPath: 'k8s/helm' | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
stage("Deploy ${appName}") { | ||
container(args.containerName) { | ||
withCredentials([file(credentialsId: kubeConfigJenkinsCred, variable: 'KUBECONFIG')]) { | ||
sh """ | ||
mkdir -p ~/.kube/ | ||
cat $KUBECONFIG > ~/.kube/config | ||
sed -i 's/COMMIT_ID: CHANGE_COMMIT_ID/COMMIT_ID: ${scmVars.GIT_COMMIT}/g' ${args.helmValueFile} | ||
helm upgrade -i -f ${args.helmValueFile} --namespace ${projectName}-${envName} --wait \ | ||
${projectName}-${envName}-${appName} ${args.helmChartPath} | ||
""" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Run OWASP Dependency Check with Maven | ||
* | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* odcInstallation: String OWASP Dependency Check Installation Name in Jenkins | ||
* owaspReportFileName: String OWASP output report file name | ||
* owaspDataPath: String OWASP Download Data Path | ||
* ] | ||
*/ | ||
def call( | ||
Map paramArgs = [:] | ||
) { | ||
|
||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
odcInstallation: 'dependency-check', | ||
containerName: 'maven', | ||
owaspReportFileName: 'dependency-check-report.xml', | ||
owaspDataPath: '/home/jenkins/dependency-check-data' | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
stage('Run OWASP Dependency Check with Maven') { | ||
container(args.containerName) { | ||
dependencycheck( | ||
additionalArguments: "--out ${args.owaspReportFileName} --data ${args.owaspDataPath}", | ||
odcInstallation: args.odcInstallation | ||
) | ||
dependencyCheckPublisher( | ||
pattern: args.owaspReportFileName | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Run SonarQube Analysis with Maven | ||
* | ||
* @param sonarQubeEnv String of SonarQube Environment Name in Jenkins | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* mavenSettingsFilePath: String Custom Maven settings file path | ||
* eg. ./.m2/maven-mirror-settings.yml | ||
* sonarQubeMavenPluginVersion: String SonarQube Maven Plugin Version. Pick version from here | ||
* https://mvnrepository.com/artifact/org.sonarsource.scanner.maven/sonar-maven-plugin | ||
* ] | ||
*/ | ||
def call( | ||
String sonarQubeEnv, | ||
Map paramArgs = [:] | ||
) { | ||
|
||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
sonarQubeMavenPluginVersion: "3.7.0.1746", | ||
mavenSettingsFilePath: '', | ||
containerName: 'maven' | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
stage('SonarQube Analysis with Maven') { | ||
container(args.containerName) { | ||
withSonarQubeEnv(sonarQubeEnv) { | ||
|
||
// Check if need custom maven settings | ||
mavenSettingsFilePathParameter = '' | ||
if(!args.mavenSettingsFilePath.isEmpty()) { | ||
mavenSettingsFilePathParameter = "-s ${args.mavenSettingsFilePath}" | ||
} | ||
|
||
sh """ | ||
mvn ${mavenSettingsFilePathParameter} -e \ | ||
org.sonarsource.scanner.maven:sonar-maven-plugin:${args.sonarQubeMavenPluginVersion}:sonar | ||
""" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Run Maven Unit Tests | ||
* | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* mavenSettingsFilePath: String Custom Maven settings file path | ||
* eg. ./.m2/maven-mirror-settings.yml | ||
* jUnitReportPath: String JUnit Output Report Path | ||
* ] | ||
*/ | ||
def call( | ||
Map paramArgs = [:] | ||
) { | ||
|
||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
mavenSettingsFilePath: '', | ||
containerName: 'maven', | ||
jUnitReportPath: '' | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
try { | ||
stage('Maven Unit Tests') { | ||
container(args.containerName) { | ||
|
||
// Check if need custom maven settings | ||
mavenSettingsFilePathParameter = '' | ||
if(!args.mavenSettingsFilePath.isEmpty()) { | ||
mavenSettingsFilePathParameter = "-s ${args.mavenSettingsFilePath}" | ||
} | ||
|
||
sh """ | ||
mvn -e ${mavenSettingsFilePathParameter} clean test | ||
""" | ||
} | ||
} | ||
} catch(Exception e) { | ||
// Print error | ||
echo e.toString() | ||
currentBuild.result = 'FAILURE' | ||
} finally { | ||
if(!args.jUnitReportPath.isEmpty()) { | ||
junit args.jUnitReportPath | ||
} | ||
// Stop job when failure | ||
if(currentBuild.result == 'FAILURE') { | ||
sh "exit 1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Publish Robot Framework Result | ||
* | ||
* @param paramArgs Map of optional variables | ||
* [ | ||
* containerName: String Container name in podTemplate | ||
* robotOutputBasePath: String Temporary Robot Output Report Base Path | ||
* passThreshold: Integer Percent of test to pass threshold | ||
* unstableThreshold: Integer Percent of test threshold to make build unstable | ||
* ] | ||
*/ | ||
def call( | ||
Map paramArgs = [:] | ||
) { | ||
|
||
// Set default optional arguments | ||
private def defaultArgs = [ | ||
containerName: 'robot', | ||
robotOutputBasePath: 'test/robot/reports', | ||
passThreshold: 100, | ||
unstableThreshold: 0 | ||
] | ||
// Replace default optional arguments with parametered arguments | ||
private def args = defaultArgs << paramArgs | ||
|
||
stage("Publish Robot Framework Result") { | ||
container(args.containerName) { | ||
step([ | ||
$class: 'RobotPublisher', | ||
disableArchiveOutput: true, | ||
outputPath: args.robotOutputBasePath, | ||
logFileName: '**/log.html', | ||
outputFileName: '**/output.xml', | ||
reportFileName: '**/report.html', | ||
otherFiles: '**/*screenshot*.png', | ||
passThreshold: args.passThreshold, | ||
unstableThreshold: args.unstableThreshold | ||
]) | ||
} | ||
} | ||
} |
Oops, something went wrong.