From c1649864bcd03f8b720a4ec2f2f705667ef33839 Mon Sep 17 00:00:00 2001 From: yuobrezkov Date: Thu, 10 Apr 2025 11:29:31 +0300 Subject: [PATCH] Added Jenkinsfile and Dockerfile --- Dockerfile | 19 ++++++++ Jenkinsfile | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d714a54 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM node:23.11-slim + +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" + +RUN corepack enable \ + && corepack prepare pnpm@latest --activate + +WORKDIR /app + +COPY package.json pnpm-lock.yaml package-lock.json ./ + +RUN pnpm install + +COPY . . + +ENV NODE_ENV=development + +CMD ["pnpm", "run", "start:dev"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..378e8e8 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,124 @@ +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 = "configurator" + GITEA_REPOSITORY_URL = "http://git.entcor/api/v1/repos/" + } + stages { + stage ('Initialize variables') { + steps { + script { + def hasTags = sh(script: "git tag -l | wc -l", returnStdout: true).trim().toInteger() > 0 + echo "${hasTags}" + + def lastVersion = "0.0.0" + + if (hasTags) { + lastVersion = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim() + } + + echo "Last version: ${lastVersion}" + + def (major, minor, patch) = lastVersion.tokenize('.') + def newVersion = "${major}.${minor}.${patch.toInteger() + 1}" + echo "New version: ${newVersion}" + + 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 + """ + def commitHash = sh(script: "git rev-parse HEAD~1", returnStdout: true).trim() // необходим для корректного отображения статусов + echo "PR ${prId} merged successfully into main!" + sleep(time: 15, unit: 'SECONDS') + sh "git checkout main && git pull origin main" + + sh """ + curl -v -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": "master"}' \ + "${env.GITEA_REPOSITORY_URL}deployer3000/${env.IMAGE_NAME}/releases" + """ + echo "New release succeeded!" + + def context = "test-org/${env.IMAGE_NAME}/pipeline/pr-${env.CHANGE_TARGET}" + 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." + } + } +} \ No newline at end of file