From 1454578765cb874107c605949e1bc0900f396cd3 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Tue, 26 Nov 2024 18:19:51 +0300 Subject: [PATCH] [DO-1361] add avroid service lib and DSL job for it (!51) Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/51 Reviewed-by: Denis Patrakeev Co-authored-by: Rustam Tagaev Co-committed-by: Rustam Tagaev --- .../Apps-Backend/avroid_service_lib.groovy | 101 ++++++++++++++++++ .../Apps-Backend/avroid-service-lib.groovy | 93 ++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 jobs-dsl/jobs/Cloud/Apps-Backend/avroid_service_lib.groovy create mode 100644 pipelines/Cloud/Apps-Backend/avroid-service-lib.groovy diff --git a/jobs-dsl/jobs/Cloud/Apps-Backend/avroid_service_lib.groovy b/jobs-dsl/jobs/Cloud/Apps-Backend/avroid_service_lib.groovy new file mode 100644 index 0000000..df6ac8a --- /dev/null +++ b/jobs-dsl/jobs/Cloud/Apps-Backend/avroid_service_lib.groovy @@ -0,0 +1,101 @@ +multibranchPipelineJob('Cloud/Apps-Backend/avroid-service-lib') { + description('avroid-service-lib') + displayName('avroid-service-lib') + factory { + remoteJenkinsFileWorkflowBranchProjectFactory { + localMarker('') + matchBranches(true) + fallbackBranch('master') + lookupInParameters(false) + remoteJenkinsFile('pipelines/Cloud/Apps-Backend/avroid-service-lib.groovy') + remoteJenkinsFileSCM { + gitSCM { + userRemoteConfigs { + userRemoteConfig { + url('https://git.avroid.tech/DevOps/jenkins-pipelines.git') + credentialsId('user-jenkins-in-gitea') + name('') + refspec('') + } + } + gitTool('') + browser {} + } + } + } + } + + branchSources { + branchSource { + source { + giteaSCMSource { + serverUrl('https://git.avroid.tech') + repoOwner('Apps-Backend') + repository('avroid-service-lib') + credentialsId('user-jenkins-in-gitea') + id('Apps-Backend/avroid-service-lib') + traits { + giteaBranchDiscovery { + // 1 Exclude branches that are also filed as PRs + // 2 Only branches that are also filed as PRs + // 3 Only branches that are also filed as PRs or main + // 4 All branches + strategyId(1) + } + + giteaPullRequestDiscovery { + // 1 Merging the pull request with the current target branch revision + // 2 The current pull request revision + // 3 Both the current pull request revision and the pull request merged with + // the current target branch revision + strategyId(2) + } + + giteaForkDiscovery { + // 1 Merging the pull request with the current target branch revision + // 2 The current pull request revision + // 3 Both the current pull request revision and the pull request merged with + // the current target branch revision + strategyId(2) + trust { + giteaTrustContributors() + } + } + + giteaTagDiscovery() + pruneStaleBranch() + pruneStaleTag() + + refSpecs { + templates { + refSpecTemplate { + value('+refs/heads/*:refs/remotes/@{remote}/*') + } + } + } + } + } + // uncomment after reload jenkins + // buildStrategies { + // buildRegularBranches() + // buildTags { + // atLeastDays("") + // atMostDays("7") + // } + // } + } + + } + } + orphanedItemStrategy { + discardOldItems { + numToKeep(20) + } + + defaultOrphanedItemStrategy { + pruneDeadBranches(true) + numToKeepStr('10') + daysToKeepStr('10') + } + } +} diff --git a/pipelines/Cloud/Apps-Backend/avroid-service-lib.groovy b/pipelines/Cloud/Apps-Backend/avroid-service-lib.groovy new file mode 100644 index 0000000..45ac19f --- /dev/null +++ b/pipelines/Cloud/Apps-Backend/avroid-service-lib.groovy @@ -0,0 +1,93 @@ +@Library('shared-lib') _ + +import tech.avroid.kube.PodTemplates +import tech.avroid.scm.Git +import tech.avroid.api.Nexus +import tech.avroid.jenkins.Notifications + +properties([ + buildDiscarder(logRotator(artifactDaysToKeepStr: '', + artifactNumToKeepStr: '30', + daysToKeepStr: '', + numToKeepStr: '30')), + disableConcurrentBuilds() +]) + +Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH) +Nexus nexus = new Nexus(this, env.JENKINS_NEXUS_URL, env.JENKINS_NEXUS_CREDENTIALS) + +String repoPath = 'Apps-Backend/avroid-service-lib.git' +String nexusRepoName = 'tavro-cloud-pypi-release' +String artifactUrl = "${env.JENKINS_NEXUS_URL}/repository/${nexusRepoName}" +String localArtifactDir = './dist' + +Map gitVars = [:] +Map author = [:] + +slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY, ["${env.JENKINS_K8S_HARBOR_SECRET}"]) + +slaveTemplates.jnlp { + slaveTemplates.poetry { + slaveTemplates.docker { + slaveTemplates.git { + node(POD_LABEL){ + try { + container('poetry'){ + stage('Download sources') { + gitVars = git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}", + branch: git.getBranch()]) + } + + stage('prepare app'){ + sh 'make setup' + } + + stage('build lib'){ + sh 'make build' + } + } + + container('git'){ + stage('push lib to nexus'){ + author = git.getAuthor(commit: gitVars.GIT_COMMIT) + + if (env.TAG_NAME) { + print "Upload from tag ${env.TAG_NAME}" + + dir(localArtifactDir){ + String artifact = sh( + script: """#!/bin/sh + ls *.whl + """, + returnStdout: true).trim() + print artifact + nexus.uploadPypiArtifact( + artifact: "${artifact}", + repository: nexusRepoName + ) + } + } + } + } + } catch (err) { + errorMessage = err.getMessage() + + println 'ERROR: ' + errorMessage + + currentBuild.result = 'FAILURE' + + String emailSubject = "${currentBuild.currentResult}. " + + "Pipeline task: ${currentBuild.fullDisplayName}" + Notifications.email( + script: this, + subject: emailSubject, + errorString: errorMessage, + recipientProviders: [], + to: author.email + ) + } + } + } + } + } +}