DO-132/add_latest_redirect (#14)

[DO-132]

Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/14
Reviewed-by: Aleksandr Vodyanov <aleksandr.vodyanov@avroid.tech>
This commit is contained in:
Boris Shestov
2024-01-25 13:29:20 +03:00
parent 5d6e266419
commit b4435cf119

View File

@@ -1,6 +1,8 @@
/* groovylint-disable NestedBlockDepth */
@Library('shared-lib') _ @Library('shared-lib') _
import tech.avroid.scm.Git import tech.avroid.scm.Git
import tech.avroid.api.Nexus
properties([ properties([
buildDiscarder(logRotator(numToKeepStr: '100')), buildDiscarder(logRotator(numToKeepStr: '100')),
@@ -28,13 +30,13 @@ String nexusRepoName = 'eisen-os_doc-feature'
Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH) Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH)
podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'), podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
yaml: ''' yaml: """
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod
spec: spec:
containers: containers:
- name: doxygen - name: doxygen
image: harbor.avroid.tech/devops/doxygen:1.9.8 image: ${env.JENKINS_DOCKER_REGISTRY}/devops/doxygen:1.9.8
securityContext: securityContext:
runAsUser: 1000 runAsUser: 1000
runAsGroup: 1000 runAsGroup: 1000
@@ -50,7 +52,7 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
imagePullPolicy: Always imagePullPolicy: Always
imagePullSecrets: imagePullSecrets:
- name: harbor-registry-secret - name: harbor-registry-secret
''' """
) { ) {
node(POD_LABEL) { node(POD_LABEL) {
@@ -79,6 +81,8 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
return return
} }
Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS)
stage('Checkout source') { stage('Checkout source') {
git.clone(urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${gitRepoName}.git", git.clone(urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${gitRepoName}.git",
branch: branchName, branch: branchName,
@@ -103,29 +107,31 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
} }
String mainDocDir = 'documentation' String mainDocDir = 'documentation'
String projectDir = 'OS'
dateFormat = sh(returnStdout: true, script: "date '+%d-%m-%Y_%H-%M'").trim() dateFormat = sh(returnStdout: true, script: "date '+%d-%m-%Y_%H-%M'").trim()
String publishDir = "${dateFormat}_${branchName}" String publishDir = "${dateFormat}_${branchName}"
String mainHTMLFile = "main.html" String mainHTMLFile = 'main.html'
String latestHTMLFile = 'latest.html'
println 'Publish dir is: ' + publishDir println 'Publish dir is: ' + publishDir
String fullUrl = "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}" +
"/${mainDocDir}/${publishDir}/html/index.html"
stage('Generate documentation') { stage('Generate documentation') {
dir(projectName) { dir(projectName) {
container(name: 'doxygen') { container(name: 'doxygen') {
script { script {
sh """ sh """
doxygen doxygen
echo '<a href=${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${projectDir}/${publishDir}/html/index.html>${publishDir}</a>' > documentation.html echo '<a href=${fullUrl}>${publishDir}</a>' > documentation.html
""" """
} }
} }
} }
} }
stage('Generate main html file') { stage('Generate common html files') {
dir(projectName) { dir(projectName) {
res = httpRequest( res = httpRequest(
url: "${env.JENKINS_NEXUS_URL}/service/rest/repository/browse/${nexusRepoName}/${mainDocDir}/${projectDir}", url: "${env.JENKINS_NEXUS_URL}/service/rest/repository/browse/${nexusRepoName}/${mainDocDir}",
httpMode: 'GET', httpMode: 'GET',
quiet: true, quiet: true,
validResponseCodes: "200,201,404", validResponseCodes: "200,201,404",
@@ -137,7 +143,8 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
def lines = content.readLines() 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]+/ java.util.regex.Pattern regexpPattern =
(java.util.regex.Pattern)~/([0-9]+(-[0-9]+)+)_([0-9]+(-[0-9]+)+)_[A-Za-z0-9]+/
List docs = [] List docs = []
@@ -150,15 +157,18 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
} }
println docs println docs
String docUrl = ""
docs.each { doc -> docs.each { doc ->
docUrl = "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}" +
"/${mainDocDir}/${doc}/html/index.html"
sh """ sh """
echo '<a href=${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${projectDir}/${doc}/html/index.html>${doc}</a> <br />' >> ${mainHTMLFile} echo '<a href=${docUrl}>${doc}</a> <br />' >> ${mainHTMLFile}
""" """
} }
} }
sh """ sh """
echo '<a href=${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${projectDir}/${publishDir}/html/index.html>${publishDir}</a>' >> ${mainHTMLFile} echo '<a href=${fullUrl}>${publishDir}</a>' >> ${mainHTMLFile}
echo '<meta http-equiv="refresh" content="0;URL=${fullUrl}" />' > ${latestHTMLFile}
""" """
} }
} }
@@ -168,23 +178,30 @@ podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: '/data'),
usernameVariable: 'USERNAME', usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD')]) { passwordVariable: 'PASSWORD')]) {
List customHTMLFiles = [mainHTMLFile, latestHTMLFile]
dir("${projectName}/") { dir("${projectName}/") {
res = httpRequest(
url: "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${projectDir}/${mainHTMLFile}", customHTMLFiles.each { htmlFile ->
authentication: env.JENKINS_NEXUS_CREDENTIALS, String artifactUrl = "${env.JENKINS_NEXUS_URL}/repository/" +
httpMode: "PUT", "${nexusRepoName}/${mainDocDir}/${htmlFile}"
uploadFile: mainHTMLFile, nexus.upload(
validResponseCodes: "201", script: this,
wrapAsMultipart: false artifactUrl: "${artifactUrl}",
host: env.JENKINS_NEXUS_URL, // +
credentials: env.JENKINS_NEXUS_CREDENTIALS, // +
artifactPath: htmlFile,
repository: nexusRepoName
) )
println("Status: "+res.status) }
} }
dir("${projectName}/Eisen_Docs/html/") { dir("${projectName}/Eisen_Docs/html/") {
sh """ sh """
find . -type f -exec curl -s --user ${USERNAME}:${PASSWORD} --ftp-create-dirs -T {} \ find . -type f -exec curl -s --user ${USERNAME}:${PASSWORD} --ftp-create-dirs -T {} \
${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${projectDir}/${publishDir}/html/{} \\; ${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}/${mainDocDir}/${publishDir}/html/{} \\;
""" """
} }
} }