/* groovylint-disable NestedBlockDepth */
@Library('shared-lib') _
import tech.avroid.scm.Git
import tech.avroid.api.Nexus
properties([
buildDiscarder(logRotator(numToKeepStr: '100')),
disableConcurrentBuilds(),
parameters([
string(name: 'BRANCH', defaultValue: 'master',
description: 'OS repo branch name.'),
booleanParam(name: 'onlyUpdatePipeline', defaultValue: false,
description: 'Only update pipeline and exit.')
]),
pipelineTriggers(
[GenericTrigger(
causeString: 'Generic Cause',
allowSeveralTriggersPerBuild: false,
genericVariables: [[defaultValue: '', key: 'webhookBody', value: '$']],
token: env.JOB_NAME)
]
)
])
String projectName = 'OS'
String owner = 'Eisen'
String gitRepoName = "${owner}/${projectName}"
String nexusRepoName = 'eisen-os_doc-feature'
Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH)
podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
yaml: """
apiVersion: v1
kind: Pod
spec:
containers:
- name: doxygen
image: ${env.JENKINS_DOCKER_REGISTRY}/devops/doxygen:1.9.8
securityContext:
runAsUser: 1000
runAsGroup: 1000
command: ["/bin/bash"]
tty: true
resources:
limits:
cpu: 300m
memory: 256Mi
requests:
cpu: 300m
memory: 256Mi
imagePullPolicy: Always
imagePullSecrets:
- name: harbor-registry-secret
"""
) {
node(POD_LABEL) {
try {
stage('Update pipeline') {
echo('Pipeline updated')
}
String branchName = ''
if (env.webhookBody) {
Map fullJSON = readJSON(text: webhookBody)
if (fullJSON.containsKey('head_commit')) {
String hashCommit = fullJSON['head_commit']['id']
String shorthashCommit = hashCommit.substring(0, 10)
branchName = shorthashCommit
} else {
branchName = params.BRANCH
}
} else {
branchName = params.BRANCH
}
println 'branchName: ' + branchName
if (params.onlyUpdatePipeline) {
currentBuild.result = 'SUCCESS'
return
}
Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS)
stage('Checkout source') {
git.clone(urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${gitRepoName}.git",
branch: branchName,
path: projectName,
disableSubmodules: true)
}
stage('Get doxygen-awesome-css') {
String doxygenAwesomeName = 'doxygen-awesome-css'
String doxygenAwesomeVersion = '2.3.1'
String doxygenAwesomeZipFile = "${doxygenAwesomeName}-${doxygenAwesomeVersion}.zip"
String doxygenAwesomeUrl = 'https://nexus.avroid.tech/repository/devops-raw-custom-files/' +
"doxygen-awesome-css/${doxygenAwesomeZipFile}"
dir(projectName) {
httpRequest(
url: doxygenAwesomeUrl,
outputFile: doxygenAwesomeZipFile
)
unzip zipFile: doxygenAwesomeZipFile
sh "mv ${doxygenAwesomeName}-${doxygenAwesomeVersion} ${doxygenAwesomeName}"
}
}
String mainDocDir = 'documentation'
dateFormat = sh(returnStdout: true, script: "date '+%d-%m-%Y_%H-%M'").trim()
String publishDir = "${dateFormat}_${branchName}"
String mainHTMLFile = 'main.html'
String latestHTMLFile = 'latest.html'
println 'Publish dir is: ' + publishDir
String fullUrl = "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}" +
"/${mainDocDir}/${publishDir}/html/index.html"
stage('Generate documentation') {
dir(projectName) {
container(name: 'doxygen') {
script {
sh """
doxygen
echo '${publishDir}' > documentation.html
"""
}
}
}
}
stage('Generate common html files') {
dir(projectName) {
res = httpRequest(
url: "${env.JENKINS_NEXUS_URL}/service/rest/repository/browse/${nexusRepoName}/${mainDocDir}",
httpMode: 'GET',
quiet: true,
validResponseCodes: "200,201,404",
authentication: "${env.JENKINS_NEXUS_CREDENTIALS}"
)
if (res.status != 404) {
String content = res.content
def lines = content.readLines()
java.util.regex.Pattern regexpPattern =
(java.util.regex.Pattern)~/([0-9]+(-[0-9]+)+)_([0-9]+(-[0-9]+)+)_[A-Za-z0-9]+/
List docs = []
lines.each { line ->
def match = (line =~ regexpPattern)
if (match) {
docs << match[0][0]
}
}
println docs
String docUrl = ""
docs.each { doc ->
docUrl = "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}" +
"/${mainDocDir}/${doc}/html/index.html"
sh """
echo '${doc}
' >> ${mainHTMLFile}
"""
}
}
sh """
echo '${publishDir}' >> ${mainHTMLFile}
echo '' > ${latestHTMLFile}
"""
}
}
stage('Publish documentation') {
withCredentials([usernamePassword(credentialsId: "${env.JENKINS_NEXUS_CREDENTIALS}",
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD')]) {
List customHTMLFiles = [mainHTMLFile, latestHTMLFile]
dir("${projectName}/") {
customHTMLFiles.each { htmlFile ->
String artifactUrl = "${env.JENKINS_NEXUS_URL}/repository/" +
"${nexusRepoName}/${mainDocDir}/${htmlFile}"
nexus.upload(
script: this,
artifactUrl: "${artifactUrl}",
host: env.JENKINS_NEXUS_URL, // +
credentials: env.JENKINS_NEXUS_CREDENTIALS, // +
artifactPath: htmlFile,
repository: nexusRepoName
)
}
}
dir("${projectName}/Eisen_Docs/html/") {
sh """
find . -type f -exec curl -s --user ${USERNAME}:${PASSWORD} --ftp-create-dirs -T {} \
${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${publishDir}/html/{} \\;
"""
}
}
}
} catch (err) {
echo 'ERROR: ' + err.getMessage()
errorMessage = err.getMessage()
currentBuild.result = 'FAILURE'
} finally {
cleanWs()
}
}
}