From f2c1c3f614f00274fecf3b8d36db8b7c161478f1 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Wed, 12 Feb 2025 11:54:08 +0300 Subject: [PATCH] [DO-1521] add_messenger_users_build (!81) Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/81 Reviewed-by: Vasiliy Chipizhin --- .../web_cloud_messenger_users.groovy | 92 +++++++++++++ .../web_cloud_messenger_users_deploy.groovy | 87 ++++++++++++ .../web-cloud-messenger-users.groovy | 90 ++++++++++++ .../web-cloud-messenger-users-deploy.groovy | 130 ++++++++++++++++++ 4 files changed, 399 insertions(+) create mode 100644 jobs-dsl/jobs/Cloud/Apps-Frontend/web_cloud_messenger_users.groovy create mode 100644 jobs-dsl/jobs/Cloud/Deploy/Frontend/web_cloud_messenger_users_deploy.groovy create mode 100644 pipelines/Cloud/Apps-Frontend/web-cloud-messenger-users.groovy create mode 100644 pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy.groovy diff --git a/jobs-dsl/jobs/Cloud/Apps-Frontend/web_cloud_messenger_users.groovy b/jobs-dsl/jobs/Cloud/Apps-Frontend/web_cloud_messenger_users.groovy new file mode 100644 index 0000000..19d57f1 --- /dev/null +++ b/jobs-dsl/jobs/Cloud/Apps-Frontend/web_cloud_messenger_users.groovy @@ -0,0 +1,92 @@ +multibranchPipelineJob('Cloud/Apps-Frontend/web-cloud-messenger-users') { + description('web-cloud-messenger-users') + displayName('web-cloud-messenger-users') + factory { + remoteJenkinsFileWorkflowBranchProjectFactory { + localMarker('') + matchBranches(true) + fallbackBranch('master') + lookupInParameters(false) + remoteJenkinsFile('pipelines/Cloud/Apps-Frontend/web-cloud-messenger-users.groovy') + remoteJenkinsFileSCM { + gitSCM { + userRemoteConfigs { + userRemoteConfig { + url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git") + credentialsId("${JENKINS_GIT_CREDENTIALS_HTTP}") + name('') + refspec('') + } + } + gitTool('') + browser {} + } + } + } + } + + branchSources { + branchSource { + source { + giteaSCMSource { + serverUrl("${JENKINS_GIT_REPOSITORY_URL}") + repoOwner('Apps-Frontend') + repository('web-cloud-messenger-users') + credentialsId("${JENKINS_GIT_CREDENTIALS_HTTP}") + id('Apps-Frontend/web-cloud-messenger-users') + 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}/*') + } + } + } + } + } + } + } + } + orphanedItemStrategy { + discardOldItems { + numToKeep(20) + } + + defaultOrphanedItemStrategy { + pruneDeadBranches(true) + numToKeepStr('10') + daysToKeepStr('10') + } + } +} diff --git a/jobs-dsl/jobs/Cloud/Deploy/Frontend/web_cloud_messenger_users_deploy.groovy b/jobs-dsl/jobs/Cloud/Deploy/Frontend/web_cloud_messenger_users_deploy.groovy new file mode 100644 index 0000000..111fd7b --- /dev/null +++ b/jobs-dsl/jobs/Cloud/Deploy/Frontend/web_cloud_messenger_users_deploy.groovy @@ -0,0 +1,87 @@ +pipelineJob('Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy') { + logRotator { + numToKeep(10) + artifactNumToKeep(10) + } + + parameters { + choice { + name('ENV') + choices(['DEV']) + description('Select one of environments') + } + choiceParameter { + name('APP_VERSION') + randomName('') + filterable(true) + filterLength(1) + script { + groovyScript { + fallbackScript { + script('') + sandbox(false) + } + script { + sandbox(false) + script( +""" +import groovy.json.JsonSlurperClassic +import groovy.json.model.* +import com.cloudbees.plugins.credentials.CredentialsProvider +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials +import java.util.regex.Pattern + +def createGetHttpClient(String url, String jenkinsCreds) { + def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class) + def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null } + String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password; + String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8")) + String authHeaderValue = "Basic " + encodedAuth + def httpClient = new URL(url).openConnection() as HttpURLConnection + httpClient.setRequestMethod('GET') + httpClient.setRequestProperty("Authorization", authHeaderValue) + httpClient.setRequestProperty("Accept", "application/json") + return httpClient +} + +String harborApiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/cloud/repositories/web-cloud-messenger-users/" + + "artifacts?page=1&page_size=100&with_tag=true&sort=-push_time" + +def httpClientHarbor = createGetHttpClient(harborApiUrl, "${JENKINS_HARBOR_CREDENTIALS}") +httpClientHarbor.connect() + +List imageVersions = [] +def harborResponse = new JsonSlurperClassic().parseText(httpClientHarbor.inputStream.text) + +harborResponse.each { image -> + image.tags.each { tag -> + imageVersions.add(tag.name) + } +} + +return imageVersions +""" + ) + } + } + } + choiceType('PT_SINGLE_SELECT') + } + + } + + definition { + cpsScm { + scm { + git { + remote { + url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git") + credentials("${JENKINS_GIT_CREDENTIALS_HTTP}") + } + branch('master') + } + } + scriptPath('pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy.groovy') + } + } +} diff --git a/pipelines/Cloud/Apps-Frontend/web-cloud-messenger-users.groovy b/pipelines/Cloud/Apps-Frontend/web-cloud-messenger-users.groovy new file mode 100644 index 0000000..b8f6f90 --- /dev/null +++ b/pipelines/Cloud/Apps-Frontend/web-cloud-messenger-users.groovy @@ -0,0 +1,90 @@ +@Library('shared-lib') _ + +import tech.avroid.kube.PodTemplates +import tech.avroid.scm.Git +import tech.avroid.jenkins.Notifications + +properties([ + buildDiscarder(logRotator(daysToKeepStr: '10', + numToKeepStr: '10')), + disableConcurrentBuilds() +]) + +String repoPath = 'Apps-Frontend/web-cloud-messenger-users.git' +String maintainerUser = '' +String appVersion = '' +String publishBranch = 'develop' +String imageTag = '' +Map gitVars = [:] + +Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH) +PodTemplates slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY, ["${env.JENKINS_K8S_HARBOR_SECRET}"]) + +String branch = git.getBranch() + +slaveTemplates.jnlp { + slaveTemplates.docker { + slaveTemplates.nodejs(imageVersion='18.19-alpine3.18') { + try { + node(POD_LABEL){ + stage('Download sources') { + gitVars = git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}", branch: branch]) + } + + container('nodejs'){ + stage('build app'){ + sh """#!/bin/sh + npm config set registry ${env.JENKINS_NEXUS_NPM_REPOSITORY} + npm install + npm run build + """ + } + } + + container('docker'){ + stage('build and push image'){ + appVersion = readJSON(file: 'package.json').version + imageTag = "${appVersion}-${gitVars.GIT_COMMIT.take(5)}" + docker.withRegistry("https://${env.JENKINS_DOCKER_REGISTRY}", env.JENKINS_HARBOR_CREDENTIALS) { + + Object buildImage = docker.build( + "${env.JENKINS_DOCKER_REGISTRY}/cloud/web-cloud-messenger-users:${imageTag}", + "-f Dockerfile ." + ) + + if (branch == publishBranch) { + maintainerUser = 'rishat.gabaidullov@avroid.team' + buildImage.push() + + stage('deploy application'){ + build job: 'Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy', + parameters: [ + string(name: 'ENV', value: 'DEV'), + string(name: 'APP_VERSION', value: imageTag) + ], wait: false + } + } + } + } + } + } + } 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: maintainerUser + ) + } + } + } +} diff --git a/pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy.groovy b/pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy.groovy new file mode 100644 index 0000000..a2cbc36 --- /dev/null +++ b/pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-users-deploy.groovy @@ -0,0 +1,130 @@ +@Library('shared-lib') _ + +import tech.avroid.kube.PodTemplates +import tech.avroid.scm.Git +import tech.avroid.jenkins.Notifications +import tech.avroid.jenkins.Jenkins + +String projectName = 'cloud' +String repositoryName = 'web-cloud-messenger-users' +String k8sAppName = repositoryName +String pageSize = 100 +String apiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/${projectName}" + + "/repositories/${repositoryName}/artifacts" +properties([ + buildDiscarder(logRotator(artifactDaysToKeepStr: '', + artifactNumToKeepStr: '10', + daysToKeepStr: '', + numToKeepStr: '10')), + disableConcurrentBuilds(), + parameters([ + choice( + name: 'ENV', + choices: ['DEV'], + description: 'Select one of environments' + ), + [$class: 'ChoiceParameter', + choiceType: 'PT_SINGLE_SELECT', + filterLength: 1, + filterable: true, + name: 'APP_VERSION', + script: [$class: 'GroovyScript', + script: [sandbox: false, script: """ +import groovy.json.JsonSlurperClassic +import groovy.json.model.* +import com.cloudbees.plugins.credentials.CredentialsProvider +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials +import java.util.regex.Pattern + +def createGetHttpClient(String url, String jenkinsCreds) { + def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class) + def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null } + String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password + String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8")) + String authHeaderValue = "Basic " + encodedAuth + def httpClient = new URL(url).openConnection() as HttpURLConnection + httpClient.setRequestMethod('GET') + httpClient.setRequestProperty("Authorization", authHeaderValue) + httpClient.setRequestProperty("Accept", "application/json") + return httpClient +} + +String harborApiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/cloud/repositories/web-cloud-messenger-users/" + + "artifacts?page=1&page_size=100&with_tag=true&sort=-push_time" + +def httpClientHarbor = createGetHttpClient(harborApiUrl, "${JENKINS_HARBOR_CREDENTIALS}") +httpClientHarbor.connect() + +List imageVersions = [] +def harborResponse = new JsonSlurperClassic().parseText(httpClientHarbor.inputStream.text) + +harborResponse.each { image -> + image.tags.each { tag -> + imageVersions.add(tag.name) + } +} + +return imageVersions +"""]], + ] + ]) + +]) + + +String repoPath = 'Apps-Frontend/web-cloud-messenger-users.git' +String branch = 'develop' +String valuesPath = ".helm/values.${params.ENV.toLowerCase()}.yaml" +String namespace = "tavro-cloud-${params.ENV.toLowerCase()}" + +String helmChart = 'web-cloud-messenger-users' +String helmRepoPath = "avroid/${helmChart}" +String helmRepo = "${env.JENKINS_NEXUS_URL}/repository/avroid-charts" + +Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH) +PodTemplates slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY, + ["${env.JENKINS_K8S_HARBOR_SECRET}"], + 'avroid-office') + +slaveTemplates.jnlp { + slaveTemplates.helm { + try { + node(POD_LABEL){ + stage('get repo with values'){ + gitVars = git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}", + branch: branch]) + } + container('helm'){ + stage('deploy'){ + echo "Deploying version ${env.APP_VERSION} to ${namespace} namespace" + sh """#!/bin/sh + helm repo add avroid ${helmRepo} + helm -n ${namespace} upgrade -f ${valuesPath} \ + --set image.tag=${env.APP_VERSION} \ + --install ${k8sAppName} ${helmRepoPath} \ + --wait + """ + } + } + } + } catch(err) { + errorMessage = err.getMessage() + + println 'ERROR: ' + errorMessage + + currentBuild.result = 'FAILURE' + + String currentBuildUser = Jenkins.GetCurrentBuildUser(script: this) + String emailSubject = "${currentBuild.currentResult}. " + + "Pipeline task: ${currentBuild.fullDisplayName}" + + Notifications.email( + script: this, + subject: emailSubject, + errorString: errorMessage, + recipientProviders: [], + to: "${currentBuildUser}@avroid.team" + ) + } + } +}