From c7a8b4f66a82e978bad0192ebbb1875e15f2a5c8 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 14:22:01 +0300 Subject: [PATCH 1/9] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20build=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 10 ++ .idea/test-ci-cd.iml | 4 + .idea/vcs.xml | 6 + Jenkinsfile | 116 +++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/test-ci-cd.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..efae37f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/.idea/test-ci-cd.iml b/.idea/test-ci-cd.iml new file mode 100644 index 0000000..7ee078d --- /dev/null +++ b/.idea/test-ci-cd.iml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index e69de29..94bd441 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -0,0 +1,116 @@ +def notify( + String context, + String giteaUser, + String giteaPass, + String repositoryUrl, + String repositoryName, + String commitHash, + String buildStatus +) { + def status = buildStatus == 'success' ? 'success' : 'failure' + def description = buildStatus == 'success' ? 'Build succeeded' : 'Build failed' + + sh """ + curl -X POST \ + -u "${giteaUser}:${giteaPass}" \ + -H "Content-Type: application/json" \ + -d '{"context":"${context}","state": "${status}", "description": "${description}"}' \ + ${repositoryUrl}deployer3000/${repositoryName}/statuses/${commitHash} + """ +} + +pipeline { + agent any + parameters { + string(name: 'PROJECT_VERSION', defaultValue: '1.0.0', description: 'SemVer version (e.g., 1.0.0)') + } + environment { + REGISTRY_NAME = 'registry.entcor/trust-module' + IMAGE_NAME = "trust-module-backend" + GITEA_REPOSITORY_URL = "http://git.entcor/api/v1/repos/" + } + stages { + stage ('Initialize variables') { + steps { + script { + // Читаем текущую версию из параметров + def (major, minor, patch) = params.PROJECT_VERSION.tokenize('.') + + // Увеличиваем патч (1.0.0 → 1.0.1) + patch = (patch as Integer) + 1 + env.IMAGE_TAG = "${major}.${minor}.${patch}" + + // Опционально: сохраняем новую версию в параметры через API (см. ниже) + echo "New version: ${env.IMAGE_TAG}" + } + } + } + stage ('Build docker image') { + when { + expression { env.CHANGE_BRANCH?.startsWith('rc') } + } + steps { + script { + def image = docker.build("${env.IMAGE_NAME}:${env.IMAGE_TAG}") + sh "docker tag ${env.IMAGE_NAME}:${env.IMAGE_TAG} ${env.REGISTRY_NAME}/${env.IMAGE_NAME}:${env.IMAGE_TAG}" + } + } + } + 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')]) { + 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/trust-module-backend/pulls/${prId}/merge + """ + echo "PR ${prId} merged successfully into main!" + 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") + + // Обновляем параметр PROJECT_VERSION через API (если нужно) + def newVersion = env.IMAGE_TAG + sh """ + curl -X POST -u ${GITEA_USER}:${GITEA_PASS} \ + "http://192.168.2.55:8080/job/${env.JOB_NAME}/buildWithParameters?PROJECT_VERSION=${newVersion}" + """ + } + } + } + } + failure { + echo "Pipeline failed. Check the logs for details." + } + aborted { + echo "Pipeline was aborted." + } + } +} + +//TODO: дженкинс entcor убрать -- 2.40.1 From e4187998500dcb9dcd0d56afd5ef98de70f15abe Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 14:56:41 +0300 Subject: [PATCH 2/9] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 94bd441..ac2600a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -26,22 +26,35 @@ pipeline { } environment { REGISTRY_NAME = 'registry.entcor/trust-module' - IMAGE_NAME = "trust-module-backend" + IMAGE_NAME = "test-ci-cd" GITEA_REPOSITORY_URL = "http://git.entcor/api/v1/repos/" } stages { stage ('Initialize variables') { steps { script { - // Читаем текущую версию из параметров - def (major, minor, patch) = params.PROJECT_VERSION.tokenize('.') - - // Увеличиваем патч (1.0.0 → 1.0.1) - patch = (patch as Integer) + 1 - env.IMAGE_TAG = "${major}.${minor}.${patch}" - - // Опционально: сохраняем новую версию в параметры через API (см. ниже) - echo "New version: ${env.IMAGE_TAG}" + 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" | \ + jq -r '.[].tag_name' | sort -V | tail -n 1 + """, returnStdout: true).trim() + + if (!lastVersion) { + lastVersion = "0.0.0" + } + + def (major, minor, patch) = lastVersion.tokenize('.') + def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" + env.IMAGE_TAG = newVersion + + sh """ + curl -X POST -u "${GITEA_USER}:${GITEA_PASS}" \ + -H "Content-Type: application/json" \ + -d '{"tag_name": "${newVersion}", "name": "Release ${newVersion}", "target_commitish": "${env.GIT_COMMIT}"}' \ + "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" + """ + } } } } @@ -93,13 +106,6 @@ pipeline { 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") - - // Обновляем параметр PROJECT_VERSION через API (если нужно) - def newVersion = env.IMAGE_TAG - sh """ - curl -X POST -u ${GITEA_USER}:${GITEA_PASS} \ - "http://192.168.2.55:8080/job/${env.JOB_NAME}/buildWithParameters?PROJECT_VERSION=${newVersion}" - """ } } } -- 2.40.1 From ade75810809310440ce274322084f60e180a302f Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 14:58:37 +0300 Subject: [PATCH 3/9] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index ac2600a..2f0d598 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -89,27 +89,27 @@ pipeline { 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')]) { - 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/trust-module-backend/pulls/${prId}/merge - """ - echo "PR ${prId} merged successfully into main!" - 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") - } - } - } - } + // 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')]) { + // 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/trust-module-backend/pulls/${prId}/merge + // """ + // echo "PR ${prId} merged successfully into main!" + // 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") + // } + // } + // } + // } failure { echo "Pipeline failed. Check the logs for details." } -- 2.40.1 From 1cbe53bba05634cb8ddb6cf613d07addcbf4d77c Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:09:03 +0300 Subject: [PATCH 4/9] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Jenkinsfile | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3ee0732..a7cb7b9 100644 --- a/.gitignore +++ b/.gitignore @@ -113,4 +113,5 @@ fabric.properties # Built Visual Studio Code Extensions *.vsix +.idea diff --git a/Jenkinsfile b/Jenkinsfile index 2f0d598..8f71ab1 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -21,9 +21,6 @@ def notify( pipeline { agent any - parameters { - string(name: 'PROJECT_VERSION', defaultValue: '1.0.0', description: 'SemVer version (e.g., 1.0.0)') - } environment { REGISTRY_NAME = 'registry.entcor/trust-module' IMAGE_NAME = "test-ci-cd" -- 2.40.1 From 669f0678ac8cf1f8e81537bd6c13f2ee10b66e73 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:11:56 +0300 Subject: [PATCH 5/9] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8f71ab1..bb54b0e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -38,9 +38,10 @@ pipeline { """, returnStdout: true).trim() if (!lastVersion) { + sh "echo we are here" lastVersion = "0.0.0" } - + sh "echo ${lastVersion}" def (major, minor, patch) = lastVersion.tokenize('.') def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" env.IMAGE_TAG = newVersion -- 2.40.1 From 1c1b0765beb2b41d8eb605b43dd9eeea40adfce5 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:18:36 +0300 Subject: [PATCH 6/9] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index bb54b0e..18d01f6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -34,11 +34,10 @@ pipeline { def lastVersion = sh(script: """ curl -s -u "${GITEA_USER}:${GITEA_PASS}" \ "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" | \ - jq -r '.[].tag_name' | sort -V | tail -n 1 + sed -n 's/.*"tag_name": *"\\([^"]*\\).*/\\1/p' | sort -V | tail -n1 """, returnStdout: true).trim() if (!lastVersion) { - sh "echo we are here" lastVersion = "0.0.0" } sh "echo ${lastVersion}" -- 2.40.1 From efa7794e38d5b8f2ecdbd57ed67e25f74dee7f42 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:30:21 +0300 Subject: [PATCH 7/9] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B06?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 80 ++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 18d01f6..afaca2b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -44,13 +44,6 @@ pipeline { def (major, minor, patch) = lastVersion.tokenize('.') def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" env.IMAGE_TAG = newVersion - - sh """ - curl -X POST -u "${GITEA_USER}:${GITEA_PASS}" \ - -H "Content-Type: application/json" \ - -d '{"tag_name": "${newVersion}", "name": "Release ${newVersion}", "target_commitish": "${env.GIT_COMMIT}"}' \ - "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" - """ } } } @@ -66,18 +59,18 @@ 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() - } - } - } - } + // 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 { @@ -86,27 +79,34 @@ pipeline { 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')]) { - // 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/trust-module-backend/pulls/${prId}/merge - // """ - // echo "PR ${prId} merged successfully into main!" - // 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") - // } - // } - // } - // } + 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')]) { + 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/trust-module-backend/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": "${newVersion}", "name": "Release ${newVersion}", "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, "trust-module-backend", commitHash, "success") + } + } + } + } failure { echo "Pipeline failed. Check the logs for details." } -- 2.40.1 From 5ee56f8e1fac14d71f41e692c6806966a0381c6a Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:33:53 +0300 Subject: [PATCH 8/9] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B07?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index afaca2b..b7a5a43 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -44,6 +44,7 @@ pipeline { def (major, minor, patch) = lastVersion.tokenize('.') def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" env.IMAGE_TAG = newVersion + env.NEW_VERSION = newVersion } } } @@ -96,7 +97,7 @@ pipeline { sh """ curl -X POST -u "${GITEA_USER}:${GITEA_PASS}" \ -H "Content-Type: application/json" \ - -d '{"tag_name": "${newVersion}", "name": "Release ${newVersion}", "target_commitish": "${env.GIT_COMMIT}"}' \ + -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!" -- 2.40.1 From e9f44ff534235718ab7c1576f5dd3bd4b7f297a0 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Wed, 9 Apr 2025 15:37:57 +0300 Subject: [PATCH 9/9] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=208?= 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 b7a5a43..7c9e290 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -91,7 +91,7 @@ pipeline { -u "${GITEA_USER}:${GITEA_PASS}" \ -H "Content-Type: application/json" \ -d '{"do":"merge"}' \ - http://git.entcor/api/v1/repos/deployer3000/trust-module-backend/pulls/${prId}/merge + http://git.entcor/api/v1/repos/deployer3000/${env.IMAGE_NAME}/pulls/${prId}/merge """ echo "PR ${prId} merged successfully into main!" sh """ -- 2.40.1