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/.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..7c9e290 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -0,0 +1,120 @@ +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 + environment { + REGISTRY_NAME = 'registry.entcor/trust-module' + IMAGE_NAME = "test-ci-cd" + GITEA_REPOSITORY_URL = "http://git.entcor/api/v1/repos/" + } + stages { + 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" + } + sh "echo ${lastVersion}" + def (major, minor, patch) = lastVersion.tokenize('.') + def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" + env.IMAGE_TAG = newVersion + env.NEW_VERSION = newVersion + } + } + } + } + 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/${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 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." + } + aborted { + echo "Pipeline was aborted." + } + } +} + +//TODO: дженкинс entcor убрать