From c4d598738207893eb4b987a452e27e1c6f28fc1b Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Fri, 15 Nov 2024 13:28:45 +0300 Subject: [PATCH] [DO-1239] add_backend_ci (!48) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit добавил pipeline для сборки cloud-messenger-backend-api Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/48 Reviewed-by: Denis Patrakeev Co-authored-by: Rustam Tagaev Co-committed-by: Rustam Tagaev --- README.md | 2 +- jobs-dsl/folders/Cloud.groovy | 9 ++ .../cloud_messenger_core_api.groovy | 92 +++++++++++++++++++ .../cloud-messenger-core-api.groovy | 77 ++++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 jobs-dsl/folders/Cloud.groovy create mode 100644 jobs-dsl/jobs/Cloud/Apps-Backend/cloud_messenger_core_api.groovy create mode 100644 pipelines/Cloud/Apps-Backend/cloud-messenger-core-api.groovy diff --git a/README.md b/README.md index e5588f6..d893a1e 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Репозиторий для реализации методологии **IaC** для управления задачами в Jenkins c использованием плагина [Job DSL](https://plugins.jenkins.io/job-dsl/) -[Ссылка](https://jenkins.devos.club/plugin/job-dsl/api-viewer/index.html) на локальную документацию. +[Ссылка](https://jenkins.avroid.tech/plugin/job-dsl/api-viewer/index.html) на локальную документацию. Джобы **dsl** должны называться через "_" пробелы и "-" запрещены, потому что у **job dsl** проблемы с парсингом. diff --git a/jobs-dsl/folders/Cloud.groovy b/jobs-dsl/folders/Cloud.groovy new file mode 100644 index 0000000..97ab2bd --- /dev/null +++ b/jobs-dsl/folders/Cloud.groovy @@ -0,0 +1,9 @@ +folder('Cloud') { + displayName('Cloud') + description('Cloud projects') +} + +folder('Cloud/Apps-Backend') { + displayName('Apps-Backend') + description('Apps-Backend organization') +} diff --git a/jobs-dsl/jobs/Cloud/Apps-Backend/cloud_messenger_core_api.groovy b/jobs-dsl/jobs/Cloud/Apps-Backend/cloud_messenger_core_api.groovy new file mode 100644 index 0000000..6af4c5e --- /dev/null +++ b/jobs-dsl/jobs/Cloud/Apps-Backend/cloud_messenger_core_api.groovy @@ -0,0 +1,92 @@ +multibranchPipelineJob('Cloud/Apps-Backend/cloud-messenger-core-api') { + description('cloud-messenger-core-api') + displayName('cloud-messenger-core-api') + factory { + remoteJenkinsFileWorkflowBranchProjectFactory { + localMarker('') + matchBranches(true) + fallbackBranch('master') + lookupInParameters(false) + remoteJenkinsFile('pipelines/Cloud/Apps-Backend/cloud-messenger-core-api.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('cloud-messenger-core-api') + credentialsId('user-jenkins-in-gitea') + id('Apps-Backend/cloud-messenger-core-api') + 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/pipelines/Cloud/Apps-Backend/cloud-messenger-core-api.groovy b/pipelines/Cloud/Apps-Backend/cloud-messenger-core-api.groovy new file mode 100644 index 0000000..0baf870 --- /dev/null +++ b/pipelines/Cloud/Apps-Backend/cloud-messenger-core-api.groovy @@ -0,0 +1,77 @@ +@Library('shared-lib') _ + +import tech.avroid.kube.PodTemplates +import tech.avroid.scm.Git + +Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH) + +String repoPath = 'Apps-Backend/cloud-messenger-core-api.git' +String ciUser = 'robot\$\$ci' +String publishBranch = 'develop' + +Map configuration = [ + vaultUrl: 'https://vault.avroid.tech', + vaultCredentialId: 'vault-role', + engineVersion: 2 +] + +List dockerCreds = [ + [path: 'team-devops/services/registry/Harbor/harbor.avroid.tech', engineVersion: 2, + secretValues: + [ + [vaultKey: 'service.user.ci.login'], + [vaultKey: 'service.user.ci.token'], + ] + ] +] + +slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY, ["${env.JENKINS_K8S_HARBOR_SECRET}"]) + +slaveTemplates.jnlp { + slaveTemplates.poetry { + slaveTemplates.docker { + node(POD_LABEL){ + + stage('Download sources') { + git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}", + branch: git.getBranch()]) + } + + stage('prepare app'){ + container('poetry'){ + sh 'make setup' + } + } + + withVault([configuration: configuration, vaultSecrets: dockerCreds]) { + String dockerToken = getProperty('service.user.ci.token') + String makeArgs = " DOCKER_REGISTRY='${env.JENKINS_DOCKER_REGISTRY}'" + + " DOCKER_REGISTRY_USER='${ciUser}'" + + " DOCKER_REGISTRY_PASSWORD='${dockerToken}'" + + container('docker'){ + stage('build image'){ + sh """#!/bin/sh + apk add make + make build-image ${makeArgs} + """ + } + + stage('push image'){ + sh """#!/bin/sh + apk add make + make build-image ${makeArgs} + """ + + if (git.getBranch() == publishBranch){ + sh """#!/bin/sh + make push-image ${makeArgs} + """ + } + } + } + } + } + } + } +}