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