From cbc99deb368a8f131428cd1a8428e2cdfcf88bfd Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:42:38 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 114 ++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 7c9e290..c347de4 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,7 +12,7 @@ def notify( sh """ curl -X POST \ - -u "${giteaUser}:${giteaPass}" \ + -u "\${giteaUser}:\${giteaPass}" \ -H "Content-Type: application/json" \ -d '{"context":"${context}","state": "${status}", "description": "${description}"}' \ ${repositoryUrl}deployer3000/${repositoryName}/statuses/${commitHash} @@ -21,6 +21,13 @@ def notify( pipeline { agent any + parameters { + string( + name: 'VERSION', + description: 'Версия в формате X.Y.Z (например, 1.0.0). Оставьте пустым для автоматического определения', + trim: true + ) + } environment { REGISTRY_NAME = 'registry.entcor/trust-module' IMAGE_NAME = "test-ci-cd" @@ -30,21 +37,34 @@ pipeline { stage ('Initialize variables') { steps { script { - withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { - def lastVersion = sh(script: """ - curl -s -u "${GITEA_USER}:${GITEA_PASS}" \ - "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" | \ - sed -n 's/.*"tag_name": *"\\([^"]*\\).*/\\1/p' | sort -V | tail -n1 - """, returnStdout: true).trim() - - if (!lastVersion) { - lastVersion = "0.0.0" + // Если версия указана в параметрах - используем её + if (params.VERSION) { + env.IMAGE_TAG = params.VERSION + env.NEW_VERSION = params.VERSION + echo "Using manual version: ${params.VERSION}" + } + // Иначе пытаемся получить последнюю версию из Gitea + else { + withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { + def lastVersion = sh(script: """ + curl -s -u "\${GITEA_USER}:\${GITEA_PASS}" \ + "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" | \ + sed -n 's/.*"tag_name": *"\\([^"]*\\).*/\\1/p' | sort -V | tail -n1 + """, returnStdout: true).trim() + + if (!lastVersion) { + lastVersion = "0.0.0" + echo "No versions found in Gitea, using default: ${lastVersion}" + } else { + echo "Found last version in Gitea: ${lastVersion}" + } + + // Инкрементим версию + def (major, minor, patch) = lastVersion.tokenize('.') + def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" + env.IMAGE_TAG = newVersion + env.NEW_VERSION = newVersion } - sh "echo ${lastVersion}" - def (major, minor, patch) = lastVersion.tokenize('.') - def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" - env.IMAGE_TAG = newVersion - env.NEW_VERSION = newVersion } } } @@ -60,61 +80,39 @@ pipeline { } } } - // stage ('Push docker image to registry') { - // when { - // expression { env.CHANGE_BRANCH?.startsWith('rc') } - // } - // steps { - // script { - // docker.withRegistry('https://registry.entcor/harbor/', 'harbor-credentials-id') { - // docker.image("${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}").push() - // } - // } - // } - // } } post { - always { - script { - echo "Cleaning up workspace..." - sh "rm -rf ${env.WORKSPACE}/rc/ || true" - } - } success { script { if (env.CHANGE_BRANCH?.startsWith('rc')) { - echo "Attempting to merge PR ${env.CHANGE_ID} into master..." withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { + // Создаем релиз только если версия НЕ была указана вручную + if (!params.VERSION) { + sh """ + curl -X POST -u "\${GITEA_USER}:\${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"tag_name": "${env.NEW_VERSION}", "name": "Release ${env.NEW_VERSION}", "target_commitish": "${env.GIT_COMMIT}"}' \ + "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" + """ + echo "New release ${env.NEW_VERSION} created in Gitea" + } + + // Остальной код (мердж PR, нотификации) def prId = env.CHANGE_ID sh """ - curl -X POST \ - -u "${GITEA_USER}:${GITEA_PASS}" \ - -H "Content-Type: application/json" \ - -d '{"do":"merge"}' \ - http://git.entcor/api/v1/repos/deployer3000/${env.IMAGE_NAME}/pulls/${prId}/merge + curl -X POST \ + -u "\${GITEA_USER}:\${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"do":"merge"}' \ + "http://git.entcor/api/v1/repos/deployer3000/${env.IMAGE_NAME}/pulls/${prId}/merge" """ - echo "PR ${prId} merged successfully into main!" - sh """ - curl -X POST -u "${GITEA_USER}:${GITEA_PASS}" \ - -H "Content-Type: application/json" \ - -d '{"tag_name": "${env.NEW_VERSION}", "name": "Release ${env.NEW_VERSION}", "target_commitish": "${env.GIT_COMMIT}"}' \ - "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" - """ - echo "New release succeeded!" - def context = "test-org/trust-module-backend/pipeline/pr-${env.CHANGE_TARGET}" + + def context = "test-org/${env.IMAGE_NAME}/pipeline/pr-${env.CHANGE_TARGET}" def commitHash = sh(script: "git rev-parse HEAD~1", returnStdout: true).trim() - notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, "trust-module-backend", commitHash, "success") + notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, env.IMAGE_NAME, commitHash, "success") } } } } - failure { - echo "Pipeline failed. Check the logs for details." - } - aborted { - echo "Pipeline was aborted." - } } -} - -//TODO: дженкинс entcor убрать +} \ No newline at end of file From 3062aecc620002a047d7fbe2dcfaede608d0674c Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:46:03 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 110 ++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index c347de4..f59467d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,7 +12,7 @@ def notify( sh """ curl -X POST \ - -u "\${giteaUser}:\${giteaPass}" \ + -u "${giteaUser}:${giteaPass}" \ -H "Content-Type: application/json" \ -d '{"context":"${context}","state": "${status}", "description": "${description}"}' \ ${repositoryUrl}deployer3000/${repositoryName}/statuses/${commitHash} @@ -21,13 +21,6 @@ def notify( pipeline { agent any - parameters { - string( - name: 'VERSION', - description: 'Версия в формате X.Y.Z (например, 1.0.0). Оставьте пустым для автоматического определения', - trim: true - ) - } environment { REGISTRY_NAME = 'registry.entcor/trust-module' IMAGE_NAME = "test-ci-cd" @@ -37,34 +30,17 @@ pipeline { stage ('Initialize variables') { steps { script { - // Если версия указана в параметрах - используем её - if (params.VERSION) { - env.IMAGE_TAG = params.VERSION - env.NEW_VERSION = params.VERSION - echo "Using manual version: ${params.VERSION}" - } - // Иначе пытаемся получить последнюю версию из Gitea - else { - withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { - def lastVersion = sh(script: """ - curl -s -u "\${GITEA_USER}:\${GITEA_PASS}" \ - "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" | \ - sed -n 's/.*"tag_name": *"\\([^"]*\\).*/\\1/p' | sort -V | tail -n1 - """, returnStdout: true).trim() - - if (!lastVersion) { - lastVersion = "0.0.0" - echo "No versions found in Gitea, using default: ${lastVersion}" - } else { - echo "Found last version in Gitea: ${lastVersion}" - } - - // Инкрементим версию - def (major, minor, patch) = lastVersion.tokenize('.') - def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" - env.IMAGE_TAG = newVersion - env.NEW_VERSION = newVersion + withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { + lastVersion = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim() + + if (!lastVersion) { + lastVersion = "0.0.0" } + sh "echo ${lastVersion}" + def (major, minor, patch) = lastVersion.tokenize('.') + def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" + env.IMAGE_TAG = newVersion + env.NEW_VERSION = newVersion } } } @@ -80,39 +56,61 @@ pipeline { } } } + // stage ('Push docker image to registry') { + // when { + // expression { env.CHANGE_BRANCH?.startsWith('rc') } + // } + // steps { + // script { + // docker.withRegistry('https://registry.entcor/harbor/', 'harbor-credentials-id') { + // docker.image("${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}").push() + // } + // } + // } + // } } post { + always { + script { + echo "Cleaning up workspace..." + sh "rm -rf ${env.WORKSPACE}/rc/ || true" + } + } success { script { if (env.CHANGE_BRANCH?.startsWith('rc')) { + echo "Attempting to merge PR ${env.CHANGE_ID} into master..." withCredentials([usernamePassword(credentialsId: 'gitea_creds', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { - // Создаем релиз только если версия НЕ была указана вручную - if (!params.VERSION) { - sh """ - curl -X POST -u "\${GITEA_USER}:\${GITEA_PASS}" \ - -H "Content-Type: application/json" \ - -d '{"tag_name": "${env.NEW_VERSION}", "name": "Release ${env.NEW_VERSION}", "target_commitish": "${env.GIT_COMMIT}"}' \ - "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" - """ - echo "New release ${env.NEW_VERSION} created in Gitea" - } - - // Остальной код (мердж PR, нотификации) def prId = env.CHANGE_ID sh """ - curl -X POST \ - -u "\${GITEA_USER}:\${GITEA_PASS}" \ - -H "Content-Type: application/json" \ - -d '{"do":"merge"}' \ - "http://git.entcor/api/v1/repos/deployer3000/${env.IMAGE_NAME}/pulls/${prId}/merge" + curl -X POST \ + -u "${GITEA_USER}:${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"do":"merge"}' \ + http://git.entcor/api/v1/repos/deployer3000/${env.IMAGE_NAME}/pulls/${prId}/merge """ - - def context = "test-org/${env.IMAGE_NAME}/pipeline/pr-${env.CHANGE_TARGET}" + echo "PR ${prId} merged successfully into main!" + sh """ + curl -X POST -u "${GITEA_USER}:${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"tag_name": "${env.NEW_VERSION}", "name": "Release ${env.NEW_VERSION}", "target_commitish": "${env.GIT_COMMIT}"}' \ + "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" + """ + echo "New release succeeded!" + def context = "test-org/trust-module-backend/pipeline/pr-${env.CHANGE_TARGET}" def commitHash = sh(script: "git rev-parse HEAD~1", returnStdout: true).trim() - notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, env.IMAGE_NAME, commitHash, "success") + notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, "trust-module-backend", commitHash, "success") } } } } + failure { + echo "Pipeline failed. Check the logs for details." + } + aborted { + echo "Pipeline was aborted." + } } -} \ No newline at end of file +} + +//TODO: дженкинс entcor убрать From d58a1e8b003a8ae101ae3ca5ea1eedf6d4750d32 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:48:51 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20100?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index f59467d..1768078 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -99,7 +99,7 @@ pipeline { echo "New release succeeded!" def context = "test-org/trust-module-backend/pipeline/pr-${env.CHANGE_TARGET}" def commitHash = sh(script: "git rev-parse HEAD~1", returnStdout: true).trim() - notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, "trust-module-backend", commitHash, "success") + notify(context, GITEA_USER, GITEA_PASS, env.GITEA_REPOSITORY_URL, env.IMAGE_NAME, commitHash, "success") } } }